HDU Problem l

Problem Description
Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M &lt;= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison.<br><br>Angel's friends want to save Angel. Their task is: approach Angel. We assume that &quot;approach Angel&quot; 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.<br><br>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.)<br>
 

Input
First line contains two integers stand for N and M.<br><br>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. <br><br>Process to the end of the file.<br>
 

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." <br>
 

Sample Input
 
 
7 8
#.#####.
#.a#..r.
#..#x...
..#..#.#
#...##..
.#......
........
 

Sample Output
 
 
13

题意:天使(a)被抓了,天使的许多朋友(r)要去救他,中间有怪物,打到怪物需要一个单位时间,走一步需要一个单位时间,求最短的时间,

可以从天使开始搜它的朋友,寻找最近的朋友

我用的是dfs+剪枝,搜索到怪物时,时间多加一,持续搜索,直到找到最小的

详解见code:

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int n,m,ans;//ans为所求的步数
bool ok;
bool v[205][205];
char s[205][205];
int fx[4]={0,0,1,-1},
    fy[4]={1,-1,0,0};
void dfs(int x,int y,int step)
{
    int dx,dy;
    if(v[x][y]||s[x][y]=='#'||step>=ans)
      return;  //step>=ans 为一处剪枝。//步数已经超过能够到达的值了
    if(s[x][y]=='r')
    {
        ok=true;
        ans=min(ans,step);
        return;
    }
    if(s[x][y]=='x')//碰到怪物步数加一
        step++;
    v[x][y]=true;     //所有该返回的判断全部结束了,设置访问标记
    for(int i=0;i<=3;++i)
    {
        dx=x+fx[i];
        dy=y+fy[i];
        dfs(dx,dy,step+1);
    }
    v[x][y]=false;     //清除标记
}
int main()
{
    while(~scanf("%d %d%*c",&n,&m)&&(n||m))//最后输入0 0 结束
    {
        int x,y;
        memset(s,'#',sizeof(s));
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=m; j++)
            {
                scanf("%c",&s[i][j]);
                if(s[i][j]=='a')
                {
                    x=i;
                    y=j;
                }
            }
            getchar();//换行
        }
        ok=false;
        ans=100000;
        dfs(x,y,0);
        if(ok)
          printf("%d\n",ans);
        else
          printf("Poor ANGEL has to stay in the prison all his life.\n");
    }
    return 0;
}




  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值