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;
     } 


  • 相关阅读:
    Java 数据库操作oracle增删改查,通用封装基于hashmap
    Python 自动化paramiko操作linux使用shell命令,以及文件上传下载linux与windows之间的实现
    Java利用 ganymedssh2build.jar来上传文件到linux以及下载linux文件以及执行linux shell命令
    Java Calendar and SimpleDateFormat 时间模块
    Java 读取properties
    Java java httpclient4.5 进行http,https通过SSL安全验证跳过,封装接口请求 get,post(formdata,json)封装,文件上传下载
    Python 基于request库的get,post,delete,封装
    更法第一 (zz)
    北京将投资707亿元建三条地铁新线 (zz.IS2120@BG57IV3)
    fgetws 讀取Unicode文件 (zz.IS2120@BG57IV3)
  • 原文地址:https://www.cnblogs.com/kingjordan/p/12027114.html
Copyright © 2011-2022 走看看