zoukankan      html  css  js  c++  java
  • Rescue

    Problem Description
    Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison.

    Angel's friends want to save Angel. Their task is: approach Angel. We assume that "approach Angel" is to get to the position where Angel stays. When there's a guard in the grid, we must kill him (or her?) to move into the grid. We assume that we moving up, down, right, left takes us 1 unit time, and killing a guard takes 1 unit time, too. And we are strong enough to kill all the guards.

    You have to calculate the minimal time to approach Angel. (We can move only UP, DOWN, LEFT and RIGHT, to the neighbor grid within bound, of course.)
     

    Input
    First line contains two integers stand for N and M.

    Then N lines follows, every line has M characters. "." stands for road, "a" stands for Angel, and "r" stands for each of Angel's friend. 

    Process to the end of the file.
     

    Output
    For each test case, your program should output a single integer, standing for the minimal time needed. If such a number does no exist, you should output a line containing "Poor ANGEL has to stay in the prison all his life." 
     

    Sample Input
    7 8 #.#####. #.a#..r. #..#x... ..#..#.# #...##.. .#...... ........
     

    Sample Output

    13

    r到a 每步1秒 遇到x用时2秒

    #include<cstdio>
    #include<cstring>
    #include<queue>
    using namespace std;
    char map[1010][1010];
    int  vis[1010][1010];
    struct node
    {
    	int hang ,lie,step;
    	friend bool operator<(node x,node y)//优先队列 遇到.和x先走.时间比较短
    	{
    		return x.step >y.step ;
    	}
    };
    int main()
    {
    	int n,m;
    	while(scanf("%d%d",&n,&m)!=EOF)
    	{
    		memset(vis,0,sizeof(vis));
    		memset(map,0,sizeof(map));
    			for(int i=0;i<n;i++)
    	{
    		scanf("%s",map[i]);
    	}
    	struct node start;
    	for(int i=0;i<n;i++)
    	{
    		for(int j=0;j<m;j++)
    		{
    			if(map[i][j]=='r')
    			{
    				start.hang =i;
    				start.lie =j;
    				start.step =0;
    				vis[i][j]=1;
    			}
    		}
    	}
    	priority_queue<node>q;
    	q.push(start);
    	int flog=0;
    	while(!q.empty() ) 
    	{
    		struct node tmp=q.top() ;
    		q.pop() ;
    		if(map[tmp.hang][tmp.lie ]=='a')
    		{
    			flog=1;
    			printf("%d
    ",tmp.step );
    			break;
    		}
    		tmp.step ++;
    		struct node tmp2;
    		if(tmp.hang +1<n)
    		{
    			tmp2=tmp;
    			tmp2.hang ++;
    			if(map[tmp2.hang ][tmp2.lie ]!='#'&&vis[tmp2.hang ][tmp2.lie ]==0)
    			{
    					if(map[tmp2.hang ][tmp2.lie ]=='x')
    		            {
    		            	tmp2.step ++;
    	                  	}
    				vis[tmp2.hang ][tmp2.lie ]=1;
    				q.push(tmp2); 
    			}
    		}
    			if(tmp.hang -1>=0)
    		{
    			tmp2=tmp;
    			tmp2.hang --;
    			if(map[tmp2.hang ][tmp2.lie ]!='#'&&vis[tmp2.hang ][tmp2.lie ]==0)
    			{
    				vis[tmp2.hang ][tmp2.lie ]=1;
    					if(map[tmp2.hang ][tmp2.lie ]=='x')
    		          {
    		            	tmp2.step ++;
    		            }
    				q.push(tmp2); 
    
    			}
    		}
    			if(tmp.lie +1<m)
    		{
    			tmp2=tmp;
    			tmp2.lie ++;
    			if(map[tmp2.hang ][tmp2.lie ]!='#'&&vis[tmp2.hang ][tmp2.lie ]==0)
    			{
    				vis[tmp2.hang ][tmp2.lie ]=1;
    					if(map[tmp2.hang ][tmp2.lie ]=='x')
    	               	{
    		                	tmp2.step ++;
    		             }
    				q.push(tmp2); 
    			}
    		}
    			if(tmp.lie -1>=0)
    		{
    			tmp2=tmp;
    			tmp2.lie --;
    			if(map[tmp2.hang ][tmp2.lie ]!='#'&&vis[tmp2.hang ][tmp2.lie ]==0)
    			{
    				vis[tmp2.hang ][tmp2.lie ]=1;
    					    if(map[tmp2.hang ][tmp2.lie ]=='x')
    	                    	{
    			             tmp2.step ++;
    		                    }
    				q.push(tmp2); 
    			}
    		}
    	}
    	if(flog==0)
    	{
    		printf("Poor ANGEL has to stay in the prison all his life.
    ");
    	}
    	}
    
    	return 0;
     } 


  • 相关阅读:
    SpringBoot2.x异步任务EnableAsync
    SpringBoot 整合thymeleaf
    SpringBoot 整合freemarker
    RabbitMQ的安装及入门使(Windows)
    jacoco-统计代码覆盖率并生成报告
    Spring Transactional
    [转]IIS7.5优化--提高线程数来适应高并发
    系统设计时考虑
    设计模式之策略模式
    接到一个新需求后的处理流程
  • 原文地址:https://www.cnblogs.com/kingjordan/p/12027114.html
Copyright © 2011-2022 走看看