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 ...
随机推荐
- layui框架--关闭当前页面并刷新父页面
//关闭当前页面 并刷新父页面 var index = parent.layer.getFrameIndex(window.name); parent.layer.close(index) windo ...
- jdbcTemplate 调用存储过程。 入参 array 返回 cursor
注:本文来源< jdbcTemplate 调用存储过程. 入参 array 返回 cursor > 需求: java传入一个list object.从数据库找到相关的数据并返回. ...
- nodejs之glob与globby
glob glob允许使用规则,从而获取对应规则匹配的文件.这个glob工具基于javascript.它使用了 minimatch 库来进行匹配 安装 npm install glob 引入 cons ...
- 【git】提交代码到远程仓库
看完不用,就是一个字:忘! 之前学了两天git结果今天要用的时候,啥也想不起来.... 场景: 已有远程仓库: git@192.168.1.1:test/test.git 要提交代码到远程仓库的新分支 ...
- 闭包&装饰器
闭包 1.函数引用 def test(): print('--test--') # 调用函数 test() # 引用函数 ret = test print(id(ret)) print(id(test ...
- django配置一个网站建设
第一步: 安装数据库MySQL,也可以使用pycharm自带的数据库sqllite,大项目要使用数据库.安装请参考上篇. 数据库在pycharm中驱动设置,setting文件中修改驱动文件密码等信息. ...
- cf1143E 倍增好题!
一开始感觉用莫队可以搞一下,但是看了题解才发现这题其实是倍增套路题 把排列转换成nxt数组,然后倍增dp[i][j]表示第i个数后面有(1<<j)个数的最靠左的区间 然后从右往左扫一次即可 ...
- jQuery 常用的方法
<!DOCTYPE html><html lang="en"><head> <meta charset="utf-8" ...
- 步步为营-89-SQL语句(删除重复数据)
1:删除重复数据 --第一步:先找到重复数据 select ProcInstID from record_errorlog group by ProcInstID having count(ProcI ...
- tensorflow:验证码的识别(上)
验证码的识别 主要分成四个部分:验证码的生成.将生成的图片制作成tfrecord文件.训练识别模型.测试模型 使用pyCharm作为编译器.本文先介绍前两个部分 验证码的识别有两种方法: 验证码识别方 ...