zoj 2110 骨头的诱惑(Tempter of the Bone) DFS

地址  https://vjudge.net/problem/ZOJ-2110

 

初始看就是个简单的DFS 或者BFS

后来卡了半天 才发现 是恰好T秒达到.......

做了剪枝 修改了成功退出的判断条件为 == T 然后ac了

 1 #include <iostream>
 2 #include <vector>
 3 #include <algorithm>
 4 #include <string>
 5 #include <memory.h>
 6 
 7 
 8 using namespace std;
 9 
10 int n, m, t;
11 
12 const int MAX_SIZE = 90;
13 
14 char G[MAX_SIZE][MAX_SIZE];
15 
16 int currx, curry;
17 
18 int addx[] = { 0,0,1,-1 };
19 int addy[] = { 1,-1,0,0 };
20 
21 int endX;
22 int endY;
23 
24 
25 
26 int Dfs(int x, int y, int limit)
27 {
28     int count = 0;
29 
30     if (x < 0 || x >= n || y < 0 || y >= m) {
31         return 0;
32     }
33     if (G[x][y] == 'X')
34         return 0;
35     if (G[x][y] == 'D' && limit == t)
36         return 1;
37     if (limit > t)
38         return 0;
39 
40     int temp = (t - limit) - abs(x - endX) - abs(y - endY);
41     
42     if (temp < 0 )
43         return 0;
44 
45 
46     for (int i = 0; i < 4; i++) {
47         int newx = addx[i] + x;
48         int newy = addy[i] + y;
49         char oldV = G[x][y];
50         G[x][y] = 'X';
51         if (Dfs(newx, newy, limit + 1) == 1)
52             return 1;
53         G[x][y] = oldV;
54     }
55 
56     return 0;
57 }
58 
59 
60 int main()
61 {
62     while (scanf("%d%d%d", &n, &m, &t)) {
63         if (n == m && m== t && t == 0) 
64             break;
65 
66         memset(G, 0, sizeof G);
67 
68         for (int i = 0; i < n; i++)
69             for (int j = 0; j < m; j++) {
70                 cin >> G[i][j];
71                 if (G[i][j] == 'S') {
72                     currx = i;
73                     curry = j;
74                 }
75                 else if (G[i][j] == 'D') {
76                     endX = i;
77                     endY = j;
78                 }
79             }
80         if (Dfs(currx, curry, 0) != 0)
81             printf("YES\n");
82         else
83             printf("NO\n"); 
84     }
85 
86     return 0;
87 }
View Code

猜你喜欢

转载自www.cnblogs.com/itdef/p/12518883.html