我是靠谱客的博主 香蕉丝袜,最近开发中收集的这篇文章主要介绍HDU 1242 Rescue(dfs深搜),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

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
 

//Must so
#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<ctype.h>
#include<queue>
#include<vector>
#include<set>
#include<cstdio>
#include<cmath>
#define mem(a,x) memset(a,x,sizeof(a))
#define inf 1<<29
#define NN 1000006
using namespace std;
const double PI = acos(-1.0);
typedef long long LL;

/**********************************************************************
题意:
给出图,‘a’是终点,‘r’是起点,‘.’表示可以走,‘#’不能走
每走一步用1秒,遇到‘x’多花1秒
问从起点到终点最少时间
dfs深度搜索
=、=最后才发现,起点‘r’不止一个
所以干脆从终点去找最近的起点
**********************************************************************/
int tx,ty;
int row,cow;//行和列
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};
char mp[202][202];
int ans;
void dfs(int x,int y,int l)//返回从该点到达终点的最短时间
{
    bool fd = 0;
    if (mp[x][y] == 'x')//这个地方回溯注意一下
    fd = 1;
    mp[x][y] = '#';
    for (int i = 0;i < 4;i++)
    {
        int xx = x + dx[i];
        int yy = y + dy[i];

        if (xx >= 0&&yy >= 0&&xx < row&&yy < cow)
        {
            if (mp[xx][yy] == '.')
            {
                dfs(xx,yy,l+1);
            }
            if (mp[xx][yy] == 'x')
            {
                dfs(xx,yy,l+2);
            }
            if (mp[xx][yy] == 'r')
            {
                ans = min(l+1,ans);
            }
        }
    }
    mp[x][y] = '.';
    if (fd) mp[x][y] = 'x';
}
int main()
{
    while (~scanf("%d%d ",&row,&cow))
    {
        for (int i = 0;i < row;i++)
        {
            scanf("%s",mp[i]);
        }
        for (int i = 0;i < row;i++)
        {
            for (int j = 0;j < cow;j++)
            {
                if (mp[i][j] == 'a')
                    tx = i,ty = j;
            }
        }
        ans = inf;
        dfs(tx,ty,0);
        if (ans == inf) puts("Poor ANGEL has to stay in the prison all his life.");
        else printf("%dn",ans);
    }
    return 0;
}

最后

以上就是香蕉丝袜为你收集整理的HDU 1242 Rescue(dfs深搜)的全部内容,希望文章能够帮你解决HDU 1242 Rescue(dfs深搜)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(45)

评论列表共有 0 条评论

立即
投稿
返回
顶部