[POJ] 2251 Dungeon Master
来源:互联网 发布:linux系统哪个版本好 编辑:程序博客网 时间:2024/06/05 08:33
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total
Submissions: 38847 Accepted: 14802 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 and the maze is
surrounded by solid rock on all sides.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. 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 Input3 4 5 S…. .###. .##.. ###.#
##### ##### ##.## ##…
##### ##### #.### ####E
1 3 3 S## #E# ###
0 0 0 Sample Output
Escaped in 11 minute(s). Trapped! Source
Ulm Local 1997
注意几点:
搜索是从S开始而不是1,1,1
多组输入要memset一下
因为方向问题调了好久。。x,y,z一视同仁就挺好
确实加深了对bfs的认识
/* Writer:GhostCai && His Yellow Duck */#include <cstdio>#include <iostream>#include <cstring>#include <queue>#define MAXN 31using namespace std;bool flag;struct point { int x, y, z;} node, r;char a[MAXN][MAXN][MAXN];bool vis[MAXN][MAXN][MAXN];point pre[MAXN][MAXN][MAXN];int h, m, n;int mx[6] = { 1, 0, -1, 0, 0, 0 };int my[6] = { 0, 1, 0, -1, 0, 0 };int mz[6] = { 0, 0, 0, 0, 1, -1 };void print( point x ){ flag = 1; int cnt = 0; point u = pre[x.x][x.y][x.z]; while ( u.x != 0 && u.y != 0 && u.z != 0 ) { u = pre[u.x][u.y][u.z]; cnt++; } printf( "Escaped in %d minute(s).\n", cnt ); /* \n! */ return;}void bfs( int x, int y, int z ){ node.x = 0; node.y = 0; node.z = 0; vis[x][y][z] = 1; pre[x][y][z] = node; node.x = x; node.y = y; node.z = z; queue<point> Q; Q.push( node ); while ( !Q.empty() && !flag ) { r = Q.front(); Q.pop(); int i, j; int nx, ny, nz; for ( i = 0; i <= 5; i++ ) { nx = r.x + mx[i]; ny = r.y + my[i]; nz = r.z + mz[i]; if ( nx < 1 || nx > m || ny < 1 || ny > n || nz < 1 || nz > h ) continue; if ( a[nx][ny][nz] == '#' || vis[nx][ny][nz] ) continue;/* cout<<nx<<" "<<ny<<" "<<nz<<endl; */ node.x = nx; node.y = ny; node.z = nz; Q.push( node ); pre[nx][ny][nz] = r; vis[nx][ny][nz] = 1; if ( a[nx][ny][nz] == 'E' ) { /* cout<<"SUCCESS"; */ print( node ); } } }}int main(){ while ( cin >> h >> m >> n ) { int sx, sy, sz; if ( h == 0 && m == 0 && n == 0 ) return(0); flag = 0; memset( a, 0, sizeof(a) ); memset( vis, 0, sizeof(vis) ); memset( pre, 0, sizeof(pre) ); int i, j, k; for ( i = 1; i <= h; i++ ) { for ( j = 1; j <= m; j++ ) { for ( k = 1; k <= n; k++ ) { cin >> a[j][k][i]; /* -------------- */ if ( a[j][k][i] == 'S' ) { sx = j; sy = k; sz = i; } } } } bfs( sx, sy, sz ); if ( !flag ) cout << "Trapped!\n"; }}
- poj 2251 Dungeon Master
- poj 2251 Dungeon Master
- poj 2251 Dungeon Master
- poj 2251 Dungeon Master
- Poj 2251 Dungeon Master
- poj 2251 Dungeon Master
- poj 2251 Dungeon Master
- POJ 2251 Dungeon Master
- POJ-2251-Dungeon Master
- POJ 2251Dungeon Master
- poj 2251 Dungeon Master
- poj 2251 Dungeon Master
- poj 2251 Dungeon Master
- POJ 2251 - Dungeon Master
- POJ 2251 Dungeon Master
- POJ 2251 Dungeon Master
- POJ-2251-Dungeon Master
- poj 2251 Dungeon Master
- 使用循环神经网络训练情感分析
- 经典笔试题:指针详解
- 在Android的webview中定制js的alert,confirm和prompt对话框的方法
- 欢迎使用CSDN-markdown编辑器
- simplest_ffmpeg_streamer加注释版
- [POJ] 2251 Dungeon Master
- 2-2 hanxin
- WebSocket总结
- Java和C++ Protect安全性
- [2017.11.18]作业12
- Mars老师Android视频教程笔记,欢迎指正
- jieba库最常用操作
- Linux pwd命令
- 欢迎使用CSDN-markdown编辑器