zoukankan      html  css  js  c++  java
  • HDU 1010 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
    //需要求的是能否在规定的那个时间刻刚好到达,而不是在规定时间内能否到达:(该代码不小心会超时的~~~~)
    #include<stdio.h>
    #include<string.h>
    #include<math.h>
    #define N 30
    int m, n, T, flag, c, d, mark[N][N];
    char Map[N][N];
    int dir[4][2] = { {1,0}, {-1,0}, {0,1}, {0,-1} };
    int Dis(int x, int y)
    {
        int a, b;
        a = fabs(x-c);
        b = fabs(y-d);
        return a+b; //求出该点到终点的最短距离
    }
    int Judge(int nx, int ny) //判断该点是否能走
    {
        if (nx >= 0 && nx < m && ny >= 0 && ny < n && Map[nx][ny] != 'X' && mark[nx][ny] == 0)
            return 1;
        return 0;
    }
    void DFS(int x, int y, int s)
    {
        int dis, nx, ny, i;
        if (s == T && x == c && y == d)
        {
            flag = 1;
            return ;
        } //能够到达则停止搜索
        if(flag) return ;
        dis = Dis(x, y);
        if (dis > T-s || (dis+T-s)%2 != 0) return ; //要是最短时间比还能走的时间大,就不能继续,当要走的步数和还剩下的步数奇偶不同时也不能继续(奇偶剪枝)
        for (i = 0; i < 4; i++)
        {
            nx = x + dir[i][0];
            ny = y + dir[i][1];
            if (Judge(nx, ny))
            {
                mark[nx][ny] = 1; //用mark数组标记是否到达过,更节约时间
                DFS(nx, ny, s+1);
                mark[nx][ny] = 0;
            }
        }
    }
    int main ()
    {
        int i, j, a, b;
        while (scanf("%d %d %d", &m, &n, &T), m+n+T)
        {
            memset(mark, 0, sizeof(mark));
            flag = 0;
            for (i = 0; i < m; i++)
                scanf("%s", Map[i]);
            for (i = 0; i < m; i++)
            {
                for (j = 0; j < n; j++)
                {
                    if(Map[i][j] == 'S')
                    {
                        a = i;
                        b = j;
                    }
                    if (Map[i][j] == 'D')
                    {
                        c = i;
                        d = j;
                    }     
                }
            }
            mark[a][b] = 1;
            DFS(a, b, 0);
            if (flag == 1) printf("YES
    ");
            else printf("NO
    ");
        }
        return 0;
    }

    下面有个不会超时的:(其实两个代码意思差不多,都运用了奇偶剪枝,但是第二个更剪枝。。。)

    #include<stdio.h>
    #include<string.h>
    #define N 30
    int m, n, T, flag, c, d, mark[N][N];
    char Map[N][N];
    int dir[4][2] = { {1,0}, {-1,0}, {0,1}, {0,-1} };
    int Dis(int a);
    void DFS(int x, int y, int s);
    int main ()
    {
        int i, j, a, b, sum;
        while (scanf("%d %d %d", &m, &n, &T), m+n+T)
        {
            sum = 0;
            memset(mark, 0, sizeof(mark));
            flag = 0;
            for (i = 0; i < m; i++)
                scanf("%s", Map[i]);
            for (i = 0; i < m; i++)
            {
                for (j = 0; j < n; j++)
                {
                    if(Map[i][j] == 'S')
                    {
                        a = i;
                        b = j;
                    }
                    if (Map[i][j] == 'D')
                    {
                        c = i;
                        d = j;
                    }
                    if(Map[i][j] == 'X')
                    {
                        sum++;
                    }
                }
            }
            if(n*m-sum-1 < T) printf("NO
    ");
            else
            {
                mark[a][b] = 1;
                DFS(a, b, 0);
                if (flag == 1) printf("YES
    "); 
                else printf("NO
    ");
            }
        }
        return 0;
    }
    int Dis(int a)
    {
        return a >= 0 ? a : -a;
    }
    void DFS(int x, int y, int s)
    {
        int dis, nx, ny, i;
        if (s == T && x == c && y == d)
        {
            flag = 1;
            return ;
        }
        if(flag) return ;
        dis = Dis(x-c) + Dis(y-d);
        if (dis > T-s || (dis+T-s)%2 != 0) return ;
        for (i = 0; i < 4; i++)
        {
            nx = x + dir[i][0];
            ny = y + dir[i][1];
            if (nx >= 0 && nx < m && ny >= 0 && ny < n && Map[nx][ny] != 'X' && mark[nx][ny] == 0)
            {
                mark[nx][ny] = 1;
                DFS(nx, ny, s+1);
                mark[nx][ny] = 0;
            }
        }
    }
  • 相关阅读:
    FFmpeg 协议初步学习
    HTML DOM(一):认识DOM
    ant 安装
    ubunut 查看port被哪个程序占用
    cacti气象图调整(批量位置调整、更改生成图大小等)
    内网port映射具体解释(花生壳)
    HDU 2647 Reward(图论-拓扑排序)
    白话经典算法系列之七 堆与堆排序
    Codeforces Round #191 (Div. 2)---A. Flipping Game
    Serverlet具体解释
  • 原文地址:https://www.cnblogs.com/syhandll/p/4671280.html
Copyright © 2011-2022 走看看