您的位置:首页 > 其它

poj_2251 Dungeon Master(bfs)

2016-11-06 19:52 453 查看
Dungeon Master
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 28091 Accepted: 10968
DescriptionYou 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 andthe maze is surrounded by solid rock on all sides. Is an escape possible? If yes, how long will it take? InputThe 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 theexit by the letter 'E'. There's a single blank line after each level. Input is terminated by three zeroes for L, R and C.OutputEach maze generates one line of output. If it is possible to reach the exit, print a line of the form 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!
dfs的话没想出怎么去剪枝,用三维bfs则比较简单。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <stack>#include <bitset>#include <queue>#include <set>#include <map>#include <string>#include <algorithm>#define FOP freopen("data.txt","r",stdin)#define inf 0x3f3f3f3f#define maxn 1000010#define mod 1000000007#define PI acos(-1.0)#define LL long longusing namespace std;struct Point{int x, y, z;};char ma[32][32][32];bool vis[32][32][32];int dx[] = {-1, 1, 0, 0, 0, 0};int dy[] = {0, 0, -1, 1, 0, 0};int dz[] = {0, 0, 0, 0, -1, 1};int sx, sy, sz;int ex, ey, ez;int L, R, C;int ans;int path[32][32][32];void bfs(){memset(path, 0, sizeof(path));queue<Point> Q;Point t;t.x = sx, t.y = sy, t.z = sz;Q.push(t);vis[t.x][t.y][t.z] = 1;while(!Q.empty()){t = Q.front();Q.pop();if(t.x == ex && t.y == ey && t.z == ez) break;for(int i = 0 ; i < 6; i++){int xx = t.x + dx[i], yy = t.y + dy[i], zz = t.z + dz[i];if(xx < 1 || xx > L || yy < 1 || yy > R || zz < 1 || zz > C || ma[xx][yy][zz] == '#') continue;if(!vis[xx][yy][zz]){vis[xx][yy][zz] = 1;Point temp;temp.x = xx, temp.y = yy, temp.z = zz;Q.push(temp);path[xx][yy][zz] = path[t.x][t.y][t.z] + 1;}}}}int main(){while(scanf("%d%d%d", &L, &R, &C) && (L != 0 || R != 0 || C != 0)){ans = inf;memset(vis, 0, sizeof(vis));for(int i = 1; i <= L; i++){for(int j = 1; j <= R; j++){scanf("%s", ma[i][j] + 1);for(int k = 1; k <= C; k++){if(ma[i][j][k] == 'S') sx = i, sy = j, sz = k;else if(ma[i][j][k] == 'E') ex = i, ey = j, ez = k;}}}bfs();if(!path[ex][ey][ez]) printf("Trapped!\n");else printf("Escaped in %d minute(s).\n", path[ex][ey][ez]);}return 0;}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: