题目描述

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. [RK3399] 虚拟按键栏显示不全或者方向不对

    CPU:RK3399 系统:Android 7.1 同样的系统代码,换了一个小分辨率的屏,虚拟按键栏就出现显示不全,而且方向不对 出现这种问题的原因是显示密度和屏不匹配,需要适当的降低显示密度即可 d ...

  2. Navicat Premium 12安装与激活(亲测已成功激活)

    说明:博主所提供的激活文件理论支持Navicat Premium 12.0.16 - 12.0.24简体中文64位,但已测试的版本为Navicat Premium 12.0.22.12.0.23和12 ...

  3. selinux 开启和关闭

    对于新手来说,linux的selinux困扰了一大批学员,开启后,导致文件权限修改不了等问题,下面就是关闭设置setlinux的方法 查看SELinux状态: 1./usr/sbin/sestatus ...

  4. [微信小程序]实现一个自定义遮罩层

    正文: 先上效果图: 点击按钮Show显示遮罩层,再次点击屏幕任何地方隐藏遮罩层; <button bindtap="showview">Show</button ...

  5. Tomcat重启Shell脚本

    --测试通过 vim restart_tomcat.sh#!/bin/bash# Filename: restart_tomcat.sh# Description: Restart tomcat# D ...

  6. DisplayAttribute应用——根据PropertyName自动获取对应的UI显示名

    model定义,使用DisplayAttribute public class AddressSetInfo { /// <summary> /// invoiceAddress.Id / ...

  7. CentOS7下搭建zabbix监控(五)——Web端配置自动发现并注册

    好像有点问题,没法自动添加主机,我后期再测测 (1).自动发现主机并注册 1)创建发现规则 2)编辑自动发现规则信息(这两步不配置问题也不大,因为在动作中也有主机IP地址) 3)添加自动发现的动作 4 ...

  8. (二十)sql基础

    sql基础 --单表查询 select * from student; select * from score; --投影查询 select * from student; --条件查询 select ...

  9. DB2函数简单示例

    DB2中的函数原理同其他编程语言中的函数,均为输入几个参数,同时返回一个值. 下面的例子演示一个寻找某一次考试中成绩最好的学生的姓名. 首先,我们新建一个表SCORE用于表示考试,并插入几条数据: D ...

  10. 第九章 JSP标签——《跟我学Shiro》

    转发地址:https://www.iteye.com/blog/jinnianshilongnian-2026398 博客分类: 跟我学Shiro 跟我学Shiro  目录贴:跟我学Shiro目录贴 ...