题目描述

The people of a certain kingdom make a revolution against the bad government of the princess. The revolutionary army invaded the royal palace in which the princess lives. The soldiers of the army are exploring the palace to catch the princess. Your job is writing a program to decide that the princess can escape from the royal palace or not.

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?

输入

Each dataset is formatted as follows.

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.

输出

Output a line containing a word "Yes", if the princess can escape from the palace. Otherwise, output "No".

样例输入

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!的更多相关文章

  1. 【bfs】抓住那头牛

    [题目] 农夫知道一头牛的位置,想要抓住它.农夫和牛都位于数轴上,农夫起始位于点N(0≤N≤100000),牛位于点K(0≤K≤100000).农夫有两种移动方式: 1.从X移动到X-1或X+1,每次 ...

  2. 【bfs】拯救少林神棍(poj1011)

    Description 乔治拿来一组等长的木棒,将它们随机地砍断,使得每一节木棍的长度都不超过50个长度单位.然后他又想把这些木棍恢复到为裁截前的状态,但忘记了初始时有多少木棒以及木棒的初始长度.请你 ...

  3. 【bfs】Knight Moves

    [题目描述] 输入nn代表有个n×nn×n的棋盘,输入开始位置的坐标和结束位置的坐标,问一个骑士朝棋盘的八个方向走马字步,从开始坐标到结束坐标可以经过多少步. [输入] 首先输入一个nn,表示测试样例 ...

  4. 【bfs】1252 走迷宫

    [题目描述] 一个迷宫由R行C列格子组成,有的格子里有障碍物,不能走:有的格子是空地,可以走. 给定一个迷宫,求从左上角走到右下角最少需要走多少步(数据保证一定能走到).只能在水平方向或垂直方向走,不 ...

  5. 【bfs】献给阿尔吉侬的花束

    [题目描述] 阿尔吉侬是一只聪明又慵懒的小白鼠,它最擅长的就是走各种各样的迷宫.今天它要挑战一个非常大的迷宫,研究员们为了鼓励阿尔吉侬尽快到达终点,就在终点放了一块阿尔吉侬最喜欢的奶酪.现在研究员们想 ...

  6. 【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, }; 它表示一个迷 ...

  7. 【bfs】仙岛求药

    [题目描述] 少年李逍遥的婶婶病了,王小虎介绍他去一趟仙灵岛,向仙女姐姐要仙丹救婶婶.叛逆但孝顺的李逍遥闯进了仙灵岛,克服了千险万难来到岛的中心,发现仙药摆在了迷阵的深处.迷阵由M×N个方格组成,有的 ...

  8. 【bfs】BZOJ1102- [POI2007]山峰和山谷Grz

    最后刷个水,睡觉去.Bless All! [题目大意] 给定一个地图,为FGD想要旅行的区域,地图被分为n*n的网格,每个格子(i,j) 的高度w(i,j)是给定的.若两个格子有公共顶点,那么他们就是 ...

  9. poj3278-Catch That Cow 【bfs】

    http://poj.org/problem?id=3278 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submis ...

随机推荐

  1. C语言的历史

    1.ALGOL语言 ALGOL ,为算法语言(ALGOrithmic Language)的缩写,是计算机发展史上首批产生的高级程式语言家族.当时还是晶体管计算机流行的时代,由于ALGOL语句和普通语言 ...

  2. jquery报变量没定义错误的原因

    该变量定义的范围:不在使用的方法中:

  3. RK3288 添加普通串口uart1和uart3

    CPU:RK3288 系统:Android 5.1 diff --git a/device/rockchip/common/init.connectivity.rc b/device/rockchip ...

  4. LeetCode 快乐数(Happy Number)

    题目描述 编写一个算法来判断一个数是不是“快乐数”. 一个“快乐数”定义为:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复这个过程直到这个数变为 1,也可能是无限循环但始终变不 ...

  5. 深度学习: 学习率 (learning rate)

    Introduction 学习率 (learning rate),控制 模型的 学习进度 : lr 即 stride (步长) ,即反向传播算法中的 ηη : ωn←ωn−η∂L∂ωnωn←ωn−η∂ ...

  6. 手写一个MVVM

    最近看了珠峰的架构课——实现一个MVVM. 首先,我们来了解一下什么是MVVM. MVVM是Model-View-ViewModel的简写.它本质上就是MVC 的改进版.MVVM 就是将其中的View ...

  7. 个微信小程序云开发云函数

    1. project.config.json写上云函数所在目录"cloudfunctionRoot": "cloudfunctions/",如图 2. app. ...

  8. OpenJudge计算概论-最长单词2

    /*======================================================================== 最长单词2 总时间限制: 1000ms 内存限制: ...

  9. Borg、Omega和Kubernetes:谷歌十几年来从这三个容器管理系统中得到的经验教训 原创: 韩佳瑶 译 Docker 2016-03-23Borg、Omega和Kubernetes:谷歌十几年来从这三个容器管理系统中得到的经验教训 原创: 韩佳瑶 译 Docker 2016-03-23

    Borg.Omega和Kubernetes:谷歌十几年来从这三个容器管理系统中得到的经验教训 原创: 韩佳瑶 译 Docker 2016-03-23

  10. VS版本 与 VC版本 的对应关系

    Visual Studio 6 : vc6Visual Studio 2003 : vc7Visual Studio 2005 : vc8Visual Studio 2008 : vc9Visual ...