zoukankan      html  css  js  c++  java
  • HDU1010 Tempter of the Bone【小狗是否能逃生----DFS奇偶剪枝(t时刻恰好到达)】

    Tempter of the Bone
    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

    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
     

    题目大意:

    一扇门只能在第T秒时打开,问小狗是否能在开门时恰好到达这扇门,逃出去。

    解题思路:

          DFS问题。

    奇偶剪枝:
    是数据结构的搜索中,剪枝的一种特殊小技巧。
    现假设起点为(sx,sy),终点为(ex,ey),给定t步恰好走到终点, 
    s        
    |        
    |        
    |        
    + e
     
    如图所示(“|”竖走,“—”横走,“+”转弯),易证abs(ex-sx)+abs(ey-sy)为此问题类中任意情况下,起点到终点的最短步 数, 记做step,此处step1=8;
      
    s  
      +  
    | +      
    |        
    + e
     
    如图,为一般情况下非最短路径的任意走法举例,step2=14;
    step2-step1=6,偏移路径为6,偶数(易证);
    故,若t-[abs(ex-sx)+abs(ey-sy)]结果为非偶数(奇数),则无法在t步恰好到达;
    返回,false;
    反之亦反。
     
    代码:
     
    [cpp] view plain copy
     
    1. #include<iostream>  
    2. #include<cstdio>  
    3. #include<cstring>  
    4. #include<algorithm>  
    5. #define N 10  
    6.   
    7. using namespace std;  
    8.   
    9. bool flag,ans,visited[N][N];  
    10. int n,m,t,xe,ye;  
    11. char map0[N][N];  
    12.   
    13. void dfs(int x,int y,int timen){  
    14.     if(flag) return ;  
    15.     if(timen>t) return ;  
    16.     if(x<0||x>n-1||y<0||y>m-1) return ;  
    17.     if(timen==t&&map0[x][y]=='D') {flag=ans=true;return ;}  
    18.     int temp=t-timen-abs(xe-x)-abs(ye-y);  
    19.     if(temp&1) return ;//奇偶剪枝,位运算判断是否为奇数,比mod更快.  
    20.       if(!visited[x-1][y]&&map0[x-1][y]!='X'){  
    21.         visited[x-1][y]=true;  
    22.         dfs(x-1,y,timen+1);  
    23.         visited[x-1][y]=false;  
    24.     }  
    25.     if(!visited[x+1][y]&&map0[x+1][y]!='X'){  
    26.         visited[x+1][y]=true;  
    27.         dfs(x+1,y,timen+1);  
    28.         visited[x+1][y]=false;  
    29.     }  
    30.      if(!visited[x][y-1]&&map0[x][y-1]!='X'){  
    31.         visited[x][y-1]=true;  
    32.         dfs(x,y-1,timen+1);  
    33.         visited[x][y-1]=false;  
    34.     }  
    35.      if(!visited[x][y+1]&&map0[x][y+1]!='X'){  
    36.         visited[x][y+1]=true;  
    37.         dfs(x,y+1,timen+1);  
    38.         visited[x][y+1]=false;  
    39.     }  
    40. }  
    41.   
    42. int main(){  
    43.     int xs,ys;  
    44.     while(scanf("%d%d%d",&n,&m,&t)!=EOF&&(n||m||t)){  
    45.         int cnt=0;  
    46.         getchar();  
    47.         memset(visited,false,sizeof(visited));  
    48.         flag=ans=false;  
    49.         for(int i=0;i<n;i++){  
    50.             for(int j=0;j<m;j++){  
    51.                 cin>>map0[i][j];  
    52.                 if(map0[i][j]=='S'){  
    53.                     xs=i;ys=j;  
    54.                     visited[i][j]=true;  
    55.                 }  
    56.                 if(map0[i][j]=='D'){  
    57.                     xe=i;ye=j;  
    58.                 }  
    59.                 if(map0[i][j]=='X'){  
    60.                     cnt++;  
    61.                 }  
    62.             }  
    63.         }  
    64.         if(n*m-cnt-1>=t) dfs(xs,ys,0);  
    65.         if(ans) printf("YES ");  
    66.         else printf("NO ");  
    67.     }  
    68.     return 0;  
    69. }  
  • 相关阅读:
    node 读取文件
    jQuery全局事件处理函数
    可以发送不同源请求的方式
    ajax 高度封装的函数
    jQuery中AJAX的回调
    jQuery中对AJAX的封装
    ajax 基本的封装
    AJAX 返回数据问题
    ajax 关于响应类型
    动态渲染数据到表格中
  • 原文地址:https://www.cnblogs.com/yzm10/p/7242686.html
Copyright © 2011-2022 走看看