POJ-2251 Dungeon Master(BFS)

来源:互联网 发布:网络舆论对社会的影响 编辑:程序博客网 时间:2024/06/06 21:57
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 5S.....###..##..###.#############.####...###########.#######E1 3 3S###E####0 0 0
Sample Output
Escaped in 11 minute(s).Trapped!


问题:三维迷宫找最短出口...直接BFS...BFS部分是没问题...但是一开始的输入有些问题导致没有AC...这种题一定注意输入格式...

#include<cstdio>#include<queue>#include<cstring>#include<iostream>using namespace std;const int maxx=35;struct point{int x;int y;int z;int step;};bool visit[maxx][maxx][maxx]; //标记是否访问过的数组 char dungeon[maxx][maxx][maxx];//储存地图的数组 int l,r,c;//各个边界 int bfs(int x,int y,int z){ point start; //开始点 queue<point> q;while(!q.empty()) q.pop(); //初始化队列 start.x=x;start.y=y;start.z=z;start.step=0;q.push(start);//入队 int t;bool found=false;while(!q.empty()){point head=q.front(); //head是当前节点 q.pop();if(dungeon[head.x][head.y][head.z]=='E'){ //如果找到路了 found=true;t=head.step; break;}head.step++;point tmp;if(head.x>0){ //向L方向探索,一正一负 tmp=head;tmp.x--;if(dungeon[tmp.x][tmp.y][tmp.z]!='#'&&visit[tmp.x][tmp.y][tmp.z]==false){visit[tmp.x][tmp.y][tmp.z]=true;q.push(tmp);}}if(head.x<l-1){tmp=head;tmp.x++;if(dungeon[tmp.x][tmp.y][tmp.z]!='#'&&visit[tmp.x][tmp.y][tmp.z]==false){visit[tmp.x][tmp.y][tmp.z]=true;q.push(tmp);}}if(head.y>0){ //向R方向探索,一正一负 tmp=head;tmp.y--;if(dungeon[tmp.x][tmp.y][tmp.z]!='#'&&visit[tmp.x][tmp.y][tmp.z]==false){visit[tmp.x][tmp.y][tmp.z]=true;q.push(tmp);}}if(head.y<r-1){tmp=head;tmp.y++;if(dungeon[tmp.x][tmp.y][tmp.z]!='#'&&visit[tmp.x][tmp.y][tmp.z]==false){visit[tmp.x][tmp.y][tmp.z]=true;q.push(tmp);}}if(head.z>0){//向C方向探索,一正一负 tmp=head;tmp.z--;if(dungeon[tmp.x][tmp.y][tmp.z]!='#'&&visit[tmp.x][tmp.y][tmp.z]==false){visit[tmp.x][tmp.y][tmp.z]=true;q.push(tmp);}}if(head.z<c-1){tmp=head;tmp.z++;if(dungeon[tmp.x][tmp.y][tmp.z]!='#'&&visit[tmp.x][tmp.y][tmp.z]==false){visit[tmp.x][tmp.y][tmp.z]=true;q.push(tmp);}}}if(found){printf("Escaped in %d minute(s).\n",t);}else{printf("Trapped!\n");}}int main(){//freopen("2.txt","r",stdin);while(scanf("%d %d %d",&l,&r,&c),l&&r&&c){memset(visit,false,sizeof(visit));memset(dungeon,0,sizeof(dungeon));int x,y,z;for(int i=0;i<l;i++){  //输入...一定要注意...在这卡了好久... for(int j=0;j<r;j++){cin>>dungeon[i][j];for(int k=0;k<c;k++){if(dungeon[i][j][k]=='S'){x=i;y=j;z=k;}}}}bfs(x,y,z);}return 0;}


0 0