Better search questions, test pruning.
Here we use three pruning methods:
1. parity pruning. The question must be exactly at the time point t to reach the destination, and the puppy cannot stay. If the given time t is not the same as the odd distance from the start point to the end point, then it cannot be reached.
2. Accessibility pruning. If K steps are not taken from the start point to the end point, the number of steps on the map is less than that on the map. No doubt, it is impossible.
3. accessibility pruning 2: the search may be farther and farther away from the target, so it is too late to go back. Therefore, if the time left is smaller than the time left to go to the target without bypassing the target, so you don't have to search any more.
With three pruning methods, Java still gave me 700 + MS, which is not flattering ..
Import Java. util. *; <br/> public class main {</P> <p> static final int maxn = 8; <br/> static char map [] [] = new char [maxn] [maxn]; <br/> static int n, m, T, dx = 0, DY = 0; <br/> static int dir [] [] = {0, 1}, {0,-1}, {-1, 0}, {1, 0 }}; </P> <p> static Boolean inmap (int r, int C) {<br/> return r> = 0 & C> = 0 & R <n & C <m; </P> <p >}< br/> static Boolean DFS (int r, int C, int d) {</P> <p> If (d <Ma Th. ABS (R-dx) + math. ABS (c-dy) return false; <br/> If (D = 0) {<br/> If (r = DX & C = Dy) return true; <br/> return false; <br/>}</P> <p> map [r] [c] = 'X '; <br/> int _ r, _ C, I; <br/> for (I = 0; I <4; I ++) {<br/> _ r = R + dir [I] [0]; <br/> _ C = C + dir [I] [1]; <br/> If (inmap (_ r, _ C) & map [_ r] [_ c]! = 'X') {<br/> If (DFS (_ r, _ c, d-1) return true; <br/>}< br/> map [r] [c] = '. '; <br/> return false; </P> <p >}</P> <p> Public static void main (string []) {</P> <p> memory in = new memory (system. in); <br/> string TMP = new string (); <br/> int I, j, wallnum, SX = 0, Sy = 0; <br/> while (true) {<br/> N = in. nextint (); <br/> M = in. nextint (); <br/> T = in. nextint (); <br/> If (n = 0 & M = 0 & t = 0) break; </P> <p> wallnum = 0; <br/> for (I = 0; I <n; I ++) {<br/> TMP = in. next (); <br/> map [I] = TMP. tochararray (); <br/> for (j = 0; j <m; j ++) {<br/> If (Map [I] [J] = 's') {<br/> SX = I; Sy = J; <br/>} elseif (Map [I] [J] = 'D') {<br/> dx = I; DY = J; <br/>} elseif (Map [I] [J] = 'X') {<br/> wallnum ++; <br/>}</P> <p> If (SX + Sy + dx + dy + T) & 1) = 1 | N * m-wallnum-1 <math. ABS (DX-SX) + math. ABS (dy-sy) {<br/> system. out. println ("no"); <br/> continue; <br/>}</P> <p> If (DFS (sx, Sy, t )) {<br/> system. out. println ("yes"); <br/>} else {<br/> system. out. println ("no"); <br/>}< br/>}