HDU 1242 Rescue --BFS+重载优先队列

来源:互联网 发布:西安java培训多少钱 编辑:程序博客网 时间:2024/06/06 12:33
Rescue
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 31933 Accepted Submission(s): 11152


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

要点: 我已开始的思路是利用bfs 把所有的可能的情况全部跑出来,利用min函数留住最小的那个,但是忽略了一个重要的地方,bfs的方向是随机的,但是进队列确实有顺序的,如果自己的命不好,很容易因为先进队列的那个把四周标记之后,但是后进队列的那个却因为前面的吧这个点给占用了(vis标记数组变为了1)而不能走那条路,使得最段路发生变化!!!!!

如果利用优先队列,并且对优先队列进行重载,这样的话,每次都是最短的陷入队列,保证了最短的那个可以在四个方向上任意行走(不会因为前面的占路)

#include<iostream>#include <cstdio>#include <cstring>#include <queue>#include <algorithm>using namespace std;struct node{    int x;    int y;    int step;} a,b,s[100000];char map[450][450];int vis[450][450];int dx[4]= {1,-1,0,0};int dy[4]= {0,0,1,-1};int n,m,sx;struct cmp{    bool operator()(const node a, const node b)const    {        return a.step%10 >b.step%10;    }};int  bfs(int x, int y){    memset(vis,0,sizeof(vis));    int min=99999999;    priority_queue<node, vector<node>,cmp>q;    a.x=x;    a.y=y;    a.step=0;    vis[a.x][a.y]=1;    q.push(a);    while(!q.empty())    {        b=q.top();        q.pop();        int nx=b.x;        int ny=b.y;        if(map[nx][ny]=='a')        {            if(b.step<min)            {                min=b.step;            }            //printf("%d\n",b.step);        }        for(int i=0; i<4; i++)        {            if(nx+dx[i]>=0 && nx+dx[i]<n && ny+dy[i]>=0 && ny+dy[i]<m && !vis[nx+dx[i]][ny+dy[i]])            {                if(map[nx+dx[i]][ny+dy[i]]=='x')                {                    a.x=nx+dx[i];                    a.y=ny+dy[i];                    vis[a.x][a.y]=1;                    a.step=b.step+2;                    q.push(a);                }                else if(map[nx+dx[i]][ny+dy[i]]!='#')                {                    a.x=nx+dx[i];                    a.y=ny+dy[i];                    vis[a.x][a.y]=1;                    a.step=b.step+1;                    q.push(a);                }            }        }    }    return min;    //printf("Poor ANGEL has to stay in the prison all his life.\n");}int main (){    while(cin>>n>>m)    {        sx=0;        memset(vis,0,sizeof(vis));        for(int i=0; i<n; i++)        {            for(int j=0; j<m; j++)            {                cin>>map[i][j];                if(map[i][j]=='r')                {                    s[sx].x=i;                    s[sx].y=j;                    sx++;                }            }        }        int min=99999999;        int ans;        for(int i=0; i<sx; i++)        {            ans=bfs(s[i].x, s[i].y);            if(ans<min)            {                min=ans;            }        }        if(min==99999999) printf("Poor ANGEL has to stay in the prison all his life.\n");        else   printf("%d\n",ans);    }}




原创粉丝点击