美文网首页程序员
HDU1010(DFS+剪枝)

HDU1010(DFS+剪枝)

作者: 朱清豪 | 来源:发表于2018-08-03 16:51 被阅读0次
    • HDU链接:Tempter of the Bone
    • 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
    • 题意:给出一副图,不可以往回走,问能否恰好花费时间T从启点走到终点。

    • 解题思路:很明显是搜索加剪枝,一个比较容易想到的剪枝是,当目前位置所剩下的步数小于到终点的最短距离时可以剪掉;另一个剪枝方式一般没有经验就想不到了,奇偶剪枝,要理解奇偶剪枝,先了解一下曼哈顿距离,从一个点到达另外一个点的最短路径长度(时间)可以根据两点坐标求出,路径长度(非最短)与最短路径的长度同奇偶,它们的差一定是偶数!举个例子,就像两个偶数的差差是偶数,两个个数的差也是偶数。
      *还有另一个可以剪的地方是:总的格数减去墙的数量必须要大于t,很容易理解,除墙以外每个格子可以走一次,所以总的步数肯定是小于等于非墙的个数,故非墙个数要大于t。

    • 代码:

    #include <cstdio>
    #include <cstring>
    #include <iostream>
    using namespace std;
    char in[10][10];
    int m, n, tt, sx, sy, ex, ey, dir[][2] = {0, 1, 1, 0, 0, -1, -1, 0};
    int abs(int x)
    {
        return x < 0 ? -x : x;
    }
    bool flag;
    void dfs(int x, int y, int s)
    {
        if (x < 0 || x >= m || y < 0 || y >= n)
            return;
        if (x == ex && y == ey && s == tt)
        {
            flag = true;
            return;
        }
        if (flag)
            return;
        int tep = (tt - s) - (abs(x - ex) + abs(y - ey));
        if (tep < 0 || tep & 1)//第一处和第二处(奇偶)剪枝
            return;
        for (int i = 0; i < 4; i++)
        {
            int dx = x + dir[i][0], dy = y + dir[i][1];
            if (in[dx][dy] != 'X')
            {
                in[dx][dy] = 'X';
                dfs(dx, dy, s + 1);
                in[dx][dy] = '.';
                if (flag)
                    return;
            }
        }
    }
    int main(void)
    {
        while (cin >> m >> n >> tt)
        {
            int wall = 0;
            if (m == 0 && n == 0)
                break;
            for (int i = 0; i < m; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    cin >> in[i][j];
                    if (in[i][j] == 'S')
                        sx = i, sy = j;
                    else if (in[i][j] == 'D')
                        ex = i, ey = j;
                    else if (in[i][j] == 'X')
                        wall++;
                }
            }
            if (m * n - wall <= tt)//第三处剪枝
            {
                printf("NO\n");
                continue;
            }
            flag = false;
            in[sx][sy] = 'X';
            dfs(sx, sy, 0);
            if (flag)
            {
                printf("YES\n");
            }
            else
            {
                printf("NO\n");
            }
        }
    }
    

    相关文章

      网友评论

        本文标题:HDU1010(DFS+剪枝)

        本文链接:https://www.haomeiwen.com/subject/rqquvftx.html