poj 2251 逃出牢笼,简单的bfs
来源:互联网 发布:5号电池多少毫安 知乎 编辑:程序博客网 时间:2024/04/29 20:26
Dungeon Master
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 18031 Accepted: 6994
Description
You 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 and the maze is surrounded by solid rock on all sides.
Is an escape possible? If yes, how long will it take?
Is an escape possible? If yes, how long will it take?
Input
The 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 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.
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
Each maze generates one line of output. If it is possible to reach the exit, print a line of the form
where x is replaced by the shortest time it takes to escape.
If it is not possible to escape, print the line
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 5S.....###..##..###.#############.####...###########.#######E1 3 3S###E####0 0 0
Sample Output
Escaped in 11 minute(s).Trapped!
Source
Ulm Local 1997
题意:很常规的三维空间的宽搜,就不多讲了
代码:
#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int ms = 1e6;struct node{ int l,r,c; int steps;};node queue[ms];int front,rear,visit[40][40][40],map[40][40][40];int L,R,C;int move[6][3]={{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};void Init(){ front = rear = 0; return;}bool IsEmpty(){ if(front == rear) return true; return false;}void Push(node m){ if(visit[m.l][m.r][m.c]) return; visit[m.l][m.r][m.c] = 1; if(rear == ms) rear = 0; queue[rear++] = m; return;}void Pop(){ if(front == rear) return; front++; return;}bool over(int l,int r,int c){ if(l<0 || l>= L || r<0 || r>=R || c<0 || c>=C) return true; return false;}int main(){ char tmp; node start,top,newn; int el,er,ec; int ans; while(1){ scanf("%d %d %d",&L,&R,&C); if(!L) return 0; Init(); memset(visit,0,sizeof(visit)); ans = -1; for(int i = 0; i < L; i++){ for(int j = 0; j < R;j++){ for(int k = 0; k < C;k++){ scanf(" %c",&tmp); if(tmp == 'S'){ start.l = i; start.r = j; start.c = k; start.steps = 0; Push(start); } if(tmp == 'E'){ el = i,er = j,ec = k; } if(tmp == '#') map[i][j][k] = 1; else map[i][j][k] = 0; } } } while(!IsEmpty()){ top = queue[front]; if(top.l == el && top.r == er && top.c == ec){ ans = top.steps; printf("Escaped in %d minute(s).\n",ans); break; } Pop(); newn.steps = top.steps+1; for(int i = 0; i < 6; i++){ newn.l = top.l+move[i][0]; newn.r = top.r+move[i][1]; newn.c = top.c+move[i][2]; if(!over(newn.l,newn.r,newn.c)) if(!map[newn.l][newn.r][newn.c]) Push(newn); } } if(ans == -1) printf("Trapped!\n"); }}
0 0
- poj 2251 逃出牢笼,简单的bfs
- 逃出你的肖申克(六):看不见的牢笼(下):柏拉图的洞穴
- 逃出你的肖申克(五):看不见的牢笼(上)
- 逃出你的肖申克(五):看不见的牢笼(上)
- 逃出你的肖申克(五):看不见的牢笼(上)
- 逃出你的肖申克(五):看不见的牢笼(上)
- POJ 2251 BFS 简单
- poj 2251(简单BFS)
- POJ 2251 简单BFS
- poj 2251 简单搜索bfs
- POJ 2251 BFS(简单)
- POJ 2251 Dungeon Master 简单的三维bfs
- POJ-2251 Dungeon Master(简单的三维bfs)
- poj 1562 简单的BFS搜索
- BFS专攻:POJ 3278 (三个方向的简单BFS)
- POJ - 2251 Dungeon Master (简单BFS)
- POJ - 2251 - Dungeon Master (简单BFS)
- POJ 2251 (BFS 简单三维迷宫)
- 百度也开始下作了
- (1176)HDU-数字三角形
- 社区挖掘中模块度Q的计算
- java字符串比较 null与""区别
- float型变量format转换中精确度设置时将int常数赋值给float型变量报错
- poj 2251 逃出牢笼,简单的bfs
- ZOJ 2850 Beautidul Meadow(水题)
- Oracle中使用MD5加密
- C++11 中STL库中新增内容
- uva11400
- DIV为空时仍然保持占位
- Windows 10技术预览版系统激活
- Java练习题-输入一个字符串,输出该字符串中字符的所有组合
- 玩转DWZ (一)---项目中怎么使用dwz