http://acm.hdu.edu.cn/showproblem.php?pid=1010
Test instructions: Just give a maze, the puppy must go through the specified number of steps to reach the exit, and each lattice can only walk once.
First of all, let's introduce the odd-and-even pruning:
In this topic, if you use pruning, you can save a lot of time.
In this topic, each time the DFS cycle can be judged by the puppy's current position and the end of the number of steps, if not an even, the description can not reach the end of the cycle, you do not have to continue Dfs.
In this problem, because each lattice can only pass once, so after one time, you can change the point position to ' X ', then I wa for a long time, and then understand each DFS cycle after the location of the point to change back to '. '.
1#include <iostream>2#include <cstring>3#include <string>4 using namespacestd;5 6 Charmap[Ten][Ten];7 intd[][2] = { {1,0}, { -1,0}, {0,1}, {0, -1 } };8 intFlag,n,m,t,dx,dy;9 Ten voidDfsintXintYintTime ) One { A if(x<1|| x>n| | y<1|| Y>m | | flag| | TIME>T)return;//Out of bounds - if(Time = = T && x==dx && y==dy) - { theFlag =1; - return; - } - intS1 = x-DX; + intS2 = y-dy; - intAns = t-time-abs (S1)-ABS (S2);//pruning, if the current remaining required number of steps minus the puppy + if(ans<0|| ans%2)return;//if the number of steps in the current position and end point is not even, the end A for(inti =0; I <4; i++) at { - if(Map[x + d[i][0]][y + d[i][1]] !='X') - { -Map[x + d[i][0]][y + d[i][1]] ='X'; -DFS (x + d[i][0], Y + d[i][1], time+1); -Map[x + d[i][0]][y + d[i][1]] ='.';//You must restore the value of this point back, or it will affect the subsequent DFS in } - } to return; + } - the intMain () * { $ intX,y,wall;Panax Notoginseng while(Cin >> n >> m >> t, n && m &&t) - { the if(!m | |!n | |!t) + { Acout <<"NO"<<Endl; the Continue; + } -Flag =0; $Wall =0; $ for(inti =1; I <= n;i++) - for(intj =1; J <= M; J + +) - { theCIN >>Map[i][j]; - if(Map[i][j] = ='S')Wuyi { thex =i; -y =J; Wu } - if(Map[i][j] = ='D') About { $DX =i; -DY =J; - } - if(Map[i][j] = ='X') Awall++; + } the if(N*m-wall <t)//pruning, if a little less wall than the specified number of steps, it must be no - { $cout <<"NO"<<Endl; the Continue; the } theMap[x][y] ='X'; theDFS (x, Y,0); - if(Flag = =1) cout <<"YES"<<Endl; in Elsecout <<"NO"<<Endl; the } the return 0; About}
HDU 1010 Tempter of the Bone (depth + pruning)