HDU 1010.Tempter of the Bone【dfs应用】(3.3)

Tempter of the Bone

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


Problem Description
The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it up, the maze began to shake, and the doggie could feel the ground sinking. He realized that the bone was a trap, and he tried desperately to get out of this maze.

The maze was a rectangle with sizes N by M. There was a door in the maze. At the beginning, the door was closed and it would open at the T-th second for a short period of time (less than 1 second). Therefore the doggie had to arrive at the door on exactly the T-th second. In every second, he could move one block to one of the upper, lower, left and right neighboring blocks. Once he entered a block, the ground of this block would start to sink and disappear in the next second. He could not stay at one block for more than one second, nor could he move into a visited block. Can the poor doggie survive? Please help him.
 

Input
The input consists of multiple test cases. The first line of each test case contains three integers N, M, and T (1 < N, M < 7; 0 < T < 50), which denote the sizes of the maze and the time at which the door will open, respectively. The next N lines give the maze layout, with each line containing M characters. A character is one of the following:

'X': a block of wall, which the doggie cannot enter; 
'S': the start point of the doggie; 
'D': the Door; or
'.': an empty block.

The input is terminated with three 0's. This test case is not to be processed.
 

Output
For each test case, print in one line "YES" if the doggie can survive, or "NO" otherwise.
 

Sample Input
  
  
4 4 5 S.X. ..X. ..XD .... 3 4 5 S.X. ..X. ...D 0 0 0
 

Sample Output
  
  
NO YES
 
【题解】:

1、题目概述:

该题是一道典型的迷宫类搜索题目,为了达到运行效率的要求,需要采取剪枝的策略。所谓剪枝,就是根据题目的特性,将一些没必要的搜索过程省去,从而提高算法的时间效率。

2、题目分析:

问题:

小狗能否从起点S,经过时间T,恰好到达终点D。

条件:

1、Therefore the doggie had to arrive at the door on exactly the T-th second.(小狗必须在T时刻准时到达出口)

2、In every second, he could move one block to one of the upper, lower, left and right neighboring blocks.(每一秒,小狗可以向上下左右四个方向移动一步)

3、He could not stay at one block for more than one second,(他只能在一个位置上待一秒钟)

4、nor could he move into a visited block.(他也不能再回到已经走过的位置)

3、算法设计:

搜索方式主要有两种,即深搜(DFS,Deep-First-Search)和广搜(BFS,Breadth-First-Search)。显然,

该题目适合采用深搜算法。

接下来我们需要设计剪枝策略,

以提高算法时间效率。

1、奇偶性剪枝(没看懂)

首先,我们先来解释一下奇偶剪枝。

若有一迷宫,将迷宫的每一个位置用0或1来标记(x+y为偶数时为0,x+y为奇数时为1)

0   1   0   1   0

1   0   1   0   1

0   1   0   1   0

1   0   1   0   1

从上面的例子中,我们观察后发现,每一个位置的值必然与其相邻位置的值相反。也就是说我们要从S位置走到D位置,如果S上的值和D上的值相同,必然经过偶数

所以,我们设计的奇偶剪枝如下:

如果起点S上的值和终点D上的值相同,时间T为奇数,则直接判断不可达,从而省去了搜索的过程,提高了算法的效率。

如果起点S上的值和重点D上的值相异,时间T为偶数,则直接判断不可达。

2、剩余可走区域小于时间。

即能够走得位置数量比时间少,那么将所有的位置走过之后,剩下的时间将无路可走,所以直接判断不可达,省去搜索过程。

3、越界。

当越界之后,则停止继续搜索。

4、超时。

搜索路径长度超过时间T之后,停止搜索。

4、编程方式:

使用递归方式编写程序,即编写一个递归函数。递归搜索时的普遍做法是:先写出退出条件,然后再写自身递归。


【代码如下】:
#include <iostream>
#include <cstdio>
#include <math.h>
#include <algorithm>
using namespace std;


#define maxx 8
char mapp[maxx][maxx];
bool escape;
int n, m, t;
int dx, dy;
int axisx[]={0,-1,0,1};//代表行的上移
int axisy[]={-1,0,1,0};//代表列的上移
void dfs(int sx, int sy, int cnt)//cnt是已经经过的时间
{
    if(sx==dx && sy==dy && cnt==t)
    {
        escape = true;
        return;
    }
    int temp = (t-cnt)-abs(sx-dx)-abs(sy-dy);
    if(temp<0 || temp&1)  //temp<0代表剩余的时间无法走完剩余的路程 temp&1是进行奇偶剪枝
    {//奇数&上1等于1,偶数&1等于0,剩余的时间必须为偶数才符合条件
        escape = false;
        return;
    }

    for(int i=0; i<4; ++i)
    {
        if(sx+axisx[i]<0 || sx+axisx[i]>n-1 || sy+axisy[i]<0 || sy+axisy[i]>m-1)//判断是否越界
        {
            continue;
        }
        if(mapp[sx+axisx[i]][sy+axisy[i]] != 'X')//遇到不是‘X’的就要把本次访问的地方改为‘X’,并进行下一次递归
        {
            mapp[sx+axisx[i]][sy+axisy[i]] = 'X';
            dfs(sx+axisx[i], sy+axisy[i], cnt+1);
            mapp[sx+axisx[i]][sy+axisy[i]] = '.';//改回来
        }
        if(escape)
        {
            break;
        }
    }
}


int main()
{
    while((cin>>n>>m>>t) && !(n==0&&m==0&&t==0))
    {
        escape = false;
        int sx, sy;
        int wallnum = 0;
        for(int i1=0; i1<n; ++i1)
        {
            for(int i2=0; i2<m; ++i2)
            {
                cin>>mapp[i1][i2];
                if(mapp[i1][i2] == 'S')
                {
                    sx = i1;
                    sy = i2;
                    mapp[sx][sy] = 'X';//
                }
                else if(mapp[i1][i2] == 'D')
                {
                    dx = i1;
                    dy = i2;
                }
                else if(mapp[i1][i2] == 'X')
                {
                    wallnum++;
                }
            }
        }//输入并处理,记录下起始位置,D的位置,wall的数目,并把s->x!

        if(!((n*m - wallnum)<t))//初步判断,block数必须小于t
        {
            dfs(sx, sy, 0);//对S的初始位置以及已用时间t=0进行DFS
        }
        if(escape)
            cout<<"YES"<<endl;
        else
            cout<<"NO"<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值