Help the Princess!

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

题目大意就是一个n*m的地图,其中有一个’@’表示公主,有若干个’$’表示士兵,一个’%’表示出口,’#’表示墙(无法进入),’.’表示可以走的地方。其中公主想要逃亡到出口,有士兵追击,若公主能够走到出口且没有遇到士兵,就输出’Yes’,否则输出’No’。特别注意如果公主逃到出口时士兵也到了出口,公主也不能成功逃亡。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue> 
using namespace std;
const int INF = 0x3f3f3f3f ;
int n , m ;
char map[220][220] ;
bool vis[220][220] ;
int sx , sy ;
int dir[4][2] = {1 , 0 , -1 , 0 , 0 , 1 , 0 , -1} ;
struct Node
{
	int x , y , s ;
	Node(int _x = 0 , int _y = 0 , int _s = 0)
	{
		x = _x , y = _y , s = _s ;
	//  cout << x << " " << y << " " << s << endl ;
	}
};
bool bfs()
{
	int ps = INF , ss = INF ;
	queue<Node> q ;
	int s = 0 ;
	memset(vis , false , sizeof vis) ;
	q.push(Node(sx , sy , s)) ;
	vis[sx][sy] = true ;
	while(q.size())
    {
    	Node t = q.front() ;
    	q.pop() ;
    	for(int i = 0 ;i < 4 ;i ++)
    	 {
    	 	int nx = t.x + dir[i][0] ;
    	 	int ny = t.y + dir[i][1] ;
    	 	int ns = t.s + 1 ;
    	 	if(vis[nx][ny]) continue ;
    	 	vis[nx][ny] =true ;
    	 	if(nx <= 0 || nx > n || ny <= 0 || ny> m) continue ;
    	 	if(map[nx][ny] == '#') continue ;
    	 	if(map[nx][ny] == '@') 
    	      {
    	      	ps = min(ns , ps) ;
    	      	continue ;
			  }
			if(map[nx][ny] == '$')
			 {
			 	ss = min(ns , ss) ;
			 	continue ;
			 }
			 q.push(Node(nx , ny , ns)) ;
		 }
		 
	}
	if(ps < ss) return true ;
	else return false ;

}
int main()
{
	while(~scanf("%d%d",&n , &m))
	{
		memset(map , 0 , sizeof map) ;
		for(int i = 1 ;i <= n ;i ++)
		 {
		 	scanf("%s" , map[i] + 1) ;
		 	for(int j = 1; j <= m;j ++)
			  {
			  	if(map[i][j] == '%')
			  	 sx = i , sy = j ;
			   } 
		 }
		 if(bfs()) puts("Yes") ;
		 else puts("No") ;
	}
	return 0 ;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值