hduoj1242 Rescue (bfs暴搜)

Rescue

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

思路:

这道题没啥好说的,就是刚(bfs暴搜),注意剪枝就行了

代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<map>
#include<set>
#include<vector>
#include<string>
#include<cstring>
#include<stack>
#include<queue>
#define ll long long
using namespace std;
const int maxn = 200 + 10;
char str[maxn][maxn];
//vector G[maxn];
bool vis[maxn][maxn];
int n,m;


struct node
{
    int x,y,step;
    friend bool operator < (node a,node b)
    {
        return a.step > b.step;
    }
};

int dx[4][2] = {1,0,-1,0,0,1,0,-1};
void bfs(int x1,int y1,int x2,int y2)
{
    int ans = -1;
    memset(vis,0,sizeof(vis));
    priority_queue<node> q;
    node p,p2;
    p.step = 0,p.x = x1,p.y = y1;
    q.push(p); 
    vis[p.x][p.y] = 1;
    while (!q.empty())
    {
        p = q.top();
        if (p.x == x2 && p.y == y2)
        {
            ans = p.step;
            break;
        }
        q.pop();
        for (int i = 0;i < 4;i ++)
        {
            p2.x = p.x + dx[i][0],p2.y = p.y + dx[i][1];
            if (vis[p2.x][p2.y]) continue;
            if (p2.x < 0 || p2.x >= n || p2.y < 0 || p2.y >= m) continue;
            if (str[p2.x][p2.y] == '#') continue;
            if (str[p2.x][p2.y] == '.' || str[p2.x][p2.y] == 'r') p2.step = p.step + 1;
            else if (str[p2.x][p2.y] == 'x') p2.step = p.step +  2;
            vis[p2.x][p2.y] = 1;
            q.push(p2);
        }
    }
    if (ans == -1) puts("Poor ANGEL has to stay in the prison all his life.");
    else printf("%d\n",ans);
    
}


int main()
{
    int bx,by,ex,ey;
    while (~scanf("%d %d",&n,&m))
    {
        for (int i = 0;i < n;i ++)
            scanf("%s",str[i]);
        for (int i = 0;i < n;i ++)
            for (int j = 0;j < m;j ++)
            {
                if (str[i][j] == 'a')
                    bx = i,by = j;
                if (str[i][j] == 'r')
                    ex = i,ey = j;
            }
        bfs(bx,by,ex,ey);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/cloudy_happy/article/details/81322233