HDOJ_1010 Tempter of the Bone

http://acm.hdu.edu.cn/showproblem.php?pid=1010

奇偶剪枝:
可以把map看成这样:
0 1 0 1 0 1
1 0 1 0 1 0
0 1 0 1 0 1
1 0 1 0 1 0
0 1 0 1 0 1
从为 0 的格子走一步,必然走向为 1 的格子
从为 1 的格子走一步,必然走向为 0 的格子
即:
0 ->1或1->0 必然是奇数步
0->0 走1->1 必然是偶数步
结论:
所以当遇到从 0 走向 0 但是要求时间是奇数的,或者, 从 1 走向 0 但是要求时间是偶数的 都可以直接判断不可达!

 1 #include<stdio.h>
 2 #include<math.h>
 3 #include<string.h>
 4 #include<stdlib.h>
 5 #define N 10
 6 
 7 char maps[N][N];
 8 int dir[4][2] = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
 9 int m, n, t, sx, sy, ex, ey, f;
10 
11 void DFS(int x, int y , int d)
12 {
13     int i, a, b, p, q;
14     if(x < 0 || x >= m || y < 0 || y >= n)
15         return ;
16     if(x == ex && y == ey && d == t)f = 1;
17     if(f == 1)
18         return ;
19     p = t - d;
20     q = abs(x - ex) + abs(y - ey);
21     if(p < q || (p % 2 == 0 && q % 2 != 0) || (p % 2 != 0 && q % 2 == 0))return ;
22     for(i = 0 ; i < 4 ; i++)
23     {
24         a = x + dir[i][0];
25         b = y + dir[i][1];
26         if(a >= 0 && a < m && b >= 0 && b < n && maps[a][b] != 'X')
27         {
28             maps[a][b] = 'X';
29             DFS(a, b, d + 1);
30             maps[a][b] = '.';
31         }
32     }
33     return ;
34 }
35 int main()
36 {
37     int i, j, wall;
38     while(scanf("%d%d%d", &m, &n, &t), m != 0 || n != 0 || t != 0)
39     {
40         f = wall = 0;
41         for(i = 0 ; i < m ; i++)
42         {
43             scanf(" ");
44             for(j = 0 ; j < n ; j++)
45             {
46                 scanf("%c", &maps[i][j]);
47                 if(maps[i][j] == 'S')sx = i, sy = j;
48                 else if(maps[i][j] == 'D')ex = i, ey = j;
49                 else if(maps[i][j] == 'X')wall++;
50             }
51         }
52         if(m * n - wall < t)
53         {
54             printf("NO\n");
55             continue;
56         }
57         maps[sx][sy] = 'X';
58         DFS(sx, sy, 0);
59         if(f == 1)printf("YES\n");
60         else printf("NO\n");
61     }
62     return 0;
63 }

 

posted @ 2015-04-23 12:24  午夜阳光~  阅读(131)  评论(0编辑  收藏  举报