首页 > 代码库 > hdu 1242 Rescue (BFS+优先队列)

hdu 1242 Rescue (BFS+优先队列)

Rescue

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


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
 

  

        题意:就是天使的朋友要把天使从监狱里面救出来,怎样花费的时间最少,a代表天使,r代表天使的朋友,然后x代表怪物,天使的朋友可以杀掉怪物,但要花费更多的时间要花时间2,而#代表墙,.代表可以走的路,要花费时间1。要你求出可以救出天使的最少时间,若不能则输出-1.

        解题思路:按照我自己的思路的话,如果不用优先队列的话,按BFS搜索的话,最先找到的,不一定是最短的,而且最先找到的它走过的路都被标记了,就导致了真正最短有可能走它走过了的不能再走了,就这样我贡献了几次WA了。然后如果我们用优先队列的话,每走一步,所话的最小步数都保持在队列了头部,这样就可以保证所找到的步数一定是最小的。还有一个提示:队取队首是:q.front();而优先队列取队首是:q.top();下次自己记住吧。嘿嘿。

 

贴出代码:

 

#include <stdio.h>#include <queue>#define maxn 0x3f3f3f3fusing namespace std;char map[205][205];int dir[4][2]={-1, 0, 0, -1, 1, 0, 0, 1};     //上下左右四个方向int visited[205][205];    //标记是否走过struct node          //优先队列,步数小的在前{    int x, y;    int num;    friend bool operator < (node a, node b)    {        return a.num >b.num;    }};int BFS(int n, int m, int startx, int starty)     //BFS搜索,先将起点入队,然后依次判断它的上下左右是否可以入队,若可以则入队{    priority_queue <node> Q;                  struct node Node, mark;    Node.x = startx;    Node.y = starty;    Node.num = 0;    visited[startx][starty] = 1;    Q.push(Node);    while(!Q.empty())    {        Node = Q.top();        Q.pop();        for(int i = 0; i<4; i++)        {            mark = Node;            mark.x += dir[i][0];   mark.y += dir[i][1];            if(mark.x>=1 && mark.x<=n && mark.y>=1 && mark.y<=m && !visited[mark.x][mark.y])                 {                visited[mark.x][mark.y] = 1;                if(map[mark.x][mark.y] == a)     //若首先找到终点则返回                {                    mark.num++;                    return mark.num;                }                else if(map[mark.x][mark.y] == .)                {                    mark.num++;                    Q.push(mark);                }                else if(map[mark.x][mark.y] == x)                {                    mark.num += 2;                    Q.push(mark);                }            }        }    }    return maxn;}int main(){    int n, m, s;    int startx, starty;    while(scanf("%d%d", &n, &m)!=EOF)    {        for(int i = 1; i<=n; i++)        {            getchar();            for(int  j = 1; j<=m; j++)            {                visited[i][j] = 0;                scanf("%c", &map[i][j]);                if(map[i][j] == r)                {                    startx = i;    starty = j;                }            }        }        s = BFS(n, m, startx, starty);        if(s != maxn)            printf("%d\n", s);        else            printf("Poor ANGEL has to stay in the prison all his life.\n");    }    return 0;}