POJ-2251-Dungeon Master(BFS 优先队列)
2016-06-06 15:52
459 查看
B - Dungeon Master
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Submit
Status
Practice
POJ 2251
Appoint description:
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!
走过的路标记为#,时间少的先出队,自定义六个搜索方向,直至搜到出口或者队列为空
代码
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Submit
Status
Practice
POJ 2251
Appoint description:
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!
走过的路标记为#,时间少的先出队,自定义六个搜索方向,直至搜到出口或者队列为空
代码
#include<stdio.h> #include<algorithm> #include<iostream> #include<string.h> #include<queue> using namespace std; const int maxn=33; char map[maxn][maxn][maxn]; int dis[6][3]= {{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}}; //搜索方向 int star_x; int star_y; int star_z;//起始点坐标 int end_x; int end_y; int end_z; int L,R,C; struct node { int x; int y; int z; int time; friend bool operator <(node n1,node n2) { return n1.time>n2.time; } }; bool Judge(node end) { if(end.x>0&&end.x<=L&&end.y>0&&end.y<=R&&end.z>0&&end.z<=C&&map[end.x][end.y][end.z]!='#')//搜索坐标未越界,且可被搜索 return true; return false; } void BFS() { node star; star.time=0; star.x=star_x; star.y=star_y; star.z=star_z; map[star.x][star.y][star.z]='#'; priority_queue<node>q; q.push(star); while(!q.empty()) { star=q.top(); q.pop(); if(star.x==end_x&&star.y==end_y&&star.z==end_z) { printf("Escaped in %d minute(s).\n",star.time); return; } for(int i=0; i<6; i++) //六个搜索方向 { node end; end.x=star.x+dis[i][0]; end.y=star.y+dis[i][1]; end.z=star.z+dis[i][2]; if(Judge(end)) { // printf("*****\n"); // printf("%d %d %d\n",end.x,end.y,end.z);//打印搜索路径 end.time=star.time+1; map[end.x][end.y][end.z]='#'; q.push(end); } } } printf("Trapped!\n"); } int main() { while(~scanf("%d%d%d",&L,&R,&C)&&L&&R&&C) { for(int i=1; i<=L; i++) { for(int j=1; j<=R; j++) { for(int k=1; k<=C; k++) { cin>>map[i][j][k]; if(map[i][j][k]=='S') { star_x=i; star_y=j; star_z=k; } if(map[i][j][k]=='E') { end_x=i; end_y=j; end_z=k; // printf("%d %d %d\n",i,j,k); } } // getchar(); } } BFS(); } return 0; }
相关文章推荐
- 安卓通过layout_weight按比例布局
- Android 用popWindow遇见的问题
- 【Android】自定义ProgressBar,SeekBar【二】
- go语言实现的简单web服务器
- Map的三种遍历方式
- 【一天一道LeetCode】#83. Remove Duplicates from Sorted List
- 四套读写方案
- 【一天一道LeetCode】#83. Remove Duplicates from Sorted List
- leetcode 275. H-Index II-h因子|二分查找
- Zabbix实战企业监控之MySQL监控
- c++ 文件操作
- 论硬盘损坏丢失视频是否可以恢复
- 使用Volley加载网络图片
- [从头读历史] 第245节 三皇与五帝时期的全面解读
- 检测目标程序ELF bit是32还是64
- OutputCache缓存各参数的说明
- 脉络清晰的BP神经网络
- PHP基础-数据类型和运算符
- iOS 零碎知识点总结
- JFinal源码解析与思想理解