• hdu 1010 Tempter of the Bone(dfs暴力)


    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
     

     很久以前做过的一道题目,最近有学弟来问,所以又重新做了一遍。

    题目要求的是 刚好在T时间走完了到达门的位置,并不是求最短到达的时间,所以不能用bfs。用dfs暴力深搜搞定。

    此题要vis回溯,还要有两个剪枝。其中最重要的一个剪枝是奇偶剪枝,没了这个直接超时。如下所示:

                       if((T%2)!=(abs(s.x-ed.x)+abs(s.y-ed.y))%2) return;//奇偶剪枝,很重要!!!

     1 #pragma comment(linker, "/STACK:1024000000,1024000000")
     2 #include<iostream>
     3 #include<cstdio>
     4 #include<cstring>
     5 #include<cmath>
     6 #include<math.h>
     7 #include<algorithm>
     8 #include<queue>
     9 #include<set>
    10 #include<bitset>
    11 #include<map>
    12 #include<vector>
    13 #include<stdlib.h>
    14 #include <stack>
    15 using namespace std;
    16 #define PI acos(-1.0)
    17 #define max(a,b) (a) > (b) ? (a) : (b)
    18 #define min(a,b) (a) < (b) ? (a) : (b)
    19 #define ll long long
    20 #define eps 1e-10
    21 #define MOD 1000000007
    22 #define N 10
    23 #define inf 1e12
    24 int n,m,t;
    25 char mp[N][N];
    26 int vis[N][N];
    27 struct Node{
    28    int x,y;
    29 }st,ed;
    30 int flag;
    31 int dirx[]={-1,1,0,0};
    32 int diry[]={0,0,-1,1};
    33 void dfs(Node s,int T){
    34 
    35    if(flag) return;
    36    if(abs(s.x-ed.x) + abs(s.y-ed.y)>T) return;
    37    //if((T-abs(s.x-ed.x) - abs(s.y-ed.y))%2 ) return;//奇偶剪枝,很重要!!!
    38    if((T%2)!=(abs(s.x-ed.x)+abs(s.y-ed.y))%2) return;//奇偶剪枝,很重要!!!
    39    if(T==0){
    40       if(s.x==ed.x && s.y==ed.y){
    41          flag=1;
    42          printf("YES
    ");
    43          return;
    44       }
    45    }
    46    Node tmp;
    47    for(int i=0;i<4;i++){
    48       tmp.x=s.x+dirx[i];
    49       tmp.y=s.y+diry[i];
    50       if(tmp.x<0 || tmp.x>=n || tmp.y<0 || tmp.y>=m ) continue;
    51       if(vis[tmp.x][tmp.y]) continue;
    52       if(mp[tmp.x][tmp.y]=='X') continue;
    53       vis[tmp.x][tmp.y]=1;
    54       dfs(tmp,T-1);
    55       vis[tmp.x][tmp.y]=0;
    56    }
    57 
    58 
    59 }
    60 int main()
    61 {
    62    while(scanf("%d%d%d",&n,&m,&t)==3){
    63       if(n==0 && m==0 && t==0){
    64          break;
    65       }
    66       for(int i=0;i<n;i++){
    67          scanf("%s",mp[i]);
    68          for(int j=0;j<m;j++){
    69             if(mp[i][j]=='S'){
    70                st.x=i;
    71                st.y=j;
    72             }
    73             if(mp[i][j]=='D'){
    74                ed.x=i;
    75                ed.y=j;
    76             }
    77          }
    78       }
    79       memset(vis,0,sizeof(vis));
    80       vis[st.x][st.y]=1;
    81       flag=0;
    82       dfs(st,t);
    83       if(flag==0){
    84          printf("NO
    ");
    85       }
    86    }
    87     return 0;
    88 }
    View Code
  • 相关阅读:
    Vue开发之vue-router的基本使用
    react监听URL的正确方式
    Electron Windows增加托盘悬浮框功能
    vue自定义标签和单页面多路由实现
    如何通过带有Vue插槽的组件传递HTML内容
    Flutter使用JsBridge方式处理Webview与H5通信
    npm实用知识_命令合集
    <keep-alive> 缓存及其缓存优化原理
    深入学习和理解 Redux
    Centos7使用Yum安装高版本的LNMP
  • 原文地址:https://www.cnblogs.com/UniqueColor/p/5000522.html
Copyright © 2020-2023  润新知