Description
Is an escape possible? If yes, how long will it take?
Input
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
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!
广搜题目,题目要求从S——>E,只能走'.',或者如果可以从一个位置跳跃到一个位置,两个位置都要是'.',步数加1,统计总步数,如果不存在这样的路径,则输出Trapped!
由题目可知,要定义一个三维数组,代码如下:
#include<stdio.h> #include<iostream> #include<algorithm> #include<string.h> #include<queue> using namespace std; struct data { int x,y,z; int steps; } st,step; int s[6][3]= {{0,-1,0},{0,1,0},{0,0,1},{0,0,-1},{1,0,0},{-1,0,0}}; char aa[111][111][111]; int st1,st2,st3,end1,end2,end3; int a,b,c,bb[111][111][111]; int bfs() { memset(bb,0,sizeof(bb)); queue<data>que; st.x=st1; st.y=st2; st.z=st3; st.steps=0; que.push(st); bb[st1][st2][st3]=1; while(!que.empty()) { st=que.front(); que.pop(); for(int i=0; i<6; i++) { int x=st.x+s[i][0]; int y=st.y+s[i][1]; int z=st.z+s[i][2]; if(x==end1&&y==end2&&z==end3) return st.steps+1; if(x>=0&&x<a&&y>=0&&y<b&&z>=0&&z<c&&!bb[x][y][z]&&aa[x][y][z]=='.') { bb[x][y][z]=1; step.x=x; step.y=y; step.z=z; step.steps=st.steps+1; que.push(step); } } } return -1; } int main() { while(scanf("%d%d%d",&a,&b,&c)) { if(a==0&&b==0&&c==0) break; for(int i=0; i<a; i++) for(int j=0; j<b; j++) for(int k=0; k<c; k++) { cin>>aa[i][j][k]; if(aa[i][j][k]=='S') { st1=i; st2=j; st3=k; } if(aa[i][j][k]=='E') { end1=i; end2=j; end3=k; } } int t=bfs(); if(t==-1) printf("Trapped!\n"); else printf("Escaped in %d minute(s).\n",t); } }