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

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

时间:2017-06-03 22:31:12      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:stat   its   ted   multiple   min   efi   分享   eal   blog   

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 }
View Code

 

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

标签:stat   its   ted   multiple   min   efi   分享   eal   blog   

原文地址:http://www.cnblogs.com/jianglingxin/p/6938646.html

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