hdu1242 Rescue BFS广搜 + 优先队列

Rescue
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 21706    Accepted Submission(s): 7747


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
 

Recommend
Eddy   |   We have carefully selected several similar problems for you:  1072 1372 1548 1026 1180

BFS广搜 + 优先队列

  题意:从前有一名天使被囚禁了,天使的朋友要去救他,你的任务是输出最短的救援时间。天使的朋友每秒可以走一步,地牢中有守卫,当你遇到守卫的时候需要停留一秒杀死守卫。给你地牢的地图,上面有几种元素,'.'表示路,可以通行。'#'代表墙,无法通行。'r'表示天使的朋友,代表起点。'a'表示天使的位置,代表终点。'x'表示守卫的位置。

  思路:因为是求“最短路径”的问题,正好用广搜可以解决。但是与普通广搜不同的是,遇到守卫的时候会多停留一秒。这就会导致队列中优先级的不稳定,所以需要用优先队列,让优先级最高的节点始终在队列最前面

#include <iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
int n,m;
char map[205][205];
int visit[205][205];
int dir[4][2]= {0,1,0,-1,1,0,-1,0};
int ax,ay;
struct nodes
{
    int x,y,t;
};
nodes node1,node2;
bool operator<(nodes a,nodes b)
{
    return a.t>b.t;//从小往大排
}
priority_queue<nodes> q;
int bfs()
{
    while(!q.empty())
        q.pop();
    node1.x=ax;
    node1.y=ay;
    node1.t=0;
    visit[ax][ay]=1;
    q.push(node1);
    while(!q.empty())
    {
        node1=q.top();
        q.pop();
        if(map[node1.x][node1.y]=='a')
        {
            return node1.t;
        }

        for(int i=0; i<4; i++)
        {
            node2.x=node1.x+dir[i][0];
            node2.y=node1.y+dir[i][1];
            if(node2.x>=0&&node2.x<n&&node2.y>=0&&node2.y<m&&map[node2.x][node2.y]!='#'&&!visit[node2.x][node2.y])
            {
                visit[node2.x][node2.y]=1;
                if(map[node2.x][node2.y]=='x')
                    node2.t=node1.t+2;
                else
                    node2.t=node1.t+1;
                q.push(node2);
            }
        }
    }
    return -1;
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        memset(visit,0,sizeof(visit));
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<m; j++)
            {
                cin>>map[i][j];
                if(map[i][j]=='r')
                {
                    ax=i;
                    ay=j;
                }

            }
        }
        int ans=bfs();
        if(ans==-1)
            printf("Poor ANGEL has to stay in the prison all his life.
");
        else
            printf("%d
",ans);
    }

}
原文地址:https://www.cnblogs.com/dshn/p/4742303.html