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

    解题思路:

    这题很明显用DFS。但是如果是没有优化过的DFS交上去就是会TE

    后来去看了一下博客。知道了一个很神奇的东西但是也是很基础的东西吧-> 奇偶剪枝

    现假设起点为(sx,sy),终点为(ex,ey),给定t步恰好走到终点

    那我们知道,如果没有障碍物的话 它的最小步数是 temp = abs(ex-sx)+abs(ey-sy)

    但是如果有障碍物的话可能就无法走到最小步数。

    如果 t - temp 是奇数的话那么肯定走不到终点  如果是偶数的话,就可能可以走到终点 

    其实很好理解这个,因为如果你偏离了最小步数,那么肯定是要想办法不再偏移 一个+1  必要想办法伴随一个 -1   所以就是偶数了

     1 #include <iostream>
     2 #include <algorithm>
     3 #include <stdlib.h>
     4 #include <string>
     5 #include <string.h>
     6 #include <set>
     7 #include <queue>
     8 #include <stdbool.h>
     9 
    10 #define LL long long
    11 #define inf 0x3f3f3f3f
    12 using namespace std;
    13 const int MAXN=1e3+5;
    14 
    15 int n,m,t;
    16 int sx,sy;
    17 int dx,dy;
    18 int flag;
    19 char map[10][10];
    20 int vis[10][10];
    21 int mx[] = {1,-1,0,0};
    22 int my[] = {0,0,1,-1};
    23 
    24 void dfs(int row,int col,int time)
    25 {
    26     int temp = t-time-abs(dx-row)-abs(dy-col);
    27     if (temp < 0 || temp&1)
    28         return ;
    29     if (flag)
    30         return ;
    31     if (map[row][col] == 'D' && time==t)
    32     {
    33         flag = 1;
    34         return ;
    35     }
    36     if (time > t)
    37         return ;
    38     for (int i=0;i<4;++i)
    39     {
    40         int nx = row + mx[i];
    41         int ny = col + my[i];
    42         if (nx>=0 && ny>=0 && nx<n && ny<m && !vis[nx][ny] && map[nx][ny] != 'X')
    43         {
    44             vis[nx][ny] = 1;
    45             dfs(nx,ny,time+1);
    46             if (flag)
    47                 return ;
    48             vis[nx][ny] = 0;
    49         }
    50     }
    51 }
    52 
    53 
    54 int main()
    55 {
    56 #ifndef ONLINE_JUDGE
    57     freopen("../in.txt","r",stdin);
    58 #endif
    59     while (~scanf("%d%d%d",&n,&m,&t))
    60     {
    61         if (m==0 && n==0 && t==0)
    62             break;
    63         memset(vis,0, sizeof(vis));
    64         flag = 0;
    65         for (int i = 0; i < n; i++)
    66         {
    67             for (int j = 0; j < m; j++)
    68             {
    69                 cin >> map[i][j];
    70                 if (map[i][j] == 'S')
    71                 {
    72                     sx = i;
    73                     sy = j;
    74                 }
    75                 if (map[i][j] == 'D')
    76                 {
    77                     dx = i;
    78                     dy = j;
    79                 }
    80             }
    81         }
    82         vis[sx][sy] = 1;
    83         dfs(sx, sy, 0);
    84         if (flag)
    85             printf("YES
    ");
    86         else
    87             printf("NO
    ");
    88     }
    89     return 0;
    90 }

    以后如果再遇到这种t步到达的问题,一定要去想想奇偶剪枝!

  • 相关阅读:
    java反射注解妙用-获取所有接口说明
    npm设置和查看仓库源
    正则表达式-linux路径匹配
    在vue中使用echarts图表
    设计模式-简单工厂模式
    设计模式
    Redis实现世界杯排行榜功能(实战)
    开发十五、k3cloud单据的附件自动上传到钉钉的审批单中
    微盟与金蝶k3cloud、erp库存对接
    开发一、k3cloud内服务工单过滤在库批号
  • 原文地址:https://www.cnblogs.com/-Ackerman/p/11219478.html
Copyright © 2011-2022 走看看