POJ-2251 Dungeon Master(三维搜索bfs)

Dungeon Master

Time Limit: 1000MS Memory Limit: 65536K

题目链接http://poj.org/problem?id=2251

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!


题目大意是这样的,这个迷宫是立体的,有k层,每层n行m列,从S到E,如果能够到达,输出最短时间,否则,输出Trapped!。
emmm,很明显的bfs,跟走迷宫一个样,只是多加了一个方向变量,具体代码如下

#include <cstdio>
#include <cstring>
#include <queue>
#define BYJ(cx,cy,cl,cen,n,m) (cx>=1)&&(cx<=n)&&(cy>=1)&&(cy<=m)&&(cl>=1)&&(cl<=cen)
using namespace std;
int dre[6][3]={{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};  //{x,y,z的走向}
int a[40][40][40],v[40][40][40];
int main() {
	int cen,n,m;
	while (scanf ("%d%d%d",&cen,&n,&m)) {
		if (cen==0 && n==0 && m==0) break;
		memset(a,0,sizeof(a));
		memset(v,0,sizeof(v));
		queue<int>qx,qy,ql,stp;     //这里也可以用一个结构体队列,但个人觉得这样写比较清晰 
		int sx,sy,sc,fx,fy,fc,mark=0;
		for (int k=1; k<=cen; k++)
			for (int i=1; i<=n; i++)
				for (int j=1; j<=m; j++) {
					char ch=getchar();
					while (ch!='.' && ch!='#' && ch!='S' && ch!='E') ch=getchar();
					if (ch=='#') a[k][i][j]=1;
					else if (ch=='S') sx=i,sy=j,sc=k;
					else if (ch=='E') fx=i,fy=j,fc=k;
				}
		qx.push(sx),qy.push(sy),ql.push(sc),stp.push(0);
		v[sc][sx][sy]=1;
		while (!qx.empty()) {
			if (mark) break;     //已经找到了,强制弹出,节约时间
			for (int i=0; i<6; i++) {    //六种方向入队
				    int cx,cy,cl;
					cx=qx.front()+dre[i][0];   
					cy=qy.front()+dre[i][1];
					cl=ql.front()+dre[i][2];    //层数
				if (BYJ(cx,cy,cl,cen,n,m) && !a[cl][cx][cy] && !v[cl][cx][cy]) {
					if (cx==fx && cy==fy && cl==fc) {
						printf ("Escaped in %d minute(s).\n",stp.front()+1);
						mark=1;break;			
					}
					v[cl][cx][cy]=1;
					qx.push(cx),qy.push(cy),ql.push(cl),stp.push(stp.front()+1);
				}
			}
			if (mark) break;
			qx.pop(),qy.pop(),ql.pop(),stp.pop();
		}
		if (!mark) printf ("Trapped!\n");
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43906000/article/details/88345491