本文主要是介绍poj 2251Dungeon Master(BFS, DFS亦可),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
今天,一看题,我就想到用BFS,毕竟BFS是具有贪心性质的,可以用来求最小短的……给我的感觉就是简单,但是很烦,因为它是三维的,和poj3728 catch that cow差不多,需要一个数组来存储到达那一点的最小的时间;需要注意的是,输出‘.’;
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 9761 | Accepted: 3777 |
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.... .###. .##.. ###.###### ##### ##.## ##...##### ##### #.### ####E1 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]);elseprintf("Trapped!\n");}
}
这篇关于poj 2251Dungeon Master(BFS, DFS亦可)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!