rescue(广搜)

题目:https://vjudge.net/contest/240161#problem/A 

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

这是一道广搜的题,直接敲板子就可以过的。

#include <iostream>
#include <queue>
#include <string.h>
#define MAXN 205
using namespace std;
int N,M;
bool visit[MAXN][MAXN];
char map[MAXN][MAXN];
int dir[4][2]={0,1,0,-1,
                      1,0,-1,0};
struct State
{
    int x,y;
    int step;
};
bool checkstate(State s)
{
    if((s.x>0&&s.x<N+1)&&(s.y>0&&s.y<N+1)&&!visit[s.x][s.y]&&map[s.x][s.y]!='#')
        return 1;
    else return 0;
}
int  BFS(State st)
{
    memset(visit,0,sizeof(visit));
    queue<State>Q;
    State now,next;
    st.step=0;
    Q.push(st);
    visit[st.x][st.y]=1;
    while(!Q.empty())
    {
        now=Q.front();
        if(map[now.x][now.y]=='a')
        {
            return now.step ;
        }
        for(int i=0;i<4;i++)
        {
            next.x=now.x+dir[i][0];
            next.y=now.y+dir[i][1];
            next.step=now.step+1;
            if(map[next.x][next.y]=='x')
            {
                map[next.x][next.y]='.';
                next.step+=1;
            }
            if(checkstate(next))
            {
                Q.push(next);
                visit[next.x][next.y]=1;
            }
        }
        Q.pop();
    }
    return 0;
}
int main()
{
    while(cin>>N>>M)
    {
        State st;
        for(int i=1;i<=N;i++)
            for(int j=1;j<=M;j++)
            {
                cin>>map[i][j];
                if(map[i][j]=='r')st.x=i,st.y=j;
            }
            int ans=BFS(st);
            if(ans)
                cout<<ans<<endl;
            else cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/guagua_de_xiaohai/article/details/81166019
今日推荐