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学习笔记_180706_接口实现、类继承、多态、转型
    poj 1279 求半平面交的 面积(推荐)
    半平面交 模板 poj 3335 poj 3130 poj 1474 判断半平面交是否为空集
    高斯消元 zoj 3645 poj 1222/XOR消元
    向量旋转专题
    hdu 1524 A Chess Game SG函数(有向无环图-拓扑图)博弈 (二维) + dfs(模板)
    HDOJ1079&POJ1082&ZOJ1024 Calendar Game [找规律博弈]
    转 博弈类题目小结(hdu,poj,zoj)
    poj 3080 get_next + kmp + 字符数组做函数参数 + 数组下标从1 开始
    poj Area 1265 求面积+ 多边形边上的点的个数+ 多边形内点个数
  • 原文地址:https://www.cnblogs.com/kingjordan/p/12027114.html
Copyright © 2011-2022 走看看