zoukankan      html  css  js  c++  java
  • 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 ;
    }
    
    每次做题提醒自己:题目到底有没有读懂,有没有分析彻底、算法够不够贪心、暴力够不够优雅。
  • 相关阅读:
    vi编辑器命令大全
    Ubuntu环境搭建svn服务器
    Visual Studio中“后期生成事件命令行” 中使用XCopy命令
    解决 Visual Studio For Mac 还原包失败问题
    [Win10应用开发] 如何使用Windows通知
    [Win10应用开发] 使用 Windows 推送服务
    如何使用 PsExec 执行远程命令
    如何在调试Window App时,触发 Suspending ,Resuming 等事件
    如何 “解决” WPF中空域问题(Airspace issuse)
    浅谈可扩展性框架:MEF
  • 原文地址:https://www.cnblogs.com/spnooyseed/p/12870915.html
Copyright © 2011-2022 走看看