HDU-1010-Tempter of the Bone
Http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 1010
I have been tossing this question for a long time. I wrote BFS at the beginning, but it was always WA. Later I found that this question was required to arrive at a given point in time. It was neither early nor late, BFS finds the shortest time and does not answer the question each time. Therefore, it switches to DFS. The pruning method is based on the hdu ppt, And the parity must be considered.
You can think of map as follows:
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
Take a step from a grid with 0 to a grid with 1
A step from a grid with 1 is bound to a grid with 0.
0-> 1 or 1-> 0 must be an odd step
0-> 0. 1-> 1 must be an even number.
Therefore, when the request is from 0 to 0 but requires an odd number of times, or from 1 to 0, but requires an even number of times, the system can directly judge whether the request is reachable.
Attach a Test Data
4 4 9
S.. X
X. X.
.. XD
....
YES
# Include <stdio. h> # include <string. h> # include <stdlib. h> # include <math. h> char map [10] [10]; int n, m, t; int xx, yy; int flag; int dir [4] [2] = {-}, {}, {0,-1}, {}; int go (int x, int y) {if (0 <= x & x <n & 0 <= y & y <m) return 1; return 0;} void dfs (int x, int y, int cnt) {int I, min; if (cnt = t & x = xx & y = yy) flag = 1; if (flag) return; min = abs (x-xx) + abs (y-yy); if (min> t-cnt) // the shortest time to the destination is greater than the remaining time return; if (cnt> t) return; if (min % 2! = (T-cnt) % 2) // return with different parity; for (I = 0; I <4; I ++) {if (go (x + dir [I] [0], y + dir [I] [1]) & map [x + dir [I] [0] [y + dir [I] [1]! = 'X') {map [X + dir [I] [0] [y + dir [I] [1] = 'X '; dfs (x + dir [I] [0], y + dir [I] [1], cnt + 1 ); map [x + dir [I] [0] [y + dir [I] [1] = '. ';}} return ;}int main () {int I, j, wall; int x, y; while (scanf ("% d", & n, & m, & t), n | m | t) {for (I = 0; I <n; I ++) scanf ("% s ", map [I]); wall = 0; for (I = 0; I <n; I ++) for (j = 0; j <m; j ++) {if (map [I] [j] = 's') {x = I; y = j ;} else if (map [I] [j] = 'D') {xx = I; yy = j ;} else if (map [I] [j] = 'X') wall ++;} if (n * m-wall <= t) // The accessible path is less than the time {printf ("NO \ n"); continue;} map [x] [y] = 'X'; flag = 0; dfs (X, y, 0); if (flag = 1) printf ("YES \ n"); elseprintf ("NO \ n");} return 0 ;}