今天,一看题,我就想到用BFS,毕竟BFS是具有贪心性质的,可以用来求最小短的……给我的感觉就是简单,但是很烦,因为它是三维的,和poj3728 catch that cow差不多,需要一个数组来存储到达那一点的最小的时间;需要注意的是,输出‘.’;
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 9761 | Accepted: 3777 |
Description
Input
Output
Escaped in x minute(s).
Trapped!
Sample Input
3 4 5 S.... .###. .##.. ###.# ##### ##### ##.## ##... ##### ##### #.### ####E 1 3 3 S## #E# ### 0 0 0
Sample Output
Escaped in 11 minute(s). Trapped!
#include<stdio.h> #include<string.h> struct Node { int x; int y; int z; }node[170000]; int l, r, c; char map[31][31][31]; int visited[31][31][31]; int step[31][31][31]; int x1, y1, z1; int x2, y2, z2; int a[6][3]={{-1,0,0},{1,0,0},{0,-1,0},{0,1,0},{0,0,-1},{0,0,1}}; int BFS(int x, int y, int z) { int i; int X, Y, Z; int front=0, rear=1; node[front].x=x; node[front].y=y; node[front].z=z; visited[z][y][x]=1; while(front != rear) { for(i=0; i<6; i++) { X=node[front].x+a[i][0]; Y=node[front].y+a[i][1]; Z=node[front].z+a[i][2]; if(X>=0&&X<c && Y>=0&&Y<r && Z>=0&&Z<l && !visited[Z][Y][X] && map[Z][Y][X]!='#') { step[Z][Y][X]=step[node[front].z][node[front].y][node[front].x]+1; if( X==x2 && Y==y2 && Z==z2) { return 1; } node[rear].x=X; node[rear].y=Y; node[rear].z=Z; visited[Z][Y][X]=1; rear++; } } front++; } return 0; } int main() { int i, j, k; while(scanf("%d %d %d", &l, &r, &c) && l) { memset(visited, 0, sizeof(visited) ); memset(step, 0, sizeof(step) ); for(i=0; i<l; i++) { for(j=0; j<r; j++) { getchar(); for(k=0; k<c; k++) { scanf("%c", &map[i][j][k]); if(map[i][j][k]=='S') { z1=i;y1=j;x1=k; } else if(map[i][j][k]=='E') { z2=i;y2=j;x2=k; } else if(map[i][j][k]=='#') visited[i][j][k]=1; } } getchar(); } if( BFS(x1, y1, z1) ) printf("Escaped in %d minute(s).\n", step[z2][y2][x2]); else printf("Trapped!\n"); } }