HDU-1242 Rescue

Rescue

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


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
 

Author
CHEN, Xue
 

Source

ZOJ Monthly, October 2003


题意是 在某范围下的矩阵图中 r表示起点 a 表示中点 x表示守卫 需要多花费一个时间 ‘.’表示空地 #表示墙壁

问在多个起点的情况下 最快到达终点的时间是多少 四个方向 每走一步花费1时间点


题目需要注意的是有多个起点 那么而且如果最短路径中遇到x还需要多花费1的时间点 那么通常情况下的最短路径就不一定是花费时间

最小的点了   因为最短路径下走的点可能有多个x  从而导致时间增加 而非最短路径中可能有很少的x从而导致非最短路径下的路径花费时间最少‘

所以这里我们就需要一个优先队列  排序按照花费时间  越小的越往前 这样我们最后求的就不是最短路径 而一步步扩展的路径都是时间最少的路径

然后不断地把每一个起点都试一遍 找出最小的那个就可以了

需要注意的是 若是找不到 a  输出的那句话中有个句号。。。因此WR了好久。。。- -


AC code:

#include<bits/stdc++.h>
using namespace std;
const int inf = 999999999;
int dir[][2]={{-1,0},{1,0},{0,-1},{0,1}};
struct node
{
    int x,y;
    int t;
};
bool operator <(const node &a,const node &b)
{
    return a.t>b.t;    
} 
bool bok[410][410];
char p[410][410]; 
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    { 
        int sx,sy;
        queue<node>s; 
        priority_queue<node>q;
        node a,b;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                cin>>p[i][j];
                if(p[i][j]=='r'){
                    a.x=i;
                    a.y=j;
                    a.t=0;
                    //q.push(a);
					s.push(a);
                }    
            } 
        } 
        //cout<<sx<<" "<<sy<<endl;
        int mm=inf;
	    while(s.size())
		{
            a = s.front();s.pop();
			memset(bok,0,sizeof(bok));
            bok[a.x][a.y]=1;
            q.push(a);
            while(q.size())
            {
                a = q.top();q.pop();
                if(p[a.x][a.y]=='a')
                {
                    mm=min(mm,a.t); 
                    break;
                }
                for(int i=0;i<4;i++)
                {
                    int tx,ty;
                    tx = a.x+dir[i][0];
                    ty = a.y+dir[i][1];
                    if(tx>0&&tx<=n&&ty>0&&ty<=m&&!bok[tx][ty]&&p[tx][ty]!='#')
                    {
                        b.x=tx,b.y=ty;
                        bok[tx][ty]=1;
                        if(p[tx][ty]=='x'){
                            b.t=a.t+2;
                        //    p[tx][ty]='.';
                        }
                        else b.t=a.t+1;
                        //if(mm==5)cout<<a.x<<" "<<a.y<<" "<<a.t<<endl;        
                        q.push(b);
                    }
                }
            }       
    	}
    	if(mm==inf)cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;
	    else cout<<mm<<endl;
	}
    return 0;
} 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值