码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 1242 Rescue (BFS+优先队列)

时间:2014-07-14 22:42:13      阅读:242      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   java   color   strong   

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 0x3f3f3f3f
using 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;
}

 

   

hdu 1242 Rescue (BFS+优先队列),布布扣,bubuko.com

hdu 1242 Rescue (BFS+优先队列)

标签:des   style   blog   java   color   strong   

原文地址:http://www.cnblogs.com/fengxmx/p/3842283.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!