POJ 2251 Dungeon Master
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 43874 | Accepted: 16554 |
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?
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.
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
where x is replaced by the shortest time it takes to escape.
If it is not possible to escape, print the line
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!
Source
思路:宽搜。
#include<queue> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; struct nond{ int x,y,z,tot; }; queue<nond>que; int map[31][31][31]; int sx,sy,sz,tx,ty,tz; int dx[6]={1,-1,0,0,0,0}; int dy[6]={0,0,1,-1,0,0}; int dz[6]={0,0,0,0,1,-1}; int n,m,k,ans=0x7f7f7f7f; void bfs(int x,int y,int z){ nond tmp;tmp.x=x;tmp.y=y;tmp.z=z;tmp.tot=0; que.push(tmp); while(!que.empty()){ nond now=que.front(); que.pop(); for(int i=0;i<6;i++){ int cx=now.x+dx[i]; int cy=now.y+dy[i]; int cz=now.z+dz[i]; int ctot=now.tot+1; if(cx==tx&&cy==ty&&cz==tz){ ans=min(ctot,ans); } if(cx>=1&&cx<=n&&cy>=1&&cy<=m&&cz>=1&&cz<=k&&!map[cx][cy][cz]){ map[cx][cy][cz]=1; nond c;c.x=cx;c.y=cy;c.z=cz;c.tot=ctot; que.push(c); } } } } int main(){ while(scanf("%d%d%d",&k,&n,&m)&&n!=0&&m!=0&&k!=0){ for(int c=1;c<=k;c++) for(int i=1;i<=n;i++) for(int j=1;j<=m;j++){ char x;cin>>x; if(x=='#') map[i][j][c]=1; else map[i][j][c]=0; if(x=='S'){ sx=i;sy=j;sz=c; } if(x=='E'){ tx=i;ty=j;tz=c; } } map[sx][sy][sz]=1; bfs(sx,sy,sz); if(ans!=0x7f7f7f7f) printf("Escaped in %d minute(s).\n",ans); else printf("Trapped!\n");ans=0x7f7f7f7f; } } /* 3 4 5 S.... .###. .##.. ###.# ##### ##### ##.## ##... ##### ##### #.### ####E 1 3 3 S## #E# ### 0 0 0 */
细雨斜风作晓寒。淡烟疏柳媚晴滩。入淮清洛渐漫漫。
雪沫乳花浮午盏,蓼茸蒿笋试春盘。人间有味是清欢。