poj 2251 (bfs)
来源:互联网 发布:阿里云绑定域名教程 编辑:程序博客网 时间:2024/05/21 10:43
Dungeon Master
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 16206 Accepted: 6280
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> #include<queue>#define maxm 35using namespace std;int n, m, t;char map[maxm][maxm][maxm];bool visit[maxm][maxm][maxm];int dir[6][3] ={0,-1,0,0,1,0,0,0,-1,0,0,1,1,0,0,-1,0,0};int bx, by, bz;int ex, ey, ez;struct point{int x, y, z, step;};bool check(int x, int y, int z){if(x>=1&&x<=n&&y>=1&&y<=m&&z>=1&&z<=t)if(map[z][x][y]!='#'&&!visit[z][x][y]){return true;}return false;}int bfs(){queue<point> q;point cur, next;cur.x = bx;cur.y = by;cur.z = bz;cur.step = 0;q.push(cur);visit[bz][bx][by]=1;while(!q.empty()){cur = q.front();q.pop();int x, y, z;for(int i = 0; i < 6; i++){z = cur.z + dir[i][0];x = cur.x+dir[i][1];y = cur.y+dir[i][2];if(check(x,y, z)){//满足进一步宽搜的条件 next.x = x;next.y = y;next.z = z;next.step = cur.step+1;if(x==ex&&y==ey&&z==ez){//达到目的地 return next.step;}visit[z][x][y] = 1;q.push(next);}}}return -1;}int main(){char c;while(cin>>t>>n>>m,t||n||m){for(int k = 1; k <= t; k++){for(int i = 1; i <= n; i++){for(int j = 1; j <= m; j++){cin>>map[k][i][j];if(map[k][i][j]=='S'){bx = i;by = j; bz = k;}else if(map[k][i][j]=='E'){ex = i; ey = j; ez = k;}}}getchar();}int ans = bfs();if(ans!=-1)printf("Escaped in %d minute(s).\n", ans);elseprintf("Trapped!\n");memset(visit, 0, sizeof(visit));}return 0;}
0 0
- POJ 2251 BFS
- poj 2251 三维BFS
- POJ 2251(BFS)
- POJ 2251 BFS 简单
- poj 2251(简单BFS)
- poj 2251 (bfs)
- poj 2251 bfs
- poj 2251(BFS)
- poj 2251 BFS
- POJ 2251(BFS)
- 【BFS】POJ 2251
- POJ 2251 简单BFS
- POJ-----2251BFS
- poj 2251 bfs基础
- POJ-2251三维bfs
- POJ 2251 Dungeon Master BFS
- POJ-2251 3维BFS..
- POJ 2251 找到出口 BFS
- C#窗体之整人小程序
- keepalived双机热备实现故障时发送邮件通知
- 我的Spring之旅(三):重构业务逻辑
- java集合(ArrayList、vector、HashMap、HashTable)源码剖析
- HDU 2081 手机短号 java
- poj 2251 (bfs)
- 在windows上搭建C语言开发环境——借助eclipse和MinGW
- Scanf函数的一些小细节
- poj 2524 Ubiquitous Religions
- Android开发之MediaPlayer音频视频播放
- hdoj 1754 I Hate It(线段树)
- C# System.DBNull string
- vc2013 atomic_int and atomic<int> bug
- kendoui仪表盘和柱状图 示例