zoukankan      html  css  js  c++  java
  • 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


  • 相关阅读:
    python数据类型汇总
    mac下hadoop环境的搭建以及碰到的坑点
    mac搭建hadoop3.1.1伪分布模式 全网最详细教程!
    Mac中安装node.js和npm
    Mac抓包工具Charles的安装激活及使用
    mysql命令行访问远程数据库
    Linux bash总结(一) 基础部分(适合初学者学习和非初学者参考)
    iTerm2 配色方案
    jar包无法引入解决办法
    41 | 怎么最快地复制一张表?
  • 原文地址:https://www.cnblogs.com/DOLFAMINGO/p/7538775.html
Copyright © 2011-2022 走看看