Dungeon Master (广搜,bfs)
2015-08-07 16:18
281 查看
Dungeon Master
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)Total Submission(s) : 64 Accepted Submission(s) : 29
[align=left]Problem Description[/align]
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?
[align=left]Input[/align]
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.
[align=left]Output[/align]
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!
[align=left]Sample Input[/align]
3 4 5
S....
.###.
.##..
###.#
#####
#####
##.##
##...
#####
#####
#.###
####E
1 3 3
S##
#E#
###
0 0 0
[align=left]Sample Output[/align]
Escaped in 11 minute(s). Trapped!
题目翻译:
有一个三维迷宫,出发点是S,出口是E,第一行三个数字分别代表 高z,行x,列y。‘.’为路,‘#’为墙。每点可以朝三维的6个方向走,不走斜角。
如果走得出来输出时间(步数),否则。。。
思路:
广搜的模板题,将二维迷宫直接扩展为三维坐标即可:
#include<stdio.h> #include<string.h> #include<queue> #include<iostream> using namespace std; #define INF 0xfffffff//先设为个很大的数 int x,y,z,ex,ey,ez,ans,vis[35][35][35],L,R,C,sign; int dx[6]={0,1,-1,0,0,0};//六个方向 int dy[6]={1,0,0,-1,0,0}; int dz[6]={0,0,0,0,1,-1}; int map[30][30][30]; struct node { int x,y,z,step;//坐标,已走步数 friend bool operator < (node a,node b) { return a.step>b.step; } }a,temp;//a为当前,temp为下一步 int jud(struct node a)//判断是否可以走 { if(a.x<0||a.x>C) return 0; if(a.y<0||a.y>R) return 0; if(a.z<0||a.z>L) return 0; if(vis[a.x][a.y][a.z]||map[a.x][a.y][a.z]) return 0; if(temp.step>=ans) return 0; return 1; } void bfs() { a.x=x; a.y=y; a.z=z; a.step=0; memset(vis,0,sizeof(vis)); priority_queue<node>q;//优先队列 vis[x][y][z]=1; q.push(a); while(!q.empty()) { a=q.top(); q.pop(); for(int i=0;i<6;i++) { temp.x=a.x+dx[i];//6个方向试探 temp.y=a.y+dy[i]; temp.z=a.z+dz[i]; temp.step=a.step+1; if(jud(temp)) { if(temp.x==ex&&temp.y==ey&&temp.z==ez) { // if(ans>temp.step) sign=1;//到达E点标记为1 ans=temp.step; return; // continue; } vis[temp.x][temp.y][temp.z]=1;//记录此点已被访问 q.push(temp); } } } } int main() { char u; while(scanf("%d%d%d",&L,&R,&C),L|R|C) { for(int l=0;l<L;l++) { for(int r=0;r<R;r++) { getchar();//吸收换行符 for(int c=0;c<C;c++) { scanf("%c",&u);//讲字符阵转换为0-1矩阵 if(u=='.') map[r][c][l]=0; else if(u=='#') map[r][c][l]=1; else if(u=='S') { x=r,y=c,z=l;//起点坐标记录 map[r][c][l]=0; } else if(u=='E') { ex=r,ey=c,ez=l;//终点坐标记录 map[r][c][l]=0; } } } getchar();//吸收空行 } // for(int l=0;l<L;l++)//查看下转换的矩阵写对没 // { // for(int r=0;r<R;r++) // { // for(int c=0;c<C;c++) // printf("%d",map[r][c][l]); // putchar('\n'); // } // putchar('\n'); // } if(x==ex&&y==ey&&z==ez) { printf("0\n"); continue; } ans=INF; sign=0; bfs(); if(sign) printf("Escaped in %d minute(s).\n",ans); else printf("Trapped!\n"); } }
相关文章推荐
- 动易2006序列号破解算法公布
- Ruby实现的矩阵连乘算法
- C#插入法排序算法实例分析
- 超大数据量存储常用数据库分表分库算法总结
- C#数据结构与算法揭秘二
- C#冒泡法排序算法实例分析
- 算法练习之从String.indexOf的模拟实现开始
- C#算法之关于大牛生小牛的问题
- C#实现的算24点游戏算法实例分析
- c语言实现的带通配符匹配算法
- 浅析STL中的常用算法
- 算法之排列算法与组合算法详解
- C++实现一维向量旋转算法
- Ruby实现的合并排序算法
- C#折半插入排序算法实现方法
- 基于C++实现的各种内部排序算法汇总
- C++线性时间的排序算法分析
- C++实现汉诺塔算法经典实例
- PHP实现克鲁斯卡尔算法实例解析
- C#获取关键字附近文字算法实例