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

这个是一个深搜题但是不是要求到目的地的最短时间而是在规定时间到达目的地;

这个题要用到奇偶剪枝,否则会T。

#include <iostream>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<deque>
#include<queue>
#include<vector>
#include<stack>
#include<ctime>
using namespace std;
struct A{
    int a,b,step;
}dog,door;
int n,m,c,tt;
char a[60][60];
void DFS(int x,int y,int step)
{
    if(x==door.a&&y==door.b&&step==c)
    {
        tt=1;
        return;
    }

    else
    {
        int temp=(c-step)-fabs(x-door.a)-fabs(y-door.b);
        if(temp<0||temp%2)//奇偶剪枝
        return;
        if(x+1<n&&a[x+1][y]!='X')
        {
            a[x+1][y]='X';
            DFS(x+1,y,step+1);
            if(tt)
            return ;
            a[x+1][y]='.';

        }

        if(x-1>=0&&a[x-1][y]!='X')
        {
            a[x-1][y]='X';
            DFS(x-1,y,step+1);
             if(tt)
            return ;
            a[x-1][y]='.';

        }

        if(y+1<m&&a[x][y+1]!='X')
        {
            a[x][y+1]='X';
            DFS(x,y+1,step+1);
            if(tt)
            return ;
            a[x][y+1]='.';

        }

        if(y-1>=0&&a[x][y-1]!='X')
        {
            a[x][y-1]='X';
            DFS(x,y-1,step+1);
             if(tt)
            return ;
            a[x][y-1]='.';

        }

    }


}
int main()
{
    while(cin>>n>>m>>c&&(n||m||c))
    {
        int p=0;
        memset(a,0,sizeof(a));
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                cin>>a[i][j];
                if(a[i][j]=='S')
                {
                    dog.a=i;
                    dog.b=j;
                    dog.step=0;
                }
                if(a[i][j]=='D')
                {
                    door.a=i;
                    door.b=j;
                    door.step=c;
                }
                if(a[i][j]=='X')
                    p++;
            }
        }
        if(n*m-p<c)
            cout<<"NO"<<endl;
        else
        {
            tt=0;
            a[dog.a][dog.b]='X';
        DFS(dog.a,dog.b,dog.step);
        if(tt)
            cout<<"YES"<<endl;
        else
            cout<<"NO"<<endl;
        }

    }
}
/*
4 4 5
S.X.
..X.
..XD
....
3 4 5
S.X.
..X.
...D
*/

猜你喜欢

转载自blog.csdn.net/qq_41232172/article/details/81105345