POJ
来源:互联网 发布:网络银行有几家 编辑:程序博客网 时间:2024/06/06 13:19
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?
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.
Output
Each 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!
三维广搜,六个方向。用一个结构体存坐标和到该点的时间
#include <iostream>#include <cstdio>#include <cstring>#include <queue>using namespace std;char Map[31][31][31];int vis[31][31][31];int dx[6]={1,-1,0,0,0,0};int dy[6]={0,0,1,-1,0,0};int dz[6]={0,0,0,0,1,-1};int L,R,C,sx,sy,sz,ex,ey,ez;struct node{ int x,y,z,step;};int bfs(){ node a; queue<node> q; a.x=sx; a.y=sy; a.z=sz; a.step=0; vis[sx][sy][sz]=1; q.push(a); while(!q.empty()) { a=q.front(); q.pop(); if(a.x==ex&&a.y==ey&&a.z==ez) { return a.step; } for(int i=0;i<6;i++) { node b; b.x=a.x+dx[i]; b.y=a.y+dy[i]; b.z=a.z+dz[i]; if(b.x>=0&&b.y>=0&&b.z>=0&&b.x<L&&b.y<R&&b.z<C&&Map[b.x][b.y][b.z]!='#'&&!vis[b.x][b.y][b.z]) { vis[b.x][b.y][b.z]=1; b.step=a.step+1; q.push(b); } } } return 0;}int main(){ while(scanf("%d%d%d",&L,&R,&C)==3) { if(L==0&&R==0&&C==0)break; getchar(); for(int i=0;i<L;i++) { for(int j=0;j<R;j++) { scanf("%s",Map[i][j]); for(int k=0;k<C;k++) { if(Map[i][j][k]=='S')//找到开始位置 { sx=i,sy=j,sz=k; } if(Map[i][j][k]=='E')//结束位置 { ex=i,ey=j,ez=k; } } } } memset(vis,0,sizeof(vis)); int min1; min1=bfs(); if(min1) { printf("Escaped in %d minute(s).\n",min1); } else { printf("Trapped!\n"); } } return 0;}
- POJ
- poj
- POJ
- POJ
- poj
- poj
- POJ
- POJ
- poj
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- 华为上机笔试题 最高分是多少
- maven release版本不更新原因分析
- 彻底理解Redis中有效期的问题
- block的使用和注意事项
- JAVA学习笔记
- POJ
- 最大公约数的相关算法
- chmod权限
- 通用方法 解决/usr/lib64/libstdc++.so.6: version `CXXABI_1.3.8' not found的问题
- Python高级编程
- iOS开发-Protocol协议及委托代理(Delegate)传值
- appium使用过程中遇到的坑
- oracle&&linux技能点记录
- ionic app实现扫描二维码添加好友