码迷,mamicode.com
首页 > 其他好文 > 详细

Tempter of the Bone (奇偶剪枝?DFS)

时间:2019-07-20 23:22:09      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:and   you   following   bottom   real   lin   please   algo   std   

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\n");
86         else
87             printf("NO\n");
88     }
89     return 0;
90 }

 

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

Tempter of the Bone (奇偶剪枝?DFS)

标签:and   you   following   bottom   real   lin   please   algo   std   

原文地址:https://www.cnblogs.com/-Ackerman/p/11219478.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!