您的位置:首页 > 其它

E - Tempter of the Bone(深度搜索)

2017-07-27 16:59 253 查看
题目:

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.如果当前时间即步数(step) >= T 而且还没有找到D点,则剪掉。

2.设当前位置(x, y)到D点(dx, dy)的最短距离为s,到达当前位置(x, y)已经花费时间(步数)step,那么,如果题目要求的时间T - step < s,则剪掉。

3. 对于当前位置(x, y),如果,(                                                                                                                                                                                  T-step-s)是奇数,则剪掉(奇偶剪枝)。

4.如果地图中,可走的点的数目(xnum) < 要求的时间T,则剪掉(路径剪枝)。

c++代码:

#include<iostream>

#include<cstdio>

#include<cstring>

#include<cmath>

#include<algorithm>

#include<cstdlib>

using namespace std;

char maze[10][10];

int book[10][10];

int n,m,t;

int bx,by,ex,ey;

int ok;

int nextt[4][2]={{-1,0},{1,0},{0,-1},{0,1}};

int abs(int p)

{

    return p>=0?p:-p;

}

void dfs(int x,int y,int step)

{

    if(ok==1)

    return;

    if(step==t&&x==ex&&y==ey)

    {

        ok=1;

        return;//到这里就可以直接返回了;

    }//剪掉枝叶二 如果说当步数相等时 小狗无法到达那个门的话那么就不需要继续往下走了 因为没有必要

    int minstep=abs(ex-x)+abs(ey-y);//就是求此刻的路到门的最小步数;  

    int overplus=t-step;  //就是说还剩下多少步可以走;

    if(minstep>overplus)return;//剪掉枝叶三 如果还要走的路大于剩下要走的路的话 那么 小狗也无法到达;

    if((minstep+overplus)%2==1)return ;//剪掉枝叶四   如果他们奇偶性不同的话那么小狗也无法到达

      //minstep和overplus的奇偶性必定相同    

    for(int i=0;i<=3;i++)

    {

        int nx=x+nextt[i][0];

        int ny=y+nextt[i][1];

        if(nx<0||ny<0||nx>=n||ny>=m)continue;

        if(book[nx][ny]==0&&maze[nx][ny]!='X')

        {

            book[nx][ny]=1;

            dfs(nx,ny,step+1);

            book[nx][ny]=0;

        }

    }

}

int main()

{

    int i,j;

    while(~scanf("%d%d%d",&n,&m,&t))

    {

        if(n==0&&m==0&&t==0)break;

        int num=0;

        for(i=0;i<n;i++)

        {

            scanf("%s",maze[i]);

            for(j=0;j<m;j++)

            {

              if(maze[i][j]=='S')

            {

                bx=i;

                by=j;

            }

            if(maze[i][j]=='X')

            num++;

            if(maze[i][j]=='D')

            {

                ex=i;

                ey=j;

            }     

            }

        }

        //控制输入 找出起始点和终点的位置以及有多少障碍       该题目需要剪掉枝叶

        if(n*m-num-1<t)

        {

            printf("NO\n");

            continue;

        } //剪掉枝叶1  如果可以踩的方块小于开启的时间t  

        //则方块会落陷那么小狗就会死掉 因此不需要往下走  这是第一个剪枝

        memset(book,0,sizeof(book));    

        book[bx][by]=1;

        ok=0;

        dfs(bx,by,0);

        if(ok==1)printf("YES\n");

        else printf("NO\n");

    }

    return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: