poj2251——Dungeon Master
来源:互联网 发布:淘宝商品排名 编辑:程序博客网 时间:2024/06/05 15:29
题目大意:在三维地牢中移动可以前后左右,也可以上下楼,一共六个方向,走一步需要一分钟,问从地牢中逃出最少需要几分钟
输入:(可以有几个case 以0 0 0结束输入)
地牢层数L 每层地牢的行数R 列数C(所有LRC都不超过30)
第i行的地牢情况(#表示墙 .表示空地 S表示入口 E表示出口)
输出:如果可以走出则输出为Escaped in x minute(s).
如果走不出则输出为Trapped!
分析:bfs求最短路。与二维类似,只不过这道题是三维,四个方向改成六个方向即可。
map[][][]:表示第k层地牢的第r行第c列
输入的同时记录入口S位置和出口E位置
bfs六个方向,判断能不能走,如果越界或撞墙或已经遍历过,就continue下一个方向,否则意味着能走,将该点入队
代码:转载自http://blog.csdn.net/libin56842/article/details/23702395
- #include <iostream>
- #include <stdio.h>
- #include <string.h>
- #include <queue>
- #include <algorithm>
- using namespace std;
- char map[35][35][35];
- int vis[35][35][35];
- int k,n,m,sx,sy,sz,ex,ey,ez;
- int to[6][3] = {{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};
- struct node
- {
- int x,y,z,step;
- };
- int check(int x,int y,int z)
- {
- if(x<0 || y<0 || z<0 || x>=k || y>=n || z>=m)
- return 1;
- else if(map[x][y][z] == '#')
- return 1;
- else if(vis[x][y][z])
- return 1;
- return 0;
- }
- int bfs()
- {
- int i;
- node a,next;
- queue<node> Q;
- a.x = sx,a.y = sy,a.z = sz;
- a.step = 0;
- vis[sx][sy][sz] = 1;
- Q.push(a);
- while(!Q.empty())
- {
- a = Q.front();
- Q.pop();
- if(a.x == ex && a.y == ey && a.z == ez)
- return a.step;
- for(i = 0; i<6; i++)
- {
- next = a;
- next.x = a.x+to[i][0];
- next.y = a.y+to[i][1];
- next.z = a.z+to[i][2];
- if(check(next.x,next.y,next.z))
- continue;
- vis[next.x][next.y][next.z] = 1;
- next.step = a.step+1;
- Q.push(next);
- }
- }
- return 0;
- }
- int main()
- {
- int i,j,r;
- while(scanf("%d%d%d",&k,&n,&m),n+m+k)
- {
- for(i = 0; i<k; i++)
- {
- for(j = 0; j<n; j++)
- {
- scanf("%s",map[i][j]);
- for(r = 0; r<m; r++)
- {
- if(map[i][j][r] == 'S')
- {
- sx = i,sy = j,sz = r;
- }
- else if(map[i][j][r] == 'E')
- {
- ex = i,ey = j,ez = r;
- }
- }
- }
- }
- memset(vis,0,sizeof(vis));
- int ans;
- ans = bfs();
- if(ans)
- printf("Escaped in %d minute(s).\n",ans);
- else
- printf("Trapped!\n");
- }
- return 0;
- }
阅读全文
0 0
- POJ2251——Dungeon Master
- POJ2251——Dungeon Master
- poj2251——Dungeon Master
- poj2251——Dungeon Master(BFS)
- POJ2251 Dungeon Master —— BFS
- POJ2251 Dungeon Master
- POJ2251 Dungeon Master
- poj2251 Dungeon Master (BFS)
- poj2251 - Dungeon Master
- poj2251 Dungeon Master
- POJ2251:Dungeon Master
- Dungeon Master(poj2251,bfs)
- Dungeon Master(poj2251)
- poj2251 Dungeon Master
- POJ2251:Dungeon Master(BFS)
- poj2251 Dungeon Master bfs
- POJ2251:Dungeon Master(BFS)
- poj2251 Dungeon Master(BFS)
- poj 1321 棋盘问题 dfs深搜
- mysql的sql执行计划详解
- 设计模式六大原则之----里氏替换原则
- POJ-3714 Raid
- BAT通吃人工智能?垂直行业仍有机遇
- poj2251——Dungeon Master
- 关于预处理
- linux调试器的实现---断点的实现
- 搞定BAT Java面试题
- LeetCode- Jump game
- SPI
- React高级指南(三)【Refs and the DOM】
- Java-script学习笔记--函数
- Java实现时钟