您的位置:首页 > 其它

POJ 2251:Dungeon Master

2015-07-31 15:27 549 查看
Dungeon Master

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 20687Accepted: 8004
Description

You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move diagonally and
the maze is surrounded by solid rock on all sides.

Is an escape possible? If yes, how long will it take?

Input

The input consists of a number of dungeons. Each dungeon description starts with a line containing three integers L, R and C (all limited to 30 in size).

L is the number of levels making up the dungeon.

R and C are the number of rows and columns making up the plan of each level.

Then there will follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock is indicated by a '#' and empty cells are represented by a '.'. Your starting position is indicated by 'S' and the
exit by the letter 'E'. There's a single blank line after each level. Input is terminated by three zeroes for L, R and C.
Output

Each maze generates one line of output. If it is possible to reach the exit, print a line of the form

Escaped in x minute(s).

where x is replaced by the shortest time it takes to escape.

If it is not possible to escape, print the line

Trapped!

Sample Input
3 4 5
S....
.###.
.##..
###.#

#####
#####
##.##
##...

#####
#####
#.###
####E

1 3 3
S##
#E#
###

0 0 0

Sample Output
Escaped in 11 minute(s).
Trapped!


一个Test给你几个地图,给了你一个三维的模型,其实质就是之前人在格子里走路只能是上下左右四个方向,这回如果你在中间的地图里的话,又多了两个方向,就叫成前后吧,其实质是bfs,中规中矩的一个模式而已。

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#include <queue>
using namespace std;

int L,R,C;
char value[50][50][50];
int bushu[50][50][50];

int bfs(int i,int j,int k)
{
	int flag=0;

	queue<int>x;
	queue<int>y;
	queue<int>z;

	x.push(i);
	y.push(j);
	z.push(k);

	while(x.size())
	{
		int temp1=x.front();
		int temp2=y.front();
		int temp3=z.front();

		x.pop();
		y.pop();
		z.pop();

		if(temp1-1>=1&&value[temp1-1][temp2][temp3]=='.'&&!bushu[temp1-1][temp2][temp3])
		{
			x.push(temp1-1);
			y.push(temp2);
			z.push(temp3);
			bushu[temp1-1][temp2][temp3]=bushu[temp1][temp2][temp3]+1;
		}

		if(temp1+1<=L&&value[temp1+1][temp2][temp3]=='.'&&!bushu[temp1+1][temp2][temp3])
		{
			x.push(temp1+1);
			y.push(temp2);
			z.push(temp3);
			bushu[temp1+1][temp2][temp3]=bushu[temp1][temp2][temp3]+1;
		}
		if(temp2-1>=1&&value[temp1][temp2-1][temp3]=='.'&&!bushu[temp1][temp2-1][temp3])
		{
			x.push(temp1);
			y.push(temp2-1);
			z.push(temp3);
			bushu[temp1][temp2-1][temp3]=bushu[temp1][temp2][temp3]+1;
		}
		if(temp2+1<=R&&value[temp1][temp2+1][temp3]=='.'&&!bushu[temp1][temp2+1][temp3])
		{
			x.push(temp1);
			y.push(temp2+1);
			z.push(temp3);
			bushu[temp1][temp2+1][temp3]=bushu[temp1][temp2][temp3]+1;
		}
		if(temp3-1>=1&&value[temp1][temp2][temp3-1]=='.'&&!bushu[temp1][temp2][temp3-1])
		{
			x.push(temp1);
			y.push(temp2);
			z.push(temp3-1);
			bushu[temp1][temp2][temp3-1]=bushu[temp1][temp2][temp3]+1;
		}
		if(temp3+1<=C&&value[temp1][temp2][temp3+1]=='.'&&!bushu[temp1][temp2][temp3+1])
		{
			x.push(temp1);
			y.push(temp2);
			z.push(temp3+1);
			bushu[temp1][temp2][temp3+1]=bushu[temp1][temp2][temp3]+1;
		}
		if(value[temp1-1][temp2][temp3]=='E'||value[temp1+1][temp2][temp3]=='E'||value[temp1][temp2-1][temp3]=='E'||
			value[temp1][temp2+1][temp3]=='E'||value[temp1][temp2][temp3-1]=='E'||value[temp1][temp2][temp3+1]=='E')
		{
			flag=1;
			return bushu[temp1][temp2][temp3]+1;
		}
	}
	return 0;
}

void solve()
{
	int i,j,k;
	for(i=L;i>=1;i--)
	{
		for(j=R;j>=1;j--)
		{
			for(k=C;k>=1;k--)
			{
				if(value[i][j][k]=='S')
				{
					int temp=bfs(i,j,k);
					if(temp==0)
						cout<<"Trapped!"<<endl;
					else
						cout<<"Escaped in "<<temp<<" minute(s)."<<endl;
					return;
				}
			}
		}
	}
}

int main()
{
	int i,j,k;
	while(scanf("%d%d%d",&L,&R,&C))
	{
		if(L+R+C==0)
			break;
		for(i=1;i<=L;i++)
		{
			for(j=1;j<=R;j++)
			{
				scanf("%s",value[i][j]+1);
			}
		}
		memset(bushu,0,sizeof(bushu));
		solve();
	}
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: