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

    Tempter of the Bone

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


    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
    题意;能否在规定的步数恰好到D点。奇偶剪枝:到D点最少步数与剩余步数奇偶性相同。
     1 #include<cstdio>
     2 #include<iostream>
     3 #include<cstring>
     4 #include<cmath>
     5 using namespace std;
     6 int R,C,T,door_x,door_y,dog_x,dog_y;
     7 char maze[10][10];
     8 int vis[10][10];
     9 bool Go;
    10 void dfs(int x,int y,int step)
    11 {
    12     if(Go == true)return ;
    13     if(maze[x][y] == 'D' && step == T){Go = true;return;}
    14     int flag = T-step -fabs(door_x-x) - fabs(door_y-y);
    15     if(step > T || flag&1 || flag < 0)return ;
    16     if(x>=0 && x<R && y>=0 && y<C && maze[x][y] != 'X' && !vis[x][y]){
    17         vis[x][y] = 1;
    18         dfs(x+1,y,step+1);
    19         dfs(x-1,y,step+1);
    20         dfs(x,y-1,step+1);
    21         dfs(x,y+1,step+1);
    22         vis[x][y] = 0;
    23     }
    24     return;
    25 }
    26 int main()
    27 {
    28     //freopen("in.txt","r",stdin);
    29     int i,j,sum;
    30     while(~scanf("%d%d%d",&R,&C,&T),(R||C||T))
    31     {
    32         sum = 0;
    33         for(i=0; i<R; i++){
    34             for(j=0; j<C; j++){
    35                 cin>>maze[i][j];
    36                 if(maze[i][j]=='S'){dog_x = i,dog_y = j;}
    37                 if(maze[i][j]=='D'){door_x = i;door_y = j;}
    38                 if(maze[i][j]=='.'){sum++;}
    39             }
    40         }
    41         Go = false;
    42         memset(vis,0,sizeof(vis));
    43         int odd = fabs(door_x-dog_x)+fabs(door_y-dog_y);
    44         if( T >= odd && sum+1 >= T && !((T - odd)%2) )             //到D点最短时间大于T
    45             dfs(dog_x,dog_y,0);
    46         if(Go)
    47             puts("YES");
    48         else
    49             puts("NO");
    50     }
    51     return 0;
    52 }
    View Code
  • 相关阅读:
    WinForm简单的打包和部署
    C#实现获取枚举的描述
    C#实现序列化对象到XML文档与反序列化
    cookie与session的区别与联系
    windows环境下创建多个Redis实例
    struts2中的constant配置详解
    PowerDesigner使用教程
    配置JAVA环境变量
    CAS实现SSO单点登录原理
    OVER(PARTITION BY)函数用法
  • 原文地址:https://www.cnblogs.com/qiu520/p/3257392.html
Copyright © 2011-2022 走看看