ZOJ-1649

--------------------------------------------------------------------------------

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

 ACM网站崩了 没有去试能不能通过 测试结果表示可以

#include <iostream>
#include <queue>
using namespace std;
struct node
{
    int x;
    int y;
    int time;
};
bool operator <(node n1,node n2)
{
    return n1.time>n2.time;
}

int main()
{
    node first,ending,temp,temp_2;
    int line,row,temp_x,temp_y;
    char prison[201][201];
    int repeat[201][201];
    int X[4]={1,-1,0,0};
    int Y[4]={0,0,1,-1};
    priority_queue <node> p;  //使用优先队列
    while(1)
    {
        while(!p.empty())  //清空队列
            p.pop();
        cin>>line>>row;
        for(int i=0;i<line;i++)
            for(int j=0;j<row;j++)
            {
                cin>>prison[i][j];      //输入数据
                repeat[i][j]=0;
            }
        for(int i=0;i<line;i++)
            for(int j=0;j<row;j++)
            {
                if(prison[i][j]=='r')  //找到起点
                {
                    first.x=i;
                    first.y=j;
                    first.time=0;
                    repeat[i][j]=1;
                    p.push(first);
                }
                if(prison[i][j]=='a')    //找到终点
                {
                    ending.x=i;
                    ending.y=j;
                }
            }
        while(!p.empty())
        {
            if(repeat[ending.x][ending.y]==1)
                break;
            temp=p.top();
            p.pop();
            for(int i=0;i<4;i++)
            {
                temp_x=temp.x+X[i];
                temp_y=temp.y+Y[i];
                if(temp_x>=0&&temp_x<line&&temp_y>=0&&temp_y<row&&repeat[temp_x][temp_y]==0)
                {
                    if(prison[temp_x][temp_y]=='a')
                    {
                        repeat[temp_x][temp_y]=1;
                        cout<<temp.time+1<<endl;
                        break;
                    }
                    if(prison[temp_x][temp_y]=='.')
                    {
                        repeat[temp_x][temp_y]=1;
                        temp_2.x=temp_x;
                        temp_2.y=temp_y;
                        temp_2.time=temp.time+1;
                        p.push(temp_2);
                    }
                    if(prison[temp_x][temp_y]=='x')
                    {
                        repeat[temp_x][temp_y]=1;
                        temp_2.x=temp_x;
                        temp_2.y=temp_y;
                        temp_2.time=temp.time+2;
                        p.push(temp_2);
                    }
                }
            }
        }
        if(repeat[ending.x][ending.y]==0)
            cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;
    }


    return 0;
}

超时了无法通过

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值