HDU 1010 Tempter of the Bone

题目大意是要求小狗在指定的时间恰好到达D点。由于不是求最短路线,故用DFS要简单。题目难点就是剪枝了:

1.记录可行的个数,如果可行点比指定的时间还短,那么是肯定到不了的,可以剪去。

2.奇偶剪枝,具体原理见链接:http://blog.csdn.net/chyshnu/article/details/6171758

AC code:

View Code
 1 #include <iostream>
 2 #include <math.h>
 3 using namespace std;
 4 int ln, col, time;
 5 int sx, sy, ex, ey, ok, avilable;
 6 int dir[4][2] = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
 7 char map[10][10];
 8 
 9 int inmap(int x, int y){
10     return x >= 0 && x <ln && y >= 0 && y <col;
11 }
12 void dfs(int x, int y, int tm)
13 {
14     if(x == ex && y == ey && tm == time){
15         ok = 1;
16     }
17     if(ok) return;
18     if(tm > time) return;
19     if((time - tm)%2!=(abs(ex-x)+abs(ey-y))%2)return;//奇偶剪枝
20     for(int i = 0; i < 4; i++)
21     {
22         int tx = x + dir[i][0];
23         int ty = y + dir[i][1];
24         if(inmap(tx, ty) && map[tx][ty] != 'X'){
25             map[tx][ty] = 'X';
26             dfs(tx, ty, tm + 1);
27             map[tx][ty] = '.';
28         }
29     }
30 }
31 int main()
32 {
33     while(scanf("%d%d%d", &ln, &col, &time) != EOF && ln)
34     {
35         avilable = 0;//记录可行点个数
36         for (int i = 0; i < ln; i++)
37         {
38             for (int j = 0; j < col; j++)
39             {
40                 cin >> map[i][j];
41                 if (map[i][j] == 'S')
42                 {
43                     sx = i;
44                     sy = j;
45                     map[sx][sy] = 'X';
46                     avilable++;
47                 }
48                 if (map[i][j] == '.')
49                     avilable++;
50                 if (map[i][j] == 'D')
51                 {
52                     ex = i;
53                     ey = j;
54                     avilable++;
55                 }
56             }
57         }
58         if(avilable < time){
59             cout <<"NO" <<endl;
60             continue;
61         }
62         ok = 0;
63         dfs(sx, sy, 0);
64         if(ok)
65             cout << "YES" << endl;
66         else 
67             cout << "NO" << endl;
68     }
69 }

 

posted @ 2012-04-30 11:39  背着超人飞  阅读(293)  评论(0)    收藏  举报