POJ2251Dungeon Master(bfs)
来源:互联网 发布:个人免费域名注册 编辑:程序博客网 时间:2024/05/17 05:50
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?
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.
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
bfs找路径,就是多了一个上楼下楼的操作,好水的题啊都不好意思写题解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!
AC代码
#include<iostream>#include<cstdio>#include<cmath>#include<queue>#include<algorithm>#include<cstring>using namespace std;int L,R,C;char m[35][35][35];int vis[35][35][35];int to[4][2]={0,1,1,0,0,-1,-1,0};struct node{ int x,y,step,lev;}sta,End;int ans;void bfs(node sta){ queue<node>q; while(!q.empty())q.pop(); sta.step=0; q.push(sta); vis[sta.lev][sta.y][sta.x]=1; node temp,temp1,temp2,temp3; while(!q.empty()&&ans==-1) { temp=q.front();q.pop(); if(temp.lev==End.lev&&temp.x==End.x&&temp.y==End.y){ans=temp.step;break;} for(int i=0;i<4;i++) { temp1.x=temp.x+to[i][0];temp1.y=temp.y+to[i][1];temp1.lev=temp.lev;temp1.step=temp.step+1; if(!vis[temp1.lev][temp1.y][temp1.x]) { vis[temp1.lev][temp1.y][temp1.x]=1; if(m[temp1.lev][temp1.y][temp1.x]=='.'||m[temp1.lev][temp1.y][temp1.x]=='E') q.push(temp1); } temp2.x=temp.x;temp2.y=temp.y;temp2.lev=temp.lev+1;temp2.step=temp.step+1; if(!vis[temp2.lev][temp2.y][temp2.x]) { vis[temp2.lev][temp2.y][temp2.x]=1; if(m[temp2.lev][temp2.y][temp2.x]=='.'||m[temp2.lev][temp2.y][temp2.x]=='E') q.push(temp2); } temp3.x=temp.x;temp3.y=temp.y;temp3.lev=temp.lev-1;temp3.step=temp.step+1; if(!vis[temp3.lev][temp3.y][temp3.x]) { vis[temp3.lev][temp3.y][temp3.x]=1; if(m[temp3.lev][temp3.y][temp3.x]=='.'||m[temp3.lev][temp3.y][temp3.x]=='E') q.push(temp3); } } }}void in(){ for(int i=1;i<=L;i++) { for(int j=1;j<=R;j++) { for(int k=1;k<=C;k++){ cin>>m[i][j][k]; if(m[i][j][k]=='S') { sta.x=k,sta.y=j; sta.lev=i; } else if(m[i][j][k]=='E') { End.x=k,End.y=j; End.lev=i; } } } }}int main(){ while(~scanf("%d%d%d",&L,&R,&C)) { if(L+R+C==0)break; memset(vis,0,sizeof vis); ans=-1; in(); bfs(sta); if(ans==-1) printf("Trapped!\n"); else printf("Escaped in %d minute(s).\n",ans); } return 0;}
阅读全文
0 0
- POJ2251Dungeon Master(bfs)
- poj2251Dungeon Master(三维bfs)
- POJ2251Dungeon Master(图搜)(三维bfs)
- POJ2251Dungeon Master(3D bfs)
- poj2251Dungeon Master bfs
- poj2251Dungeon Master【BFS】
- POJ2251Dungeon Master(三维图)(BFS)
- POJ2251Dungeon Master
- POJ2251Dungeon Master(AC)
- 【简单搜索】POJ2251Dungeon Master
- POJ2251Dungeon Master&&nyoj523亡命逃窜
- Dungeon Master(bfs)
- Dungeon Master(BFS)
- POJ_2251Dungeon Master(bfs)
- (bfs)Dungeon Master
- Dungeon Master (BFS)
- Dungeon Master(BFS)
- poj Dungeon Master(Bfs)
- java中父类与子类的转换问题
- 自定义类型:结构体的内存对齐和传参问题
- Inception结构
- 浅谈UML学习笔记之类图
- PADS VX2.2在WIN7系统下菜单显示不完整的解决方法
- POJ2251Dungeon Master(bfs)
- 后面需要看的视频
- wpf 绑定数据无法更新ui控件可能存在的问题
- python量化分析系列之---python分别使用多线程和多进程获取所有股票实时数据
- 将Html中表格数据导出为Excel
- CentOS下Mysql 改变数据存储路径
- 欢迎使用CSDN-markdown编辑器
- 【VC++】CString与std::string互转
- 结尾0的个数