POJ-2251 Dungeon Master(BFS)
来源:互联网 发布:网络舆论对社会的影响 编辑:程序博客网 时间:2024/06/06 21:57
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
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!
3 4 5S.....###..##..###.#############.####...###########.#######E1 3 3S###E####0 0 0
Escaped in 11 minute(s).Trapped!
问题:三维迷宫找最短出口...直接BFS...BFS部分是没问题...但是一开始的输入有些问题导致没有AC...这种题一定注意输入格式...
#include<cstdio>#include<queue>#include<cstring>#include<iostream>using namespace std;const int maxx=35;struct point{int x;int y;int z;int step;};bool visit[maxx][maxx][maxx]; //标记是否访问过的数组 char dungeon[maxx][maxx][maxx];//储存地图的数组 int l,r,c;//各个边界 int bfs(int x,int y,int z){ point start; //开始点 queue<point> q;while(!q.empty()) q.pop(); //初始化队列 start.x=x;start.y=y;start.z=z;start.step=0;q.push(start);//入队 int t;bool found=false;while(!q.empty()){point head=q.front(); //head是当前节点 q.pop();if(dungeon[head.x][head.y][head.z]=='E'){ //如果找到路了 found=true;t=head.step; break;}head.step++;point tmp;if(head.x>0){ //向L方向探索,一正一负 tmp=head;tmp.x--;if(dungeon[tmp.x][tmp.y][tmp.z]!='#'&&visit[tmp.x][tmp.y][tmp.z]==false){visit[tmp.x][tmp.y][tmp.z]=true;q.push(tmp);}}if(head.x<l-1){tmp=head;tmp.x++;if(dungeon[tmp.x][tmp.y][tmp.z]!='#'&&visit[tmp.x][tmp.y][tmp.z]==false){visit[tmp.x][tmp.y][tmp.z]=true;q.push(tmp);}}if(head.y>0){ //向R方向探索,一正一负 tmp=head;tmp.y--;if(dungeon[tmp.x][tmp.y][tmp.z]!='#'&&visit[tmp.x][tmp.y][tmp.z]==false){visit[tmp.x][tmp.y][tmp.z]=true;q.push(tmp);}}if(head.y<r-1){tmp=head;tmp.y++;if(dungeon[tmp.x][tmp.y][tmp.z]!='#'&&visit[tmp.x][tmp.y][tmp.z]==false){visit[tmp.x][tmp.y][tmp.z]=true;q.push(tmp);}}if(head.z>0){//向C方向探索,一正一负 tmp=head;tmp.z--;if(dungeon[tmp.x][tmp.y][tmp.z]!='#'&&visit[tmp.x][tmp.y][tmp.z]==false){visit[tmp.x][tmp.y][tmp.z]=true;q.push(tmp);}}if(head.z<c-1){tmp=head;tmp.z++;if(dungeon[tmp.x][tmp.y][tmp.z]!='#'&&visit[tmp.x][tmp.y][tmp.z]==false){visit[tmp.x][tmp.y][tmp.z]=true;q.push(tmp);}}}if(found){printf("Escaped in %d minute(s).\n",t);}else{printf("Trapped!\n");}}int main(){//freopen("2.txt","r",stdin);while(scanf("%d %d %d",&l,&r,&c),l&&r&&c){memset(visit,false,sizeof(visit));memset(dungeon,0,sizeof(dungeon));int x,y,z;for(int i=0;i<l;i++){ //输入...一定要注意...在这卡了好久... for(int j=0;j<r;j++){cin>>dungeon[i][j];for(int k=0;k<c;k++){if(dungeon[i][j][k]=='S'){x=i;y=j;z=k;}}}}bfs(x,y,z);}return 0;}
0 0
- POJ 2251 Dungeon Master BFS
- POJ 2251 Dungeon Master (BFS)
- POJ 2251 Dungeon Master(BFS)
- POJ 2251 Dungeon Master【BFS】
- POJ 2251 Dungeon Master(BFS)
- poj 2251 Dungeon Master(bfs)
- POJ 2251--Dungeon Master【BFS】
- poj 2251 Dungeon Master 【BFS】
- POJ 2251:Dungeon Master【bfs】
- poj-2251-Dungeon Master-BFS
- poj-2251 Dungeon Master BFS
- poj 2251 Dungeon Master(BFS)
- POJ 2251 Dungeon Master (BFS)
- POJ 2251 Dungeon Master(bfs)
- POJ - 2251 Dungeon Master(BFS)
- poj 2251 Dungeon Master(bfs)
- poj-2251-Dungeon Master(bfs)
- POJ 2251Dungeon Master(BFS)
- *Linux学习笔记
- Python学习(一)
- Linux中的变量【2】环境变量
- CSDN博客的RSS订阅---使用foxmail订阅
- python3.4之决策树
- POJ-2251 Dungeon Master(BFS)
- js栏目根据地址栏修改背景添加class
- Java中日期的表示
- Spark 2.0 streaming中 InputDStream 和 ReceiverInputDStream 的区别
- MPAndroidChart代码套路记录
- VS2013 禁用与卸载插件
- Word 2010 打开文档,鼠标无法选中或者点击
- [kuangbin带你飞]专题十 匹配问题 F - Rain on your Parade(二分图匹配)(HKmatch)
- HTML笔记