HDOJ 1242 Rescue(BFS + 优先队列)

Rescue




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
题目大意:天使被抓走了并且被关在一个牢房里,他的朋友们想要去救他。天使的朋友只要能够到达天使所在的地方,就可以解救出他。然而有一些地方有守卫在,我们必须杀掉守卫才可以通过,杀掉守卫需要1分钟,从一个地方移动到另一个地方也需要1分钟,求营救出公主的最短时间。

解题思路:一直以为是一道简单的BFS,所以就用队列做的,结果WA了9次,和别人讨论之后才知道要用优先队列做。由于天使只有一个而朋友可能有很多,所以可以从天使开始BFS离他最近的朋友,如果可以到达朋友所在的位置就打印输出时间,否则输出Poor ANGEL has to stay in the prison all his life.

代码如下:

#include 
#include 
#include 
#include 

using namespace std;

const int maxn = 205;
const int maxm = 205;

typedef struct Node{
    int x;
    int y;
    int t;
}node;

bool operator < (node a,node b)
{
    return a.t > b.t;
}

char maze[maxn][maxm];
int dir[4][2] = {{0,1},{-1,0},{0,-1},{1,0}};

int ax,ay,rx,ry;
int m,n;

void bfs()
{
    priority_queue que;
    //queue que;
    node temp,a;
    a.x = ax;
    a.y = ay;
    a.t = 0;
    maze[a.x][a.y] = '#';
    que.push(a);

    while(que.size()){
        node p = que.top();
        que.pop();
        maze[p.x][p.y] = '#';
        for(int i = 0;i < 4;i++){
            int nx = p.x + dir[i][0];
            int ny = p.y + dir[i][1];
            if(0 <= nx && nx < n && 0 <= ny && ny < m && maze[nx][ny] != '#'){
                if(maze[nx][ny] == 'r'){
                    printf("%d\n",p.t + 1);
                    return ;
                }
                else if(maze[nx][ny] == 'x'){
                    temp.t = p.t + 2;
                }
                else if(maze[nx][ny] == '.'){
                    temp.t = p.t + 1;
                }
                temp.x = nx;
                temp.y = ny;
                que.push(temp);
            }
        }
    }
    printf("Poor ANGEL has to stay in the prison all his life.\n");
}

int main()
{
    while(scanf("%d %d",&n,&m) != EOF){
        for(int i = 0;i < n;i++){
            scanf("%s",maze[i]);
        }
        for(int i = 0;i < n;i++){
            for(int j = 0;j < m;j++){
                if(maze[i][j] == 'a'){
                    ax = i;
                    ay = j;
                    break;
                }
            }
        }
        bfs();
    }
    return 0;
}
/*
在网上找了一份代码,运行下面的数据得到的结果不对,交到OJ上却AC了,奇怪
6 8
########
#.xxxx.#
#......#
#ax....r
rrrrrrrr
rrrrrrrr
*/


你可能感兴趣的:(搜索)