poj-2251 Dungeon Master【bfs】
来源:互联网 发布:mac用什么看文档 编辑:程序博客网 时间:2024/05/02 06:32
很基础的一道bfs
/*这题是一个三维的迷宫题目,其中用'.'表示空地,'#'表示障碍物,'S'表示起点,'E'表示终点,求从起点到终点的最小移动次数,解法和二维的类似,只是在行动时除了东南西北移动外还多了上下。对于题目给出数据的含义就是输入l,r,c,分别代表迷宫有l层,每层长宽分别是c,r。对于数据以可以这样移动(1,1,1)->(1,1,2)->(1,1,3)->(1,1,4)->(1,1,5)->(1,2,5)->(1,3,5)->(1,3,4)->(1,4,4)->(2,4,4)->(2,4,5)->(3,4,,5)共11步就可以到达终点 对于数据二明显不能到达,则输出Trapped*/#include <stdio.h>#include <string.h>#include <stdlib.h>#include <queue>#define MAX_SIZE 32using namespace std;int l,r,c;char Map[MAX_SIZE][MAX_SIZE][MAX_SIZE];bool vis[MAX_SIZE][MAX_SIZE][MAX_SIZE];int dx[6]={1,0,-1,0,0,0};int dy[6]={0,1,0,-1,0,0};int dz[6]={0,0,0,0,-1,1};struct node{int x,y,z,step;};bool judge(int x,int y,int z){if(x<r&&x>=0 && y<c&&y>=0 && z<l&&z>=0 && !vis[z][x][y] &&Map[z][x][y]!='#')return true;return false;}int solve(node &s,node &e){memset(vis,0,sizeof(vis));queue<node> que;que.push(s);vis[s.x][s.y][s.z]=1;while(!que.empty()){node u=que.front();que.pop();// printf("%d %d %d\n",u.x,u.y,u.z);if(u.x==e.x && u.y==e.y && u.z==e.z)return u.step;for(int i=0;i<6;++i){int mx=u.x+dx[i],my=u.y+dy[i],mz=u.z+dz[i];if(judge(mx,my,mz)){que.push((node){mx,my,mz,u.step+1});vis[mz][mx][my]=1;}}}return -1;}int main(){while(~scanf("%d%d%d",&l,&r,&c)){if(l==0&&r==0&&c==0)break;node s,e;for(int i=0;i<l;++i)for(int j=0;j<r;++j)scanf("%s",Map[i][j]);for(int i=0;i<l;++i)for(int j=0;j<r;++j)for(int k=0;k<c;++k){if(Map[i][j][k]=='S'){s.x=j,s.y=k,s.z=i,s.step=0;}if(Map[i][j][k]=='E'){e.x=j,e.y=k,e.z=i,e.step=0;}}int t=solve(s,e);if(t!=-1)printf("Escaped in %d minute(s).\n",t);else printf("Trapped!\n");}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)
- 解决软键盘对fixed的影响
- Redis学习笔记(二)Redis持久化
- 30.leetcode题目104: Maximum Depth of Binary Tree(递归,深度优先)
- Hadoop单机安装测试
- Android开发教程(三)
- poj-2251 Dungeon Master【bfs】
- 当使用vim编辑commit msg时,希望直接退出,而不是退出后提交
- 在Linux里设置环境变量的方法(export PATH)
- iOS CPU占有率达到了100%甚至更多,然后导致App闪退情况总结及解决过程。
- 仿OPPO R7 plus中的时钟应用的导航效果
- 获取应用版本号
- PHP cURL 返回 HTTP/1.1 100 Continue
- centos系统下载
- 进程间通讯总结 (5)