Tempter of the Bone dfs

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 

meaning :狗在S点,门在D点,门只有在T时刻才开,迷宫为N*M矩阵,迷宫内有X代表的墙。问是否狗能准时逃出来。我们要制造一个计划,让狗能在T时刻刚好到达门口


分析:为了准时到达door有可能需要绕路,而在只能上下左右情况下绕路,
多出来的步伐即偏移步伐一定是偶数,
根据这个条件可剪枝,除此之外,
当前位置到door的最快时间都大于剩余的时间时就要剪枝。
为了准时到达door,没必要去想如何特意绕路,dfs会把所有的情况考虑进去
 

搜索中要用到的奇偶剪枝的原理:

看张图,没障碍物#时,S到E的最短路长为6,但是当有障碍物时,就要绕行了

看这张图,黑色为最短路径,当他绕行(红色加蓝色部分)时,其中蓝色部分其实还是最短路径部分平移来的,所以多走的步数也就是红色部分

对于红色部分我们可以分为两部分,一部分是远离最短路径的步数,另一部分是回到最短路径的部分,他们一定是对称的,所以多走的步数一定是偶数!!!

所以要是问走x步能否到达e,就算出最短路径长y,如果x-y是偶数就能到达,否则不能到达!

 

#include<iostream>
#include<cstdio>
#include<bits/stdc++.h>
using namespace std;
char ch[10][10];
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
int n,m,T,rs,fx,fy;
void dfs(int x,int y,int step)
{
    if(rs)return;
    if(x==fx&&y==fy&&step==T){
        rs=1;return;
    }
    int len=T-step-fabs(fx-x)-fabs(fy-y);//len即是偏移路径又是剩余时间与最短时间的差
    if(len<0||len&1)return;
    for(int i=0;i<4;i++)
    {
        int ii=x+dx[i];
        int jj=y+dy[i];
        if(ch[ii][jj]=='X'||ii<0||jj<0||ii>=n||jj>=m)continue;
        ch[ii][jj]='X';
        dfs(ii,jj,step+1);
        if(rs)return;
        ch[ii][jj]='.';
    }
    return;
}
int main()
{
    while(cin>>n>>m>>T)
    {
        getchar();
        if(n+m+T==0)break;
        int wall=0,sx,sy;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                scanf("%c",&ch[i][j]);
                if(ch[i][j]=='S'){
                    sx=i;sy=j;
                }else if(ch[i][j]=='D'){
                    fx=i;fy=j;
                }else if(ch[i][j]=='X'){
                    wall++;
                }
            }getchar();
        }
        int t1=fabs(fx-sx)+fabs(fy-sy),t2=m*n-wall-1;
        if(t1>T||t2<T){
                printf("NO\n");
                continue;
        }
        ch[sx][sy]='X';
        rs=0;
        dfs(sx,sy,0);
        rs?printf("YES\n"):printf("NO\n");
        /*for(int i=0;i<n;i++)puts(ch[i]);*/
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值