// At the beginning, I used BFS for continuous wa, BFS is used to find the shortest path, and this question is to find the fixed time // paper-cut parity pruning and DFS # include <stdio. h> # include <queue> # include <math. h> # include <string. h> using namespace STD; # define N 10 char ma [N] [N]; struct node {int X, Y, step;} SS, TT; int dis [4] [2] = {1, 0,-1, 0, 0,-1}, n, m, T; int vis [N] [N]; int judge (int x, int y) {If (MA [x] [Y]! = 'X' & x> = 1 & x <= N & Y> = 1 & Y <= M &&! Vis [x] [Y]) // At the beginning, there are fewer! Vis [x] [Y] causes wa return 1; return 0;} int DFS (int x, int y, int step) {int XX, YY, St, I; if (MA [x] [Y] = 'D' & step = T) return 1; if (Step> = T) return 0; for (I = 0; I <4; I ++) {xx = x + dis [I] [0]; YY = Y + dis [I] [1]; ST = Step + 1; if (Judge (XX, YY) {vis [XX] [YY] = 1; if (DFS (XX, YY, St) return 1; vis [XX] [YY] = 0 ;}} return 0 ;}int main () {int I, J, K; while (scanf ("% d", & N, & M, & T), N | M | T) {for (I = 1; I <= N; I ++) scanf ("% s", ma [I] + 1); k = 0; For (I = 1; I <= N; I ++) for (j = 1; j <= m; j ++) {If (MA [I] [J] ='s ') {ss. X = I; SS. y = J;} If (MA [I] [J] = 'D') {TT. X = I; TT. y = J;} If (MA [I] [J] = 'X') K ++ ;} if (N * m-k <t | (INT) FABS (TT. x-ss.x) + (INT) FABS (TT. y-ss.y) % 2! = T % 2) {printf ("NO \ n"); continue;} memset (VIS, 0, sizeof (VIS); vis [ss. x] [ss. y] = 1; if (DFS (ss. x, SS. y, 0) printf ("Yes \ n"); else printf ("NO \ n");} return 0 ;}
Good DFS + parity Pruning for zoj 2110