题目描述

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. CMMI基础知识扫盲

    CMMI全称是Capability Maturity Model Integration,CMMI是个好东西来的,但行内人士对她的认识并不全面,甚至有种种的误解.尽管网上有很多CMMI相关介绍,但一般 ...

  2. linux下修改jar中的文件

    解压修改后再打包 解压: jar xvf xxx.jar 打包: jar cvfm0 xxx.jar META-INF/MANIFEST.MF ./ 注: -m参数可以将 一个具体的mainfest文 ...

  3. SpringBoot缓存技术

    一.SpringBoot整合Ehhcache 添加maven依赖 <dependency> <groupId>org.springframework.boot</grou ...

  4. vlookup实战_英语单词更新

    https://study.163.com/provider/400000000398149/index.htm?share=2&shareId=400000000398149( 欢迎关注博主 ...

  5. mac上运行shell脚本遇到回车字符错误

    今天运行一段其他人给的shell脚本,遇到如下问题,这个脚本的内容如下: dname=\((dirname "\)PWD") mkdir ${dname}"/rom_pu ...

  6. angular自定义组件

    https://cli.angular.io/ 打开终端创建header组件: ng g component components/header import { Component, OnInit ...

  7. js刷新页面location.reload()用法

    转: js刷新页面location.reload()用法 2018年05月10日 10:23:28 大灰狼的小绵羊哥哥 阅读数 31359更多 分类专栏: [前端面试点滴知识 ]   本文介绍了js刷 ...

  8. 23Flutter FloatingActionButton实现类似闲鱼App底部导航凸起按钮:

    /* 一.Flutter FloatingActionButton介绍 FloatingActionButton简称FAB,可以实现浮动按钮,也可以实现类型闲鱼app的底部凸起导航. child:子视 ...

  9. Qt编写控件属性设计器4-加载属性

    一.前言 控件能加载拖曳拉伸了,这些都是基本的前提工作,接下来的重点就是要动态加载选中控件的属性了,Qt的属性机制那是异常的强大,只能用强大到爆来形容,Qt中编写自定义控件,如果属性都用Q_PROPE ...

  10. 一百四十三:CMS系统之评论布局和功能一

    模型 class CommentModel(db.Model): """ 评论 """ __tablename__ = 'comment' ...