Dungeon Master
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 62953 Accepted: 23024
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!

真是越来越喜欢bfs了。。。。感觉bfs比dfs容易一点而且应用也广.。。。
题目大意:你在3d地牢里面,然后你需要找一条最快的路出去,输入l,r,c,分别是z轴,x轴,y轴的数量,你可以上下左右 上下(这个上下指的是层次)移动,如果可以出去输出最小步数,如果不可以输出Trapped!。
思路:这个题目一开始没看懂题,后来想明白了,其实这个题和二维bfs一样的,不过需要你脑子里面建模,是一个3维的,你可以把这个地图放在空间直角坐标系下建模,然后。。。然后就和二维一样,不过注意区分一下x,y,z就行了。
代码:

#include<iostream>
#include<queue>
#include<cstring>
using namespace std;

int l,r,c;
char str[30][30][30];
int vis[30][30][30];
int step[30][30][30];
int dir[6][3]={{-1,0,0},{1,0,0},{0,-1,0},{0,1,0},{0,0,1},{0,0,-1}};
struct node{
	int x,y,z;
	node(int a,int b,int c):x(a),y(b),z(c){}
	node(){}
};
void bfs(int x1,int y1,int z1,int x2,int y2,int z2){
	queue<node>st;
	st.push(node(x1,y1,z1));
	vis[x1][y1][z1]=1;
	step[x1][y1][z1]=0;
	bool flag=true;
	while(!st.empty()){
		int fxx=st.front().x;
		int fyy=st.front().y;
		int fzz=st.front().z;
		st.pop();
		for(int i=0;i<6;i++){
			int fx=fxx+dir[i][0];
			int fy=fyy+dir[i][1];
			int fz=fzz+dir[i][2];
			if(fx<0||fy<0||fz<0||fx>=r||fy>=c||fz>=l)continue;
			if(str[fz][fx][fy]=='.'&&!vis[fx][fy][fz]){
				vis[fx][fy][fz]=1;
				st.push(node(fx,fy,fz));
				step[fx][fy][fz]=step[fxx][fyy][fzz]+1;
			}
			if(fx==x2&&fy==y2&&fz==z2&&!vis[fx][fy][fz]){
				vis[fx][fy][fz]=1;
				flag=false;
				step[fx][fy][fz]=step[fxx][fyy][fzz]+1;
				cout<<"Escaped in "<<step[fx][fy][fz]<<" minute(s)."<<endl;
				break;
			}
		}
	}
	if(flag){
		cout<<"Trapped!"<<endl;
	}
}
int main(){
	while(scanf("%d%d%d",&l,&r,&c)==3&&(l||r||c)){
		memset(vis,0,sizeof(vis));
		memset(step,0,sizeof(step));
		int x1,y1,z1,x2,y2,z2;
		for(int i=0;i<l;i++){
			for(int j=0;j<r;j++){
				for(int k=0;k<c;k++){
					scanf(" %c",&str[i][j][k]);
					if(str[i][j][k]=='S'){
						z1=i;x1=j;y1=k;
					}else if(str[i][j][k]=='E'){
						z2=i;x2=j;y2=k;
					}
				}
			}
		}
		bfs(x1,y1,z1,x2,y2,z2);
	}
}