Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1010
On the map of N * m, Mark "s" as the starting point of the dog, and "D" as the door. Ask if you can reach "D" just at the given t time, and then output "yes, otherwise no, each vertex can only go once.
Train of Thought: DFS problem. Finding a path with exactly the length of t is not necessarily the shortest path, so we cannot simply use BFs.
However, DFS usually times out, So pruning is required. Here we mainly use parity pruning. Refer to the link:
Http://baike.baidu.com/view/7789287.htm? Fr = ALAD
Http://www.slyar.com/blog/depth-first-search-even-odd-pruning.html
The optimized code is as follows: 46 Ms
# Include <iostream> # include <cstring> # include <cstdio> # include <cmath> using namespace STD; char map [9] [9]; int n, m, T, di, DJ; bool escape; int dir [4] [2] = {0,-1 }}; void DFS (INT Si, int SJ, int CNT) {If (CNT> 10000) return; If (escape) return; if (SI> N | SJ> M | Si <= 0 | SJ <= 0) return; if (CNT = T & Si = di & SJ = Dj) Escape = 1; // find the path exactly where T is. If (escape) return; if (CNT> = T) return; // if the number of current steps is greater than T, int I, temp; temp = (T-CNT)-ABS (Si-DI)-ABS (SJ-DJ); // parity pruning. T-CNT indicates the number of remaining steps, the sum of the following two absolute values indicates the shortest path from the current point to the end point. If (temp <0 | temp & 1) return; // The result cannot be smaller than 0 or is the base, the difference must be an even number to solve the problem. For (I = 0; I <4; I ++) {If (Map [Si + dir [I] [0] [SJ + dir [I] [1]! = 'X') {map [Si + dir [I] [0] [SJ + dir [I] [1] = 'X '; DFS (Si + dir [I] [0], SJ + dir [I] [1], CNT + 1 ); map [Si + dir [I] [0] [SJ + dir [I] [1] = '. ';}} return;} int main () {int I, j, Si, SJ; while (CIN >> n >> M >> T) {If (n = 0 & M = 0 & t = 0) break; int wall = 0; for (I = 1; I <= N; I ++) for (j = 1; j <= m; j ++) {CIN> map [I] [J]; if (Map [I] [J] ='s ') {Si = I; SJ = J ;} else if (Map [I] [J] = 'D') {di = I; dj = J ;} else if (Map [I] [J] = 'X') wall ++;} If (N * m-wall <= T )/ /If the total number of steps that can be taken on a map is less than or equal to T, the map cannot be reached. {Cout <"no" <Endl; continue;} escape = 0; Map [Si] [SJ] = 'X'; DFS (Si, SJ, 0 ); if (escape) cout <"yes" <Endl; else cout <"no" <Endl;} return 0 ;}
Hdu-1010 tempter of the bone