zoukankan      html  css  js  c++  java
  • HDU 1010 Tempter of the Bone(DFS+奇偶剪枝)

    HDU 1010 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. 

    InputThe 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. 
    OutputFor 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步从起点到达终点,设起点S(sx, sy),终点E(ex, ey),那么S到E的最短路长度为L = abs(ex - sx) + abs(ey - sy),那么所有从S到E的路径都和L同奇同偶,(不理解的好好想想),而且L必须要小于等于T才可能到达终点,那么就可以利用这两个性质来进行剪枝

    int flag = t - num - abs(x - ex) - abs(y - ey);
    if(flag < 0 || flag&1) return;

    代码:

    #include<iostream>
    #include<cstdio>
    #include<cmath>
    using namespace std;
    const int N = 10;
    char mp[N][N];
    int dx[4] = {0, 0, 1, -1};
    int dy[4] = {1, -1, 0, 0};
    int n, m, t;
    int sx, sy, ex, ey;
    int ok;
    int check(int x, int y) {
        return x >= 0 && x < n && y >= 0 && y < m;
    }
    void dfs(int x, int y, int num) {
        if(x == ex && y == ey && num == t) ok = 1;
        if(ok == 1) return;
        int flag = t - num - abs(x - ex) - abs(y - ey);
        if(flag < 0 || flag&1) return;
        for(int i = 0; i < 4; i++) {
            int nx = x + dx[i], ny = y + dy[i];
            if(check(nx, ny) && mp[nx][ny] != 'X') {
                mp[nx][ny] = 'X';
                dfs(nx, ny, num + 1);
                mp[nx][ny] = '.';
            }
        }
    }
    int main() { 
        while(scanf("%d%d%d", &n, &m, &t) == 3 && n && m && t) {
            for(int i = 0; i < n; i++) {
                scanf("%s", mp[i]);
                for(int j = 0; j < m; j++) {
                    if(mp[i][j] == 'S') {
                        sx = i;
                        sy = j;
                    }
                    if(mp[i][j] == 'D') {
                        ex = i;
                        ey = j;
                    }
                }
            }
            mp[sx][sy] = 'X';
            int flag = t - abs(sx - ex) - abs(sy - ey);
            if(flag < 0 || flag&1) {
                printf("NO
    ");
                continue;    
            }
            ok = 0;
            dfs(sx, sy, 0);
            if(ok) printf("YES
    ");
            else printf("NO
    ");
        }
        return 0;
    }
    作者:kindleheart
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须在文章页面给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    hdu1124 Factorial (求解一个数的阶乘中出现多少个0)
    SQL on Linux: Erro Unable to read instance id from /var/opt/mssql/.system/instance_id
    Error during WebSocket handshake: Unexpected response code: 200 问题处理
    CUP计算资源争抢通过IIS启用处理器关联解决
    ABP在MultipleDbContext也就是多库的场景下发布后异常“Could not find content root folder”问题处理
    ABP运行Login failed for user 'IIS APPPOOL XXXXX Reason: Could not find a login matching the name provided问题解决
    vs2017cpu占用过高解决方案
    docker查看挂载目录Volume
    windows 10安装docker一直挂起在Installing Components and Removing Files
    ABP vue+asp.net core yarn serve报 Cannot find module 'typescript/package.json错误
  • 原文地址:https://www.cnblogs.com/kindleheart/p/9297333.html
Copyright © 2011-2022 走看看