HDU 1242 Rescue【BFS+优先队列】

Rescue

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


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”出发,此题可以用回溯法进行深搜,但那样做的话,效率还是不能让人满意,但是广搜的话,由于入队后每次出队时,根据地
图情况的不同,出队元素所记忆的时间并不是层次递增的,因此使用简单广搜的话,同样需要全部搜索才能找到正确答案。有没有一种方法能
让某一步因为遇到士兵而多花时间的结点在队列中向后推迟一层出队呢?答案是肯定的,在这里我们可以用优先队列来实现,总体思想上是,
根据时间进行优先性选择,每次都要出队当前队列元素中记录时间最少的出队,而入队处理时,我们可以按顺序对四个方向上的各种情况按正
常处理入队就行了,出队顺序由优先队列根据预设优先性自动控制。

当到达X点时,时间+2 排在队列最后,时间较小的那部分(时间+1的部分)先执行,然后在执行+2的部分



#include
#include
#include
#include
#include
using namespace std;
struct node
{
    int x,y,num;
    friend bool operator <(const node &a,const node &b)//优先队列
    {
        return a.num>b.num;
    }
};
int flag;
int n,m;
char map[220][220];
int step[4][2]= {{-1,0},{1,0},{0,-1},{0,1}};
int vis[220][220];

int BFS(int x,int y);
int main (void)
{
    int ax,ay;
    while(~scanf("%d%d",&n,&m))
    {
        flag=0;
        getchar();//找了很久WAWA 
        for(int i=0; iq; //定义优先队列
    memset(vis,0,sizeof(vis));
    point.x = x;
    point.y = y;
    point.num=0;
    vis[point.x][point.y] = 1;
    q.push(point);
    while(!q.empty())
    {
        point = q.top();//top优先队列的顶部元素(下一个元素 相当于普通队列的front)
        q.pop();
        if(map[point.x][point.y]=='r')
        {
            flag=1;
            return point.num;
        }
        for(int i=0; i<4; i++)
        {
            newpoint.x = point.x + step[i][0];
            newpoint.y = point.y + step[i][1];
            if(newpoint.x>=0&&newpoint.y>=0&&newpoint.x


你可能感兴趣的:(ACM__BFS,ACM解题报告)