首页 > 代码库 > zoj 1649 Rescue (bfs+队列)

zoj 1649 Rescue (bfs+队列)

Rescue

Time Limit: 2 Seconds      Memory Limit: 65536 KB

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"stdio.h"
#include"string.h"
#include"queue"
#include"algorithm"
using namespace std;
#define N 205
#define inf 100000000
int ei,ej,n,m;  //目标位置和行列范围
int dir[4][2]={0,1,0,-1,-1,0,1,0};
int mark[N][N];  //记录走到当前位置所用最少时间
char g[N][N];
struct node
{
    int x,y,t;
};
int bfs(int x,int y)
{
    int i;
    queue<node >q;
    node cur,next;
    cur.x=x;
    cur.y=y;
    cur.t=0;
    q.push(cur);
    mark[x][y]=0;
    while(!q.empty())
    {
        cur=q.front();
        q.pop();
        for(i=0;i<4;i++)
        {
            next.x=x=dir[i][0]+cur.x;
            next.y=y=dir[i][1]+cur.y;
            if(x<0||x>=n||y<0||y>=m||g[x][y]=='#')
                continue;
            next.t=cur.t+1;
            if(g[x][y]=='x')
                next.t++;
            if(next.t<mark[x][y])
            {                
                mark[x][y]=next.t;
                q.push(next);
            }
        }
    }     //因为没用优先队列,故只有队列空之后才能得到最优解
    if(mark[ei][ej]!=inf)
        return mark[ei][ej];
    return -1;
}
int main()
{
    int i,j,si,sj;
    while(scanf("%d%d",&n,&m)!=-1)
    {
        for(i=0;i<n;i++)
        {
            scanf("%s",g[i]);
            for(j=0;j<n;j++)
            {
                mark[i][j]=inf;  //初始化为无限大
                if(g[i][j]=='a')
                {
                    si=i;sj=j;
                }
                if(g[i][j]=='r')
                {
                    ei=i;ej=j;
                }
            }
        }
        int t=bfs(si,sj);
        if(t==-1)
            printf("Poor ANGEL has to stay in the prison all his life.\n");
        else
            printf("%d\n",t);
    }
    return 0;
}