HDU 1242 Rescue

Rescue

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 16221    Accepted Submission(s): 5892


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
                                                                                                                                                                                          此题要求用最短时间救出Angel,而且监狱里有警卫(有任意多个), 因此不能单纯的用bfs。可以用数组记录走到这点所用的时间   ,如果有用更少时间的路径,则更新时间及路径,由于路径的多可能性,因此不用标记,由时间的更新控制循环,当时间及路径不能再更新时,则终止。




#include
    
    
     
     
#include
     
     
      
      
#include
      
      
       
       

using namespace std;

char map[300][300];
int x1,x2,y1,y2,m,n;
int mintime[300][300];
int dx[] = {0,0,-1,1};
int dy[] = {-1,1,0,0};
int len[300][300];

struct node
{
	int x,y,t;
	node(){
	}
	node(int x,int y,int t){
		this->x = x;
		this->y = y;
		this->t = t;
	}
}q[300*300];

void bfs(int x,int y)
{
    int begin = 0,end = 0;
    q[end++] = node(x,y,0);
    len[x][y] = 0;
    node t1,t2;
    while(begin < end)
    {
     	node now = q[begin++];
     	t2.t = t1.t = now.t;
     	for(int i = 0;i < 4;i++)
     	{
     		int xx = now.x + dx[i];
     		int yy = now.y + dy[i];
     		t1.t=t2.t;
     		if(xx < 1 || xx > n|| yy < 1 || yy > m || map[xx][yy] == '#')
                continue;
     		if(map[xx][yy] == '.' || map[xx][yy] == 'a')
     			t1.t++;
            else if(map[xx][yy] == 'x')
                t1.t += 2;
            if(mintime[xx][yy] == -1)
                mintime[xx][yy] = 100000;
            if(t1.t < mintime[xx][yy]){
                mintime[xx][yy] = t1.t;
                len[xx][yy] = len[now.x][now.y]+1;
                q[end++] = node(xx,yy,t1.t);
            }   
     	}
    }
    if(len[x2][y2]&&mintime[x2][y2] != -1)
     	printf("%d\n",mintime[x2][y2]);
    else printf("Poor ANGEL has to stay in the prison all his life.\n");
}
int main()
{
   while(cin>>n>>m)
   {
        int i,j;
        for(i = 1;i <= n;i++)
            for(j = 1;j <= m;j++){
                cin>>map[i][j];
   	  		    if(map[i][j] == 'a'){
   	  			    x2 = i;
   	  			    y2 = j;
   	  		    }
   	  		    if(map[i][j] == 'r'){
   	  			   x1 = i;
   	  			   y1 = j;
   	  		}
   	  	}
    memset(mintime,-1,sizeof(mintime));
    bfs(x1,y1);
   }
}

      
      
     
     
    
    
同样也可以用优先队列做
#include
    
    
     
     
#include
     
     
      
      
#include
      
      
       
       
#include
       
       
        
        

using namespace std;

char map[300][300];
int x1,x2,y1,y2,m,n;
int dx[] = {0,0,-1,1};
int dy[] = {-1,1,0,0};
int len[300][300];

struct node
{
    int x,y,t;
    /*重载,令优先队列从小到大排*/
    friend bool operator < (const node a,const node b)
    {
        return a.t > b.t;
    }
};

priority_queue
        
        
          q; void bfs(int x,int y) { while(!q.empty()) q.pop(); node s,now; now.x = x; now.y = y; now.t = 0; q.push(now); while(!q.empty()) { now = q.top(); if(now.x == x2 && now.y == y2){ cout< 
         
           < 
          
            = n || s.y < 0 || s.y >= m) continue; if(map[s.x][s.y] == '#') continue; if(map[s.x][s.y] == 'x') s.t = now.t + 2; else s.t = now.t + 1; if(len[s.x][s.y] == -1) len[s.x][s.y] = 999999; if(s.t < len[s.x][s.y]) { len[s.x][s.y] = s.t; q.push(s); } } } cout<<"Poor ANGEL has to stay in the prison all his life."< 
           
             >n>>m) { memset(len,-1,sizeof(len)); for(int i = 0; i < n; i++) for(int j = 0; j < m ;j++) { cin>>map[i][j]; if(map[i][j] == 'r'){ x1 = i; y1 = j; map[i][j] = '.'; } if(map[i][j] == 'a'){ x2 = i; y2 = j; map[i][j] = '.'; } } bfs(x1,y1); } } 
            
           
          
        
       
       
      
      
     
     
    
    
                                                                                                   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值