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 ...
随机推荐
- 基于MVC 的Quartz.Net组件实现的定时执行任务调度
新建mvc项目之后,首先引用Quartz组件.工具-->NuGet包管理器-->管理解决方案的 NuGet包管理器 组件安装完成. Quartz.Net一个最简单任务至少包括三部分实现:j ...
- lua内存监测和回收
以下来自书籍<Cocos2d-x之Lua核心编程> 1.----------------------------------------- 若想查看程序当前的内存占用情况,可以使用Lua提 ...
- RedHat Linux关闭防火墙的命令
获得root 控制权限.在“#”下操作. 查看防火墙状态. systemctl status firewalld 临时关闭防火墙命令.重启电脑后,防火墙自动起来. systemctl stop fir ...
- jQuery对url的操作
// 修改URL中指定参数的值 function changeURLArg(url,arg,arg_val){ var pattern=arg+'=([^&]*)'; var replaceT ...
- Confluence 6 恢复一个站点有关使用站点导出为备份的说明
推荐使用生产备份策略.我们推荐你针对你的生产环境中使用的 Confluence 参考 Production Backup Strategy 页面中的内容进行备份和恢复(这个需要你备份你的数据库和 ho ...
- django模板导入外部js和css等文件
1.新建文件夹templates(存放模板文件),新建文件夹media(存放js.css.images文件夹),并把两个文件夹放到了项目的根目录下 2.设定模板路径 设置模板路径比较简单,只要在set ...
- 将CSDN内容移过来
将CSND主要博客内容移过来 1.Linux rpm.yum.ssh.apache.Samba等讲解 https://blog.csdn.net/weixin_38834998/article/det ...
- LeetCode(78):子集
Medium! 题目描述: 给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: nums = [1,2,3] 输出: [ [3 ...
- linux下安装pip与pip安装
在执行脚本的时候,说有库找不到 pip安装的时候说不认识pip 安装pip 使用脚本安装和升级pip wget https://bootstrap.pypa.io/get-pip.py 运行脚本pyt ...
- python 自动获取手机短信验证码
需要一个有权限的 APK 在手机实时存储短信到手机内存 /sdcard/smslog.txt 里(外部SD卡也可以知道能通过adb命令访问到): /***** ...... try {long tim ...