HDU 1242【DFS/BFS】

46 篇文章 0 订阅
17 篇文章 0 订阅

Rescue

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 35096    Accepted Submission(s): 12203


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 <= 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


题解:题目其实很简单,但是容易出错。从Angel的位置开始找,四个方向,每次从一个方向搜到底,如果没找到就换一个方向找,直到找完四个方向,若是还是没有找到就输出“Poor ANGEL has to stay in the prison all his life.” 。找到了注意更新最短的距离,因为可能存在多条路径,另外本题还可用BFS做,网上大部分的题解都是BFS加优先队列做的,我就不贴了。

代码:

#include<cstdio>
#include<cstring>
#include<queue>
#include<iostream>
#define INF 99999999
using namespace std;
const int maxn=205;
char s[maxn][maxn];
int vis[maxn][maxn];
int minx=INF;//刚开始置为无穷大
int n,m;
int dir[4][2]={0,1,-1,0,0,-1,1,0};//四个方向
void dfs(int x,int y,int step){
    if(s[x][y]=='x') step++;//打死保安花费一分钟
    if(s[x][y]=='r'){//找到她的朋友之后还要判断当前路径是否是耗时最短的
        if(step<minx)
            minx=step;
        return ;
    }
    for(int i=0;i<4;i++){
        int tx=x+dir[i][0];
        int ty=y+dir[i][1];
        if(tx<0||tx>=n||ty<0||ty>=m)//边界判断
            continue;
        if(!vis[tx][ty]&&s[tx][ty]!='#'){
            vis[tx][ty]=1;
            dfs(tx,ty,step+1);
            vis[tx][ty]=0;//此路不通或者找到之后要从其他方向尝试看能不能找到或者看能不能耗时更短。因此需要将以前走过的路标记为没有走过。
        }
    }
    return;
}
int main(){
    while(~scanf("%d %d",&n,&m)){
        minx=INF;
        memset(vis,0,sizeof vis);
        int sx,sy;
        for(int i=0;i<n;i++){
            scanf("%s",s[i]);
        }
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                if(s[i][j]=='a'){
                    sx=i,sy=j;
                    break;
                }
            }
        }
        vis[sx][sy]=1;
        dfs(sx,sy,0);
        if(minx!=INF) printf("%d\n",minx);
        else printf("Poor ANGEL has to stay in the prison all his life.\n");
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值