现在的位置: 首页 > 综合 > 正文

zoj1649 BFS

2013年01月28日 ⁄ 综合 ⁄ 共 2327字 ⁄ 字号 评论关闭

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

 

        因为普通的BFS是按照步数优先来求解的,但对于本题来说,步数最少的解确不一定是最优解。如:

2 5

axxxr

.....

显然按照普通的BFS求解的话,结果是7(即笔直向左走到头),但最优解应该是6。为了解决这个矛盾,我们可以另设一个数组,记录从起点走到当前位置的最少时间,然后在BFS的过程中,只要从当前位置走到下一个位置的时间小于下一个位置的最小时间,就入队。此处还可以做一个优化,为了避免同一个节点重复入队,我们用优先队列,按照时间由小到大的顺序扩展。

#include <iostream>
#include<cstdio>
#include<queue>

using namespace std;
struct st
{
    int x,y;
    int time;
    bool operator<(const st t)const
    {
        return time>t.time;
    }
}cur,next;
char map[205][205];
int mt[205][205];//记录最少时间
int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
int n,m;

bool path(int x,int y)
{
    if(x>=0&&x<n&&y>=0&&y<m&&map[x][y]!='#')
        return true;
    return false;
}

int BFS(int sx,int sy)
{
    priority_queue<st>q;
    cur.x=sx;
    cur.y=sy;
    cur.time=0;
    mt[sx][sy]=0;
    q.push(cur);
    while(!q.empty())
    {
        cur=q.top();
        q.pop();
        if(map[cur.x][cur.y]=='a')
            return cur.time;
        for(int i=0;i<4;i++)
        {
            next.x=cur.x+dir[i][0];
            next.y=cur.y+dir[i][1];
            next.time=cur.time+1;
            if(map[next.x][next.y]=='x')
                next.time++;
            if(path(next.x,next.y)&&next.time<mt[next.x][next.y])
            {
                mt[next.x][next.y]=next.time;
                q.push(next);
            }
        }
    }
    return -1;
}

int main()
{
    int i,j,sx,sy;
    while(~scanf("%d%d",&n,&m))
    {
        for(i=0;i<n;i++)
            scanf("%s",map[i]);
        for(i=0;i<n;i++)
            for(j=0;j<m;j++)
            {
                if(map[i][j]=='r')
                {
                    sx=i;
                    sy=j;
                }
                mt[i][j]=100000000;
            }
        int res=BFS(sx,sy);
        if(res==-1)
            printf("Poor ANGEL has to stay in the prison all his life.\n");
        else
            printf("%d\n",res);
    }
    return 0;
}

 

抱歉!评论已关闭.