【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 ...
随机推荐
- EASYUI赋值和取值
$("#ajname").textbox("setValue",name); $("#jyzjg").combobox(&q ...
- 【代码】python 绘图(以faster rcnn 的 loss 曲线为例)
# -*- coding=utf-8 -*-''' import matplotlib.pyplot as plt import re logs=open('loss').read() #print ...
- Java 程序流程语句
顺序结构 什么是顺序结构:一行一行的执行代码 选择结构 if 什么是选择结构:通过判断条件来做选择的语句,我们称为选择语句或分支语句 定义方式:if语句使用boolean表达式或boolean值作为选 ...
- sqlserver 触发器语法
语法:Trigger on an INSERT, UPDATE, or DELETE statement to a table or view (DML Trigger)CREATE TRIGGER ...
- Centos7 安装svn步骤(宝塔面板)
1.运行以下命令安装SVN yum install subversion 2.运行以下命令创建目录 mkdir /var/svn 3.依次运行以下命令创建版本库 cd /var/svn svnad ...
- 使用oid2name列出数据库和对应的oid
[postgres@postgre base]$ ll total 172 drwx------ 2 postgres postgres 12288 Dec 6 09:21 1 drwx------ ...
- Java并发包线程池之ForkJoinPool即ForkJoin框架(二)
前言 前面介绍了ForkJoinPool相关的两个类ForkJoinTask.ForkJoinWorkerThread,现在开始了解ForkJoinPool.ForkJoinPool也是实现了Exec ...
- $().attr() 和 $().css的区别
使用jquery的.attr( )方法去给div隐藏,这样写的 $("#div_id").attr("display","none"),发现 ...
- react中异步的使用
let promise; promise = this.props.corporationService.preSearchPage(params); promise.then((data) => ...
- 超详细的RNN代码实现(tensorflow)
一.学习单步的RNN:RNNCell 如果要学习TensorFlow中的RNN,第一站应该就是去了解“RNNCell”,它是TensorFlow中实现RNN的基本单元,每个RNNCell都有一个cal ...