2020 BIT冬训-图&&DFS&&BFS B - Rescue HDU - 1242(BFS,优先队列)

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.)

一个N * M (N, M <= 200)的监狱,在监狱中有墙、路和警卫。

现在Angel被困在了监狱中,你需要去拯救她。如果你走到了有警卫的点,那么必须杀掉该点的警卫才能顺利通过该点。我们假设你可以移动到上、下、左、右相邻点且所需的时间为1秒,杀掉警卫的时间也为1秒,而且你的能力足够去杀掉监狱中的所有警卫。

你很想快点救出Angel,所以请计算出你救出她所需要的最少时间是多少。

 

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.
第一行有两个整数N和M。

 接下来N行,每一行M个字符,”.”表示通路,”a”表示Angel所在的位置,并且”r”代表你所在的位置,”x”表示警卫所在的位置

 

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."

对于每组样例,输出一个整数,表示你能救出Angel所需的最少时间。如果没有这样的整数可以求出,你要输出"Poor ANGEL has to stay in the prison all his life."

 

Sample Input

7 8
#.#####.
#.a#..r.
#..#x...
..#..#.#
#...##..
.#......
........

Sample Output

13
本题的思路是BFS加上优先队列。
不能用函数递归的原因是可能会无限循环或者无法找到最少时间。
重写(忘记是不是这个名词了)struct的优先级。再辅以优先队列令时间最少的先出列。
这样下一步求出的点的时间就是该点的最短时间。且在将该点压入队列后令vis为1表示已经确认该点(避免重复)。
AC代码如下:
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
int n,m,flag;
char st[202][202];
int vis[202][202];
int to[4][2]={-1,0,0,-1,1,0,0,1};
struct node{
    int x,y,step;
    bool operator <(const node & n)const{
        return n.step<step;
    }
};
void bfs(int x,int y){
    int mi;
    vis[x][y]=1;
    priority_queue<node>q;
    node u,v;
    u.x=x,u.y=y,u.step=0;
    q.push(u);
    while(!q.empty()){
        u=q.top();
        if(st[u.x][u.y]=='a'){
            flag=1;
            mi=u.step;
            break;
        }
        q.pop();
        for(int i=0; i<4; i++){
            int ex=u.x+to[i][0];
            int ey=u.y+to[i][1];
            if(ex<0||ex>=n||ey<0||ey>=m||st[ex][ey]=='#'||vis[ex][ey]==1)
                continue;
            if(st[ex][ey]=='.'||st[ex][ey]=='a')
                v.step=u.step+1;
            if(st[ex][ey]=='x')
                v.step=u.step+2;
            v.x=ex,v.y=ey;
            vis[ex][ey]=1;
            q.push(v);
        }
    }
    if(flag)
        printf("%d\n",mi);
    else
        printf("Poor ANGEL has to stay in the prison all his life.\n");
}
int main(){
    int sx,sy;
    while(~scanf("%d %d",&n,&m)){
        memset(vis,0,sizeof(vis));
        flag=0;
        for(int i=0; i<n; i++)
            scanf("%s",st[i]);
        for(int i=0; i<n; i++){
            for(int j=0; j<m; j++){
                if(st[i][j]=='r'){
                    sx=i;
                    sy=j;
                    break;
                }
            }
        }
        bfs(sx,sy);
    }
    return 0;
}

 

 
posted @ 2021-02-24 20:04  mikku  阅读(53)  评论(0)    收藏  举报