Tempter of the Bone

本文介绍了一个迷宫逃脱问题,通过深度优先搜索算法帮助小狗在限定时间内找到出口。文章讨论了如何利用奇偶剪枝技术减少搜索空间,提高算法效率。
 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

这个题想的是用bfs()来写,看看最短和最长,看看在不在这个区间里,发现有例子根本就不对,看了题解才发现原来是深搜。我对于深搜还是广搜的理解就是,广搜就是在找一条最短的路径,而深搜是放一条狗出去找,找不到这只狗再回来,换一条路走,就是他是在尝试一人独自完成一件事。广搜有点像小女人(例子有点不恰当,谅解),不敢迈步,总是一点一点的在中心往外面去试探,总是慢慢拓展面积一直到那个点在我搜索区域的的范围内。

这个题还用到了奇偶性剪枝,要不然会超时。
这里写图片描述
关于奇偶剪枝:

不利用奇偶剪枝的话一直TLE,现在对于奇偶剪枝不理解,暂时记住公式,写一点自己奇偶剪枝的小理解,t-cnt为目前剩余的时间,abs(x-dx)+abs(y-dy)为从当前改点到终点(门)所需要的最短步数,剩余时间为偶数(奇数),就需要走偶数步(奇数步),

奇数-偶数 = 奇数

而奇数-奇数= 偶数,偶数-偶数=偶数

所以只有当剩余的时间与目前位置到终点的最短步数奇偶性相同时,才有可能恰好在t时刻到大门的地方(因为中间会有墙,需根据题目条件继续判定,奇偶剪枝只是把范围缩小了)
还有就是开头时间步数可移动位置的数量关系,也属于剪枝
abs(x-dx)+abs(y-dy)为从当前改点到终点(门)所需要的最短步数,但他会根据总时间的需要进行绕行,奇数步绕行后也是奇数步,偶数步绕行后也是偶数步,故他们之间奇偶性是相同的

#include<stdio.h>
#include<string.h>
#include<cmath>
#include<queue>
using namespace std;
const int Max=10;

char mp[Max][Max];
int vis[Max][Max];
int st_x,st_y,en_x,en_y;
int move_step[4][2]={{0,1},{0,-1},{1,0},{-1,0}};//第一次尝试这么写,感觉还是挺好用的
int n,m,t;
int flag;

void dfs(int x,int y,int step)
{
    if(x==en_x&&y==en_y&&step==t)
    {
        flag=1;
        return;
    }

    int temp=t-step-abs(en_x-x)-abs(en_y-y);//过程中奇偶剪枝的核心代码
    if(temp<0||temp&1)        //这就是属于提前判断,看看剩下的步数还能不能到,或者符不符合奇偶
        return;


    for(int i=0;i<4;i++)
    {
       int next_x,next_y;
       next_x=x+move_step[i][0];
       next_y=y+move_step[i][1];
       if(next_x<1||next_x>n||next_y<1||next_y>m||vis[next_x][next_y]||mp[next_x][next_y]=='X')
         continue;//对于这些路径就没有必要放狗了,一个是在前面放过了,再就是根本这个点不允许
       vis[next_x][next_y]=1;//走过这条路就不能走了
       dfs(next_x,next_y,step+1);
       //这是刚才那只狗的结果反馈之后,我们应该怎样做
       if(flag)//一种那只狗已经找到了那个点
          return ;//我们这些人就没有必要再往下找了
                //要是刚才那只狗没有找到合适的路径,我们就不能占着这个点了,我们需要放出这个位置
        vis[next_x][next_y]=0;  //可能下一只狗能用到这个点,一旦回来之后这些点都会被释放
    }
}


int main()
{
    while(~scanf("%d %d %d",&n,&m,&t))
    {
        memset(vis,0,sizeof(vis));
        memset(mp,0,sizeof(mp));
        if(!n&&!m&&!t)
            break;
        int wall=0;//也是为了剪掉一些数据
        flag=0;    //一开始flag没有初始化,WA了几次,因为这是几组数据,每次都需要初始化数据的
        for(int i=1;i<=n;i++)
           for(int j=1;j<=m;j++)
           {
               scanf(" %c",&mp[i][j]);
               if(mp[i][j]=='S')
                  st_x=i,st_y=j;
                else if(mp[i][j]=='D')
                    en_x=i,en_y=j;
                else if(mp[i][j]=='X')
                    wall++;

           }

        if(t>n*m-wall-1)//如果时间比可能的最大步数还大的话,就剪枝了
        {
               printf("NO\n");
               continue;
        }

        int temp=abs(en_x-st_x)+abs(en_y-st_y);//开始进行剪枝,直接判断能不能到达
        if((temp+t)&1)
        {
            printf("NO\n");
            continue;
        }

        vis[st_x][st_y]=1;//这是起点一定是回不来了
        dfs(st_x,st_y,0);//对起点所有可能的路径进行深搜,深搜还是一种固定的标准,条件多一点

        if(flag)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值