HDU-1242 Rescue

来源:互联网 发布:php入门教学 编辑:程序博客网 时间:2024/05/22 08:21

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