Starting from S to stopping D, and arriving at t at the specified time (not early or late ). Note that each '.' can only go once.
Analysis: DFS, but common DFS and TL are used, so pruning is required. We can think that if we can arrive at, but there are other points around the D point that we can step on, and we can observe that, from a point (not a D point) if the distance from point D (ABS (X-dx) + ABS (Y-dy) is an odd number, the odd number step is required, and the even number is the even number step ,()
Code:
# Include <stdio. h> # include <string. h> # include <stdlib. h >#include <iostream> using namespace STD; # define M 10 const int DX [4] = {0, 0,-1, 1 }; const int dy [4] = {1,-1, 0, 0}; char map [m] [m]; int n, m, T; int Sx, Sy, ex, ey; int limit (int x, int y) {return (Map [x] [Y]! = 'X' & x> = 0 & x <n & Y> = 0 & Y <m);} int DFS (int x, int y, int step) {If (x = ex & Y = ey & step = T) return 1; int NX, NY; int temp = T-step-ABS (X-Ex)-ABS (Y-ey); // T-step is the rest to go, ABS (..) Is the distance from the D point. The subtraction must be an even number. If (temp <0 | temp & 1) return 0; int I; for (I = 0; I <4; I ++) {Nx = x + dx [I]; ny = Y + dy [I]; If (limit (NX, NY )) {map [NX] [NY] = 'X'; If (DFS (NX, NY, step + 1) return 1; Map [NX] [NY] = '. ';}} return 0 ;}int main () {int I, j; while (scanf ("% d", & N, & M, & T), N | M | T) {memset (MAP, 0, sizeof (MAP); for (I = 0; I <n; I ++) {scanf ("% s", map [I]) ;}for (I = 0; I <n; I ++) {for (j = 0; j <m; j ++) {If (Map [I] [J] ='S ') {SX = I; Sy = J;} If (Map [I] [J] = 'D') {EX = I; ey = J ;}} map [SX] [sy] = 'X'; // be sure to initialize int ans = DFS (sx, Sy, 0 ); printf ("% s \ n", ANS? "Yes": "no");} return 0 ;}
Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1010
Hdoj 1010 tempter of the bone [DFS] + [parity pruning]