标签:
Escaped in x minute(s).
Trapped!
3 4 5 S.... .###. .##.. ###.# ##### ##### ##.## ##... ##### ##### #.### ####E 1 3 3 S## #E# ### 0 0 0
Escaped in 11 minute(s). Trapped!
开三维数组用BFS求。
1 #include<cstdio> 2 #include<queue> 3 using namespace std; 4 int dx[6]={-1,1,0,0,0,0}; 5 int dy[6]={0,0,-1,1,0,0}; 6 int dz[6]={0,0,0,0,1,-1}; 7 char str[100][100][100]; 8 int l,r,c,i,j,k,ans,bx,bz,by; 9 struct stu 10 { 11 int x,y,z,step; 12 }; 13 bool f(stu st) 14 { 15 if(st.x<0 || st.z<0 || st.y<0 || st.x>=r || st.y>=c || st.z>=l || str[st.z][st.x][st.y] ==‘#‘) 16 { 17 return false; 18 } 19 return true; 20 } 21 int bfs(int bz,int bx,int by) 22 { 23 str[bz][bx][by]=‘#‘; 24 int nx,ny,time,zz,xx,yy; 25 queue<stu> que; 26 stu st,next; 27 st.x=bx; 28 st.y=by; 29 st.z=bz; 30 st.step=0; 31 que.push(st); 32 33 while(!que.empty()) 34 { 35 36 st=que.front(); 37 que.pop(); 38 39 xx=st.x; 40 yy=st.y; 41 zz=st.z; 42 time=st.step; 43 for(i = 0 ;i < 6 ; i++) 44 { 45 st.x=xx+dx[i]; 46 st.y=yy+dy[i]; 47 st.z=zz+dz[i]; 48 if(!f(st)) 49 { 50 continue; 51 } 52 if(str[st.z][st.x][st.y] == ‘E‘) 53 { 54 return time+1; 55 } 56 57 st.step=time+1; 58 que.push(st); 59 str[st.z][st.x][st.y]=‘#‘; 60 } 61 } 62 return 0; 63 } 64 int main() 65 { 66 while(scanf("%d %d %d",&l,&r,&c)&&l&&r&&c) 67 { 68 for(i = 0 ; i < l ; i++) 69 { 70 for(j = 0 ; j < r ;j++) 71 { 72 scanf("%s",str[i][j]); 73 for(k = 0 ; k < c ; k++) 74 { 75 if(str[i][j][k] == ‘S‘) 76 { 77 bz=i; 78 bx=j; 79 by=k; 80 } 81 } 82 } 83 } 84 ans=bfs(bz,bx,by); 85 if(ans) 86 printf("Escaped in %d minute(s).\n",ans); 87 else 88 printf("Trapped!\n"); 89 90 } 91 }
POJ 2251-Dungeon Master (三维空间求最短路径)
标签:
原文地址:http://www.cnblogs.com/yexiaozi/p/5720197.html