hdu 1242

15 篇文章 0 订阅
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

【题意】你现在在监狱里面,你想要营救你的伙伴。但是你的伙伴被关押了,所以只能是你去见他,监狱里面自然是有狱警的,因为你是劫狱的,所以你要干掉狱警,现在你很厉害,没有一个狱警比你强,但是干掉狱警还是要花时间的,现在的你只能上下左右的移动,狱警和你的伙伴不能移动,你每次移动都需要一分钟,但是干掉狱警也需要一分钟,问你救出朋友需要的最少时间,如果就不出来就输出Poor ANGEL has to stay in the prison all his life.

【分析】简单的搜索题,就是给某些地方一个不一样的权值而已,加一个复合判断就行。

【代码】

#include <bits/stdc++.h>

using namespace std;
struct P
{
    int x,y,val;
};
char mat[205][205];
int val[205][205];
int changex[]= {0,0,1,-1};
int changey[]= {1,-1,0,0};
int c,d,start_x,start_y,end_x,end_y;
bool in_map(int x,int y)//防止越界
{
    if(x<0||x>=c||y<0||y>=d)
        return false;
    return true;
}
int main()
{
    while(~scanf("%d%d",&c,&d))//注意是多组输入
    {
        memset(val,0x3f3f3f3f,sizeof(val));
        for(int i=0; i<c; i++)
            for(int j=0; j<d; j++)
            {
                scanf(" %c",&mat[i][j]);
                if(mat[i][j]=='r')
                {
                    start_x=i;
                    start_y=j;
                }
                else if(mat[i][j]=='a')
                {
                    end_x=i;
                    end_y=j;
                }
            }
        P a,b;
        a.x=start_x;
        a.y=start_y;
        val[a.x][a.y]=0;
        a.val=0;
        queue<P> ans;
        ans.push(a);
        while(!ans.empty())
        {
            a=ans.front();
            ans.pop();
            for(int i=0; i<4; i++)
            {
                b.x=a.x+changex[i];
                b.y=a.y+changey[i];
                if(!in_map(b.x,b.y))
                    continue;
                if(mat[b.x][b.y]=='#')
                    continue;
                if((mat[b.x][b.y]=='a'||mat[b.x][b.y]=='.')&&val[b.x][b.y]>a.val+1)
                {
                    val[b.x][b.y]=a.val+1;
                    b.val=a.val+1;
                    ans.push(b);
                }
                else if(mat[b.x][b.y]=='x'&&val[b.x][b.y]>a.val+2)
                {
                    val[b.x][b.y]=a.val+2;
                    b.val=a.val+2;
                    ans.push(b);
                }
            }
        }
        if(val[end_x][end_y]<80005)//这里需要判断是否救出来了
            printf("%d\n",val[end_x][end_y]);
        else
            printf("Poor ANGEL has to stay in the prison all his life.\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zuhiul

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值