hdu-1010-Tempter of the Bone (DFS + parity pruning)

 topic link

 

Ideas: pruning + dfs

We number the parity of the map with 01:
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 
We found that one step from 0 must go to 1, and one step from 1 must go to 0.
That is to say, if the coordinates of the current dog are not the same as the parity of the coordinates of D, then the dog needs to take an odd number of steps.
In the same way, if the coordinates of the dog are the same as the parity of the coordinates of D, then the dog needs to take an even number of steps.
 
That is to say, the dog's coordinates x, y and the remainder of 2 are its parity, and Dxy and the remainder of 2 are the parity of D.
Add the two parities and then take the remainder of 2, and compare the remainder with the remainder of the remainder of the remainder of the time.

 

 1 /*
 2     Name:hdu-1010-Tempter of the Bone
 3     Copyright:
 4     Author:
 5     Date: 2018/4/25 11:14:12
 6     Description:
 7 */
 8 #include <iostream>
 9 #include <cstring>
10 #include <cstdio>
11 #include <algorithm>
12 using namespace std;
13 int n, m ,t, flag, ex, ey, sx, sy; 
14 int dir[4][2] = {1,0,-1,0,0,1,0,-1};
15 char map[10][10];
16 int vis[10][10];
17 void dfs(int x, int y, int steps) {
18     if (steps > t) return ;
19     if (x == ex && y == ey && steps == t) {
20         flag = 1;
21         return;
22     }
23     for (int i=0; i<4; i++) {
24         int xx = x + dir[i][0];
25         int yy = y + dir[i][1];
26         if (xx >= n || yy >=m || xx<0 || yy<0) continue;
27         if (map[xx][yy] == 'X' || vis[xx][yy]) continue;
28         vis[xx][yy] = 1;
29         dfs(xx, yy, steps+1);
30         vis[xx][yy] = 0;
31         if (flag) return ;//剪枝 
32     }
33 }
34 int main()
35 {
36 //    freopen("in.txt", "r", stdin);
37     while (cin>>n>>m>>t, n+m+t) {
38         getchar();
39         memset(vis, 0, sizeof(vis));
40         memset(map, 0, sizeof(map));
41         flag = 0;
42         int wall = 0;
43         for (int i=0; i<n; i++) {
44             scanf("%s", map[i]);
45             for (int j=0; j<m; j++) {
46                 if (map[i][j] == 'S') {
47                     sx = i, sy =j;
48                 }
49                 if (map[i][j] == 'D') {
50                     ex = i, ey = j;
 51                  }
 52                  if (map[i][j] == ' X ' ) {
 53                      wall++ ;
 54                  }
 55              }
 56          }
 57          if (abs(ex - sx) + abs(ey - sy) > t || (ex+ey+sx+sy+t)% 2 == 1 ) { // path pruning + parity pruning 
58              cout<< " NO\n " ;
 59              continue ;
 60          }
 61          if (n*m-wall < t) {
62             cout<<"NO\n";
63             continue;
64         }
65         vis[sx][sy] = 1;
66         dfs(sx, sy, 0);
67         if (flag == 0) {
68             cout<<"NO\n";
69         } else {
70             cout<<"YES\n";
71         }
72     }
73     return 0;
74 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324832985&siteId=291194637