Dungeon Master
2016-01-03 16:41
302 查看
算法:广搜(BFS);
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?
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!
三维的向六个方向搜:a[6][3]={-1,0,0,1,0,0,0,-1,0,0,1,0,0,0,-1,0,0,1};
代码:
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?
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!
三维的向六个方向搜:a[6][3]={-1,0,0,1,0,0,0,-1,0,0,1,0,0,0,-1,0,0,1};
代码:
#include <iostream> #include <iomanip> #include <string> #include <algorithm> #include <queue> #include <cstring> using namespace std; int n,m,k,q1,p1,s1,s2,p2,q2,t,b[35][35][35]; char map[35][35][35]; int a[6][3]={-1,0,0,1,0,0,0,-1,0,0,1,0,0,0,-1,0,0,1}; struct cm { int x,y,z,step; }; void BFS(int dx,int dy,int dz) { memset(b,0,sizeof(b)); queue<cm>que ; cm cur,loer; cur.x=dx; cur.y=dy; cur.z=dz; cur.step=0; b[dx][dy][dz]=1; que.push(cur); while(que.size()) { loer=que.front(); que.pop(); if(loer.x==s2&&loer.y==p2&&loer.z==q2) { t=1; cout<<"Escaped in "<<loer.step<<" minute(s)."<<endl; break; } for(int i=0;i<6;i++) { int nx=loer.x+a[i][0]; int ny=loer.y+a[i][1]; int nz=loer.z+a[i][2]; if(nx>=0&&nx<n&&ny>=0&&ny<m&&nz>=0&&nz<k&&map[nx][ny][nz]=='.'&&!b[nx][ny][nz]) { cur.x=nx; cur.y=ny; cur.z=nz; cur.step=loer.step+1; b[nx][ny][nz]=1; que.push(cur); } } } } int main() { int i,j,s; while(cin>>n>>m>>k&&n&&m&&k) { for(i=0;i<n;i++) { for(j=0;j<m;j++) {cin>>map[i][j]; for(t=0;t<k;t++) { if(map[i][j][t]=='S') { s1=i;p1=j;q1=t; map[i][j][t]='.'; } else if(map[i][j][t]=='E') { s2=i;p2=j;q2=t; map[i][j][t]='.'; } } } } t=0; BFS(s1,p1,q1); if(t==0) cout<<"Trapped!"<<endl; } }
相关文章推荐
- Android TCP 简单实现
- 【Windows Form 实战】学生成绩管理系统(四) 登录模块设计
- Hibernate之缓存,N+1
- Dungeon Master
- HTML使用<span>和<div>布局中遇到的问题
- SimpleAdapter gridView
- eval在MATLAB和Shell中的作用
- java解析和生成GSON串
- C++中的类模板详细讲述
- tbytes 转 十六进制 string
- Java学习----this和super(在继承中)
- Spark学习笔记
- Linux基本命令
- 在ORCAD里面如何给元器件自动编号
- bzoj 4373: 算术天才⑨与等差数列
- 三种方法实现Linux系统调用
- UML遗留问题总结
- 机器学习中的数学(1)-回归(regression)、梯度下降(gradient descent)
- 我为什么要写博客
- Swift控制手电筒操作(iOS)