• HDOJ_ACM_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
     
    Code
     
    View Code
     1 #include <stdio.h>
     2 #include <math.h>
     3 #define N 7
     4 #define M 7
     5 int dx[4]={0,1,0,-1};
     6 int dy[4]={1,0,-1,0};
     7 char map[N + 5][M +5];
     8 int n, m, t, flag, desx, desy;
     9 void DFS(int sx, int sy, int step)
    10 {
    11     int i, nx, ny, temp;
    12     //the exit condition
    13     if (flag == 1)
    14         return;
    15     //pruning stategy, one is shortest path, the other is odd-even pruning
    16     temp = (t - (step - 1)) - abs(desx - sx) - abs(desy - sy);
    17     if (temp < 0 || temp & 1)
    18         return;
    19     for (i = 0; i < 4; i++)
    20     {
    21         nx = sx + dx[i];
    22         ny = sy + dy[i];
    23         //out of index
    24         if (nx < 0 || nx >= n || ny < 0 || ny >= m)
    25             continue;
    26         //find it
    27         if (map[nx][ny] == 'D' && step == t)
    28         {
    29             flag = 1;
    30             return;
    31         }
    32         //recursion
    33         if (map[nx][ny] == '.')
    34         {
    35             map[nx][ny] = 'X';
    36             DFS(nx, ny, step + 1);
    37             map[nx][ny] = '.';
    38         }
    39     }
    40     return ;
    41 }
    42 int main()
    43 {
    44     int i, j, sx, sy, countP;
    45     while (scanf("%d %d %d",&n, &m, &t) != EOF && (n || m || t))
    46     {
    47         countP = 0; 
    48         flag = 0;
    49         for (i = 0; i < n; i++)
    50         {
    51             scanf("%s", map[i]);
    52             for (j = 0; j < m; j++)
    53             {
    54                 if (map[i][j] == 'S')
    55                 {
    56                     sx = i; 
    57                     sy = j;
    58                 }
    59                 if (map[i][j] == 'D')
    60                 {
    61                     desx = i;
    62                     desy = j;
    63                 }
    64                 if (map[i][j] == '.')
    65                     countP++;
    66             }
    67         }
    68         //pruning stategy
    69         if (countP + 1 < t)
    70         {
    71             puts("NO");
    72             continue;
    73         }
    74         DFS(sx, sy, 1);
    75         if (flag == 1)
    76             puts("YES");
    77         else
    78             puts("NO");
    79         
    80     }
    81     return 0;
    82 }
     
    Ideas
    I think Actually, there is other pruning strategy, I can use BFS to pretreatment to get the shortest path.  On the other hand, I should learn how to using circle to implement DFS.
     
    Recommend
    JGShining
     
  • 相关阅读:
    vue_03
    vue03
    vue2
    vue02
    vue 01
    JavaScript要点 (一) 变量-作用域
    在iOS应用程序中打开设备设置界面及其中某指定的选项界面
    多线程操作Coredata(转)
    iOS_城市定位
    本地验证码
  • 原文地址:https://www.cnblogs.com/chuanlong/p/3029243.html
Copyright © 2020-2023  润新知