POJ 2251 Dungeon Master (非三维bfs)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 55224 | Accepted: 20493 |
Description
Is an escape possible? If yes, how long will it take?
Input
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
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! 思路:看了网上很多人用的都是三维的bfs,但是此题可以用二维解决。 向上或向下走时,只要令x坐标减去或加上R即可,要注意的就是往东西南北方向走时不能越层,而只能在同一层走。
输入中的换行可以无视。 当时做的时候找错找了好久,最后发现是for循环里面的t.y写成了t.x,我晕~ ---->>>> 以后写代码时一定要细心(ノ▼Д▼)ノ
#include<iostream>
#include<cstring> //使用memset必须加此头文件
#include<stdio.h> //使用printf必须加此头文件
#include<queue> using namespace std; int L, R, C;
int minute = ;
char maze[][];
int vis[][];
int dx[] = { ,-,, }; //南北方向
int dy[] = { ,,,- }; //东西方向 struct node
{
int x, y;
int step;
}s; void bfs(int x, int y)
{
s.x = x;
s.y = y;
s.step = ;
vis[x][y] = ;
queue<node> Q;
Q.push(s); node t;
while (!Q.empty())
{
t = Q.front();
Q.pop(); if (maze[t.x][t.y] == 'E')
{
printf("Escaped in %d minute(s).\n", t.step);
return;
} // k表示此坐标所在的层数,因为如果只是往东西南北方向走的话,只能在同一层
int k = t.x / R + ; for (int i = ; i < ; ++i)
{
int xx = t.x + dx[i];
int yy = t.y + dy[i]; //注意同一层中的坐标判断条件
if ((xx >= ((k - )*R)) && (xx < (k*R)) && yy >= & yy < C && maze[xx][yy] != '#' && !vis[xx][yy])
{
vis[xx][yy] = ;
s.x = xx;
s.y = yy;
s.step = t.step + ;
Q.push(s);
}
} //跳入下一层
int x1 = t.x + R;
int y1 = t.y;
if (x1 < L*R && maze[x1][y1] != '#' && !vis[x1][y1])
{
vis[x1][y1] = ;
s.x = x1;
s.y = y1;
s.step = t.step + ;
Q.push(s);
} //跳入上一层
int x2 = t.x - R;
int y2 = t.y;
if (x2 >= && maze[x2][y2] != '#' && !vis[x2][y2])
{
vis[x2][y2] = ;
s.x = x2;
s.y = y2;
s.step = t.step + ;
Q.push(s);
}
} cout << "Trapped!" << endl; } int main()
{
int start_x, start_y; //记录起点坐标
while (cin >> L >> R >> C)
{
if (L == && R == && C == )
break; for (int i = ; i < L*R; ++i)
for (int j = ; j < C; ++j)
{
cin >> maze[i][j]; //迷宫下标从0开始存储
if (maze[i][j] == 'S')
{
start_x = i;
start_y = j;
}
} memset(vis, , sizeof(vis));
bfs(start_x, start_y); }
return ;
}
POJ 2251 Dungeon Master (非三维bfs)的更多相关文章
- POJ.2251 Dungeon Master (三维BFS)
POJ.2251 Dungeon Master (三维BFS) 题意分析 你被困在一个3D地牢中且继续寻找最短路径逃生.地牢由立方体单位构成,立方体中不定会充满岩石.向上下前后左右移动一个单位需要一分 ...
- POJ 2251 Dungeon Master【三维BFS模板】
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 45743 Accepted: 17256 Desc ...
- poj 2251 Dungeon Master 3维bfs(水水)
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 21230 Accepted: 8261 D ...
- POJ 2251 Dungeon Master(三维空间bfs)
题意:三维空间求最短路,可前后左右上下移动. 分析:开三维数组即可. #include<cstdio> #include<cstring> #include<queue& ...
- POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)
POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层 ...
- BFS POJ 2251 Dungeon Master
题目传送门 /* BFS:这题很有意思,像是地下城,图是立体的,可以从上张图到下一张图的对应位置,那么也就是三维搜索,多了z坐标轴 */ #include <cstdio> #includ ...
- POJ 2251 Dungeon Master(地牢大师)
p.MsoNormal { margin-bottom: 10.0000pt; font-family: Tahoma; font-size: 11.0000pt } h1 { margin-top: ...
- POJ 2251 Dungeon Master /UVA 532 Dungeon Master / ZOJ 1940 Dungeon Master(广度优先搜索)
POJ 2251 Dungeon Master /UVA 532 Dungeon Master / ZOJ 1940 Dungeon Master(广度优先搜索) Description You ar ...
- POJ 2251 Dungeon Master (三维BFS)
题目链接:http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total S ...
- poj 2251 Dungeon Master
http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submis ...
随机推荐
- SpringBoot和SpringCloud面试题
一. 什么是springboot 1.用来简化spring应用的初始搭建以及开发过程 使用特定的方式来进行配置(properties或yml文件) 2.创建独立的spring引用程序 main方法运行 ...
- 8 张图帮你一步步看清 async/await 和 promise 的执行顺序(转)
https://mp.weixin.qq.com/s?__biz=MzAxODE2MjM1MA==&mid=2651555491&idx=1&sn=73779f84c289d9 ...
- poj1564 Sum It Up dfs水题
题目描述: Description Given a specified total t and a list of n integers, find all distinct sums using n ...
- 移动端判断ios还是android终端
<script> //判断ios还是android终端 var u = navigator.userAgent; var isAndroid = u.indexOf ...
- swift 学习- 22 -- 嵌套类型
// 枚举 常备用于为特定的类 或 结构体实现某些功能, 类似的, 枚举可以方便的定义工具类 或 结构体, 从而为某个复杂的类型所使用, 为了实现这种功能, Swift 允许你定义 嵌套类型, 可以在 ...
- oracle user locked(timed)处理
故障现象: SQL> connect scott/scottERROR:ORA-01017: invalid username/password; logon deniedSQL> con ...
- 获取表单内的所有元素的值 表单格式化插件jquery.serializeJSON
简单描述:一个form表单里有十几个input或者select,要获取到他们的值,我的做法一直都是$("#id").val();这样做本来没什么说的,但是如果有很多呢,就很麻烦,看 ...
- Python基础知识之大杂烩
一.range 和 xrange 的区别 xrange 与 range 基本上都是在循环的时候用,两者的用法完全相同.所不同的是xrange生成的是一个生成器,而range生成的是一个list对象. ...
- hdu1213并查集
板子题不多说,上代码 #include<iostream> #include<cstdio> #include<cstring> using namespace s ...
- 在组件放使用v-model和slot插槽的简单实用
封装的组件(SelectDefault.vue文件): <template> <div class="select-default"> <label& ...