• hdu1010 Tempter of the Bone —— dfs+奇偶性剪枝


    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010


    Tempter of the Bone

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 131057    Accepted Submission(s): 35308


    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
     
    Author
    ZHANG, Zheng
     
    Source
     
    Recommend
    JGShining
     

    代码如下:

     1 #include<stdio.h>//hdu1010 dfs+奇偶性剪枝
     2 #include<stdlib.h>
     3 
     4 char map[10][10];
     5 int n,m,t, wall, si, sj, di, dj;
     6 int d[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
     7 
     8 int dfs(int i, int j, int step)
     9 {
    10     if(step == t)//如果最后一秒
    11     {   //判断是否到达door
    12         if(i == di && j == dj) return 1;
    13         else return 0;
    14     }
    15     
    16     if (i == di && j == dj )//如果到达door
    17     {
    18         //判断是否最后一秒
    19         if (step == t) return 1;
    20         else return 0;
    21     }
    22     
    23     //奇偶性剪枝
    24     int temp = t-step-abs(i - di) + abs(j - dj);
    25     if( temp%2 )
    26         return 0;
    27 
    28     for (int k = 0; k<4; k++)
    29     {
    30         int x = i + d[k][0];
    31         int y = j + d[k][1];
    32         if (x>=1 && x<=n && y>=1 && y<=m && map[x][y] != 'X')
    33         {
    34             map[x][y] = 'X';
    35             if(dfs(x, y, step + 1)) return 1;
    36             map[x][y] = '.';//回溯出口,记得复原(可能把'D'变成'.',但已经用didj保存其位置,所以没关系)
    37         }
    38     }
    39     return 0;
    40 }
    41 
    42 int main()
    43 {
    44     while(scanf("%d%d%d",&n,&m,&t) &&m &&n &&t)
    45     {
    46         wall = 0;
    47         for (int i = 1; i<=n; i++)
    48         {   
    49             scanf("%s",map[i]+1);
    50             for (int j = 1; j<=m; j++)
    51             {
    52                 //用scanf(%c) 就出问题?
    53                 if (map[i][j] == 'S')  { si = i; sj = j; }
    54                 if (map[i][j] == 'D')  { di = i; dj = j;}
    55                 if (map[i][j] == 'X')  { wall++; }
    56             }
    57         }
    58         
    59         map[si][sj] = 'X';
    60         if (n * m - wall-1 < t)
    61         {   //初始判断剩余的空格(记得减去初始位置)是否够走
    62             puts("NO");
    63             continue;
    64         }
    65 
    66         if (dfs(si, sj, 0))
    67             puts("YES");
    68         else
    69             puts("NO");
    70     }
    71     return 0;
    72 }
    View Code


  • 相关阅读:
    My.NET.ClassLibrary.Note.1
    DesignPattern.Ch12.Facade.Node
    DesignPattern.Ch10.Template Method.Note
    Oracle.sql.Note
    C#.Static Field And Static Constructor.Autocriticism
    DesignPattern.Ch4.Open Closed Principle.Note
    关于n!被整除的问题【算法实现】
    令人费解的编译错误:error C2144: syntax error : 'double' should be preceded by ';' 和 error C3646: ';' : unknown override specifier
    诡异难解决的错误:Windows已在xxx.exe中触发一个断点
    Linux(Ubuntu10.04 )下libxml2的安装以及使用示例
  • 原文地址:https://www.cnblogs.com/DOLFAMINGO/p/7538775.html
Copyright © 2020-2023  润新知