HDOJ1010-Tempter of the Bone(搜索+奇偶剪枝)

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

Means:

问你是否可以在第T秒恰好能从‘S’到‘D’点

Solve:

如果路径的长度和最短路(曼哈顿距离)的长度奇偶,才能走,不然就不能走剪枝

Code:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3
 4 #define CLR(x , v)        memset(x , v , sizeof(x))
 5
 6 static const int MAXN = 666;
 7 static const int dirx[4] = {1 , 0 , -1 , 0};
 8 static const int diry[4] = {0 , 1 , 0 , -1};
 9
10 char data[MAXN][MAXN];
11 int n , m , t;
12 int stx , edx , sty , edy;
13 bool flag;
14 bool vis[MAXN][MAXN];
15 int sp;
16
17 void Dfs(int x ,int y , int ti)
18 {
19     if(x == edx && y == edy && ti == t)
20     {
21         flag = 1;
22         return ;
23     }
24     if(flag)
25         return;
26     for(int i = 0 ; i < 4 ; ++i)
27     {
28         int nx = x + dirx[i] , ny = y + diry[i];
29         int tp = abs(edx - nx) + abs(edy - ny);
30         if(nx > n || ny > m || nx < 1 || ny < 1 || vis[nx][ny] || data[nx][ny] == ‘X‘)
31             continue;
32         if(ti + tp + 1 > t || (tp + ti + 1) % 2 != sp)
33             continue;
34         vis[nx][ny] = 1;
35         Dfs(nx , ny , ti + 1);
36         vis[nx][ny] = 0;
37     }
38 }
39 int main()
40 {
41     while(~scanf("%d%d%d" , &n , &m , &t) && (n || m || t))
42     {
43         CLR(data , ‘\0‘);
44         CLR(vis , 0);
45         flag = 0;
46         for(int i = 1 ; i <= n ; ++i)
47         {
48             for(int j = 1 ; j <= m ; ++j)
49             {
50                 scanf(" %c" , &data[i][j]);
51                 if(data[i][j] == ‘S‘)
52                     stx = i , sty = j;
53                 if(data[i][j] == ‘D‘)
54                     edx = i , edy = j;
55             }
56         }
57
58         sp = t & 1;
59
60         int dis = abs(stx - edx) + abs(sty - edy);
61         if(dis > t || dis % 2 != sp)
62         {
63             puts("NO");
64             continue;
65         }
66         vis[stx][sty] = 1;
67         Dfs(stx , sty , 0);
68
69         if(flag)
70             puts("YES");
71         else
72             puts("NO");
73     }
74 }

时间: 2024-08-10 23:05:38

HDOJ1010-Tempter of the Bone(搜索+奇偶剪枝)的相关文章

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): 92175    Accepted Submission(s): 25051 Problem Description The doggie found a bone in an ancient maze, which fascinated him a

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): 75141    Accepted Submission(s): 20531 Problem Description The doggie found a bone in an ancient maze, which fascinated him a

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): 82702    Accepted Submission(s): 22531 Problem Description The doggie found a bone in an ancient maze, which fascinated him a

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): 78390    Accepted Submission(s): 21395 Problem Description The doggie found a bone in an ancient maze, which fascinated him a

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 despe

HDU 1010 Tempter of Bone DFS + 奇偶剪枝

奇偶剪枝: 对于从起始点 s 到达 终点 e,走且只走 t 步的可达性问题的一种剪枝策略. 如下列矩阵 : 从任意 0 到达 任意 0,所需步数均为偶数,到达任意 1 ,均为奇数.反之亦然 所以有,若s走且只走 t 步可到达e,则必有t >= abs(s.x - e.x) + abs(s.y - e.y),且 (t&1) == ((abs(s.x - e.x) + abs(s.y - e.y))&1). 否则必不可达. #include <iostream> #inclu

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): 68206    Accepted Submission(s): 18719 Problem Description The doggie found a bone in an ancient maze, which fascinated him a

【HDU - 1010】Tempter of the Bone(dfs+剪枝)

Tempter of the Bone 直接上中文了 Descriptions: 暑假的时候,小明和朋友去迷宫中寻宝.然而,当他拿到宝贝时,迷宫开始剧烈震动,他感到地面正在下沉,他们意识到这是一个陷阱!他们想尽一切办法逃出去.迷宫是一个大小为 N*M 的长方形,迷宫中有一扇门.一开始,门是关着的,他会在第 t 秒的时间打开.因为,小明和朋友必须在第 t 秒到大门口.每一秒,他都可以向上下左右四个方向移动一个点.一旦他移动了,他刚才所在的点就消失,(这意味着他不能回到他已经走过的点).他不能在一个

hdu 1010 Tempter of the Bone 深搜+剪枝

题意:在一个坐标内,给定起点和终点,问能否恰好在t时刻到达终点. 以前很少写搜索题,所以看到这个题,就按照普通的深搜写了一下,交上去超时了.后来在网上搜了一下才知道,要剪枝才行.可是,我以前从没写过剪枝,不知道怎么剪,就按照别人的思路往下想.看懂以后,我对剪枝的理解是:对于一些没有必要继续搜索的路径,不再往下深搜,提前返回到上一层.花了半天时间调试代码,终于AC了. 奇偶剪枝:根据题目,doggie必须在第t秒到达门口.也就是需要走t-1步.设doggie开始的位置为(sx,sy),目标位置为(