【BFS】Help the Princess!
题目描述
For simplicity, the ground of the palace is a rectangle divided into a grid. There are two kinds of cells in the grid: one is a cell that soldiers and the princess can enter, the other is a cell that soldiers or the princess cannot enter. We call the former an empty cell, the latter a wall. The princess and soldiers are in different empty cells at the beginning. There is only one escape hatch in the grid. If the princess arrives the hatch, then the princess can escape from the palace. There are more than or equal to zero soldiers in the palace.
The princess and all soldiers take an action at the same time in each unit time. In other words, the princess and soldiers must decide their action without knowing a next action of the other people. In each unit time, the princess and soldiers can move to a horizontally or vertically adjacent cell, or stay at the current cell. Furthermore the princess and soldiers cannot move out of the ground of the palace. If the princess and one or more soldiers exist in the same cell after their move, then the princess will be caught. It is guaranteed that the princess can reach the escape hatch via only empty cells if all soldiers are removed from the palace.
If there is a route for the princess such that soldiers cannot catch the princess even if soldiers make any moves, then the princess can escape the soldiers. Note that if the princess and a soldier arrive the escape hatch at the same time, the princess will be caught. Can the princess escape from the palace?
输入
H W
map1
map2
...
mapH
The first line of a dataset contains two positive integers H and W delimited by a space, where H is the height of the grid and W is the width of the grid (2≤H,W≤200).
The i-th line of the subsequent H lines gives a string mapi, which represents situation in the ground of palace.
mapi is a string of length W, and the j-th character of mapi represents the state of the cell of the i-th row and the j-th column.
'@', '$', '%', '.', and '#' represent the princess, a soldier, the escape hatch, an empty cell, and a wall, respectively. It is guaranteed that there exists only one '@', only one '%', and more than or equal to zero '$' in the grid.
输出
样例输入
2 4
%.@$
..$$
样例输出
Yes
【小结】
本来想以前的那个小孩子放两团火那样操作的,就是说先放士兵进去,然后再放公主进去,然后看哪个士兵能否在公主进入%前捉住。
后来赛后知道原来是我看错题目了。
题目意思是士兵能否在公主找到%之前找到。
【题解】
就是利用%,来找士兵和公主,如果士兵有多个,那么就直接更新士兵的最短时间即可。
逆向思维,利用%进行BFS。最后比较对应的时间即可。
#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int N = ; int dir[][] = {
{-,},
{,-} , {,},
{ ,}
};
int n,m;
char G[N][N];
int vis[N][N];
typedef struct Node{
int x , y ,step ;
Node( int X = , int Y =
, int Step = ):
x(X),y(Y),step(Step){}
}Node;
int num1 , num2 ;
queue <Node> Q ;
void BFS(){ while( !Q.empty() ){
Node cur = Q.front() ;
Q.pop(); for(int i=;i<;i++){
int tx = cur.x + dir[i][] ;
int ty = cur.y + dir[i][] ;
if( !( <= tx && tx <= n && <= ty && ty <= m) ) continue;
if( G[tx][ty] == '#' ){
continue ;
}else if( G[tx][ty] == '@' ){
num1 = min( num1 , cur.step ) ;
}else if( G[tx][ty] == '$' ){
num2 = min( num2 , cur.step ) ;
}else if( G[tx][ty] == '.' && vis[tx][ty] == ){
vis[tx][ty] = ;
Q.push( Node(tx,ty,cur.step+) );
}
}
}
if( num1 < num2 ){
puts("Yes\n");
}else{
puts("No\n");
}
return ;
}
int main()
{
scanf("%d%d",&n,&m);
for( int i = ; i <= n ; i++ ){
scanf("%s", G[i]+ );
}
int px , py ;
for( int i = ; i <= n ; i++ ){
for( int j = ; j <= m ; j++ ){
if( G[i][j] == '%' ){
px = i , py = j ;
}
}
} num1 = num2 = inf ;
Q.push( Node( px , py , ) );
vis[px][py] = ;
BFS() ; return ;
}
【BFS】Help the Princess!的更多相关文章
- 【bfs】抓住那头牛
[题目] 农夫知道一头牛的位置,想要抓住它.农夫和牛都位于数轴上,农夫起始位于点N(0≤N≤100000),牛位于点K(0≤K≤100000).农夫有两种移动方式: 1.从X移动到X-1或X+1,每次 ...
- 【bfs】拯救少林神棍(poj1011)
Description 乔治拿来一组等长的木棒,将它们随机地砍断,使得每一节木棍的长度都不超过50个长度单位.然后他又想把这些木棍恢复到为裁截前的状态,但忘记了初始时有多少木棒以及木棒的初始长度.请你 ...
- 【bfs】Knight Moves
[题目描述] 输入nn代表有个n×nn×n的棋盘,输入开始位置的坐标和结束位置的坐标,问一个骑士朝棋盘的八个方向走马字步,从开始坐标到结束坐标可以经过多少步. [输入] 首先输入一个nn,表示测试样例 ...
- 【bfs】1252 走迷宫
[题目描述] 一个迷宫由R行C列格子组成,有的格子里有障碍物,不能走:有的格子是空地,可以走. 给定一个迷宫,求从左上角走到右下角最少需要走多少步(数据保证一定能走到).只能在水平方向或垂直方向走,不 ...
- 【bfs】献给阿尔吉侬的花束
[题目描述] 阿尔吉侬是一只聪明又慵懒的小白鼠,它最擅长的就是走各种各样的迷宫.今天它要挑战一个非常大的迷宫,研究员们为了鼓励阿尔吉侬尽快到达终点,就在终点放了一块阿尔吉侬最喜欢的奶酪.现在研究员们想 ...
- 【bfs】迷宫问题
[题目描述] 定义一个二维数组: int maze[5][5] = { 0,1,0,0,0, 0,1,0,1,0, 0,0,0,0,0, 0,1,1,1,0, 0,0,0,1,0, }; 它表示一个迷 ...
- 【bfs】仙岛求药
[题目描述] 少年李逍遥的婶婶病了,王小虎介绍他去一趟仙灵岛,向仙女姐姐要仙丹救婶婶.叛逆但孝顺的李逍遥闯进了仙灵岛,克服了千险万难来到岛的中心,发现仙药摆在了迷阵的深处.迷阵由M×N个方格组成,有的 ...
- 【bfs】BZOJ1102- [POI2007]山峰和山谷Grz
最后刷个水,睡觉去.Bless All! [题目大意] 给定一个地图,为FGD想要旅行的区域,地图被分为n*n的网格,每个格子(i,j) 的高度w(i,j)是给定的.若两个格子有公共顶点,那么他们就是 ...
- poj3278-Catch That Cow 【bfs】
http://poj.org/problem?id=3278 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submis ...
随机推荐
- Java网站视频资源加密
----------------------------------------------------------分享此文章,只为让版权能够得到更多的保护---------------------- ...
- Eclipse简明使用教程(java集成开发环境)
说明:我使用的是绿色便携版的Eclipse,免安装,解压就能用.链接:https://download.csdn.net/download/loongstyle/10700321 1_Eclipse中 ...
- spring boot + vue 前后分离实现登录功能(二)
安装 axios 进行路由转发 npm install axios --save-dev 或者 cnpm install axios --save-dev 修改 Main.js 新增 var axio ...
- thymeleaf 声明
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd&qu ...
- SpringBoot-文件在线预览解决方案-基于OpenOffice及jacob
项目中有一个需求:实现文件(主要是Office文件)的在线预览,根据前端需求,Office文件需要转换成pdf或者html方可在浏览器中打开预览,那么后端需要将文件转为pdf/格式返回地址给前端.目前 ...
- legend3---16、网站的安全性问题
legend3---16.网站的安全性问题 一.总结 一句话总结: 通过客户端传递参数的方式也是有些危险,需要注意 单纯的获取数据的方法还好,但是 修改数据库方法一定要同时做前后端验证 1.php的b ...
- 【Java】 HashMap
Java HashMap 标签(空格分隔): Java source-code hash-map 总结 HashTable的基本数据结构 Entry的hash与table的长度计算indexFor才能 ...
- POM 标签大全详解
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- OpenSL ES: 利用OpenSL ES播放一个存在于SDcard上的PCM文件
native-lib.cpp #include <jni.h> #include <string> #include <SLES/OpenSLES.h> #incl ...
- Chrome 浏览器自动填表呈现淡黄色解决
Chrome 浏览器,当记住用户名和密码后,下次填写表单时,被记住的部分会被填充为淡黄色,有些时候不好看. 解决方式如下: input:-webkit-autofill { -webki ...