您的位置:首页 > 产品设计 > UI/UE

ZOJ第一题_1649Rescue(优先队列下的BFS)

2013-10-07 10:01 281 查看
ZOJ Problem Set - 1649

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

一遍AC。。。



#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <queue>

using namespace std;

const int maxn=205;
char map[maxn][maxn];
bool vis[maxn][maxn];

struct Node
{
    int x;
    int y;
    int step;
};

struct cmp
{
    bool operator()(Node &X,Node &Y)
    {
        return X.step>Y.step;
    }
};
priority_queue<Node,vector<Node>,cmp>q;
int dx[]={-1,0,1,0};
int dy[]={0,-1,0,1};

int N,M;
int sx,sy,ex,ey;

bool OK(Node cur)
{
    if(cur.x<0||cur.x>=N||cur.y<0||cur.y>=M)
        return false;
    return true;
}

void ReadIn()
{
    int i,j;
    for(i=0;i<N;i++)
    {
        for(j=0;j<M;j++)
        {
            cin>>map[i][j];
            if(map[i][j]=='r')
            {
                sx=i;
                sy=j;
            }
            if(map[i][j]=='a')
            {
                ex=i;
                ey=j;
            }
        }
    }
}

int BFS()
{
    int i;
    Node first,next,cur;
    while(!q.empty())
        q.pop();
    memset(vis,0,sizeof(vis));
    first.x=sx;
    first.y=sy;
    first.step=0;
    vis[first.x][first.y]=true;
    q.push(first);
    while(!q.empty())
    {
        cur=q.top();
        q.pop();

        if(cur.x==ex&&cur.y==ey)
        return cur.step;

        for(int i=0;i<4;i++)
        {
            next.x=cur.x+dx[i];
            next.y=cur.y+dy[i];

            if(OK(next)&&!vis[next.x][next.y]&&map[next.x][next.y]!='#')
            {
                vis[next.x][next.y]=true;
                if(map[next.x][next.y]=='.')
                {
                    next.step=cur.step+1;
                    q.push(next);
                }
                else if(map[next.x][next.y]=='a')
                {
                    next.step=cur.step+1;
                    q.push(next);
                }
                else if(map[next.x][next.y]=='x')
                {
                    next.step=cur.step+2;
                    q.push(next);
                }
            }
        }
    }
    return -1;
}

int main()
{
    int i;
    while(scanf("%d%d",&N,&M)!=EOF)
    {
        ReadIn();
        int ans=BFS();
        if(ans==-1)
            printf("Poor ANGEL has to stay in the prison all his life.\n");
        else
            printf("%d\n",ans);
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: