hdoj 1010 Tempter of the Bone

 1 #include <stdio.h>
 2 #include <iostream>
 3 #include <math.h>
 4 
 5 using namespace std;
 6 
 7 int n, m, t, di, dj, success;
 8 char maze[10][10];
 9 
10 void dfs(int i, int j, int cnt)
11 {
12     int temp;
13     if (i == di && j == dj && cnt == t)
14     {
15         success = 1;
16         return;
17     }
18     temp = (t - cnt) - abs(i - di) - abs(j - dj);//剪枝
19     if (temp < 0 || temp % 2)
20         return;
21     if (i > n || j > m || i <= 0 || j <= 0)
22         return;
23     maze[i][j] = 'X';
24     if (maze[i + 1][j] != 'X' && !success)
25         dfs(i + 1, j, cnt + 1);
26     if (maze[i][j + 1] != 'X' && !success)
27         dfs(i, j + 1, cnt + 1);
28     if (maze[i - 1][j] != 'X' && !success)
29         dfs(i - 1, j, cnt + 1);
30     if (maze[i][j - 1] != 'X' && !success)
31         dfs(i, j - 1, cnt + 1);
32     maze[i][j] = '.';
33 }
34 
35 int main()
36 {
37     while (scanf("%d %d %d", &n, &m, &t) != -1 && (n || m || t))
38     {
39         int i, j, wall = 0, si, sj;
40         for (i = 1; i <= n; i++)
41         {
42             for (j = 1; j <= m; j++)
43             {
44                 cin >> maze[i][j];
45                 if (maze[i][j] == 'X')
46                     wall++;
47                 else if (maze[i][j] == 'S')
48                     si = i, sj = j;
49                 else if (maze[i][j] == 'D')
50                     di = i, dj = j;
51             }
52         }
53         if (n * m - wall <= t) //剪枝
54         {
55             printf("NO\n");
56             continue;
57         }
58         success = 0;
59         
60         dfs(si, sj, 0);
61         if (success)
62             printf("YES\n");
63         else
64             printf("NO\n");
65     }
66 
67     return 0;
68 }

posted on 2012-08-11 18:25  Xor<>OR  阅读(124)  评论(0)    收藏  举报

导航