poj2251Dungeon Master bfs
来源:互联网 发布:耳机频响测试软件 编辑:程序博客网 时间:2024/06/05 00:14
http://poj.org/problem?id=2251
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? InputThe 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 5S.....###..##..###.#############.####...###########.#######E1 3 3S###E####0 0 0Sample OutputEscaped in 11 minute(s).Trapped!
#include<iostream>#include<queue>#include<cstring>#include<cstdio>using namespace std;char map[50][50][50];int tx[6]={1,-1, 0, 0, 0, 0};int ty[6]={0, 0, 1,-1, 0, 0};int tz[6]={0, 0, 0, 0, 1,-1};int sx,sy,sz;int ex,ey,ez;int n,m,l;//n层,每层m行,l列struct node{ int x; int y; int z; int step;};int bfs(){ queue<node>q; node head,next; head.x=sx; head.y=sy; head.z=sz; head.step=0; map[head.x][head.y][head.z]='#';//表示这个点已经访问过了,以后不会再访问了 q.push(head); while(!q.empty()) { head=q.front(); q.pop(); if(head.x==ex&&head.y==ey&&head.z==ez) { return head.step; } for(int i=0;i<6;i++) { next.x=head.x+tx[i]; next.y=head.y+ty[i]; next.z=head.z+tz[i]; if(next.x>=0&&next.x<n&&next.y>=0&&next.y<m&&next.z>=0&&next.z<l&&map[next.x][next.y][next.z]!='#') { next.step=head.step+1; map[next.x][next.y][next.z]='#'; q.push(next); } } } return 0;}int main(){ int i,j,k; while(cin>>n>>m>>l&&n&&m&&l) { for(i=0;i<n;i++) { for(j=0;j<m;j++) { cin>>map[i][j]; for(k=0;k<l;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; } } } } i=bfs(); if(i==0) cout<<"Trapped!"<<endl; else cout<<"Escaped in "<<i<<" minute(s)."<<endl; } return 0;}
0 0
- poj2251Dungeon Master bfs
- poj2251Dungeon Master【BFS】
- POJ2251Dungeon Master(bfs)
- POJ2251Dungeon Master(三维图)(BFS)
- poj2251Dungeon Master(三维bfs)
- POJ2251Dungeon Master(图搜)(三维bfs)
- POJ2251Dungeon Master(3D bfs)
- POJ2251Dungeon Master
- POJ2251Dungeon Master(AC)
- 【简单搜索】POJ2251Dungeon Master
- POJ2251Dungeon Master&&nyoj523亡命逃窜
- poj2251 Dungeon Master (BFS)
- (BFS)2251 Dungeon Master
- Dungeon Master bfs
- 532 - Dungeon Master//bfs
- Dungeon Master(poj2251,bfs)
- Dungeon Master(bfs)
- Dungeon Master BFS
- JAVAWEB天气预报 中国气象网API(WebServices)
- Pycharm
- Android EditText监听只能输入2位小数
- iOS学习笔记之-C语言基础02(关键字、标识符、注释、数据类型)
- hdu 1754 I Hate It 线段树
- poj2251Dungeon Master bfs
- 四 XPath语言
- android项目调用c接口
- 阿里云yum不升级内核的解决方案
- POJ 2985 The k-th Largest Group [并差集+treap]
- JavaScript获取当前根目录
- android github 资源地址
- 实现@using{}代码块
- 电话通讯录用到的 Rui