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 ...
随机推荐
- 39)django-XSS 过滤
使用kingedit别人是可以输入script代码.这在后台是不允许script代码运行的. 这里主要使用beatifulSoup过滤 示例1 beatufulsoup4 from bs4 impor ...
- vue打包项目后使用-webkit-line-clamp: 2;这个属性不生效?
在项目中要实现多行省略,-webkit-line-clamp: 2;打包后不生效,使用下面的方法: word-break: break-all; text-overflow: ellipsis; di ...
- 最近Android真的凉凉了?
都说Android最近行情不好,很多人都遇到瓶颈或放弃或转行.其实这种情况15年16年也是如此,相对比之下,个人认为今年比去年好多了,Android接下来将会走向复苏的春天. 自从Google开始推出 ...
- 探索一个NSObject对象占用多少内存?
1 下面写代码测试探索NSObject的本质 Objective-C代码,底层实现其实都是C\C++代码 #import <Foundation/Foundation.h> int mai ...
- 整合Flask中的目录结构
一.SQLAlchemy-Utils 由于sqlalchemy中没有提供choice方法,所以借助SQLAlchemy-Utils组件提供的choice方法 import datetime from ...
- phpmyadmin 安装
首先,安装mysql $ sudo apt-get install mysql-server$ sudo apt-get install mysql-client安装时输出root用户的密码在安装ph ...
- laravel 兜底路由
在 Laravel 5.6 中,引入了兜底路由功能.所谓兜底路由,就是当路由文件中定义的所有路由都无法匹配用户请求的 URL 时,用来处理用户请求的路由,在此之前,Laravel 都会通过异常处理器为 ...
- IDEA拷贝操作
另外一种添加方式
- jquery实现全选 反选 取消
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- webpack学习笔记--其它配置项
其它配置项 除了前面介绍到的配置项外,Webpack 还提供了一些零散的配置项.下面来介绍它们中常用的部分. Target JavaScript 的应用场景越来越多,从浏览器到 Node.js,这些 ...