Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=1010
Title Description: In the n*m matrix, there is a starting point and end point, the middle of the wall, give the starting point and the wall, and give the number of steps, in the case of the steps to the end, the point can not go again;
Key points: dfs+ odd and even pruning;
The subject with DFS can make the result, but will time out, need to use to prune, to subtract most of the possibility;
Parity pruning:
The minimum step of the starting point (tx,ty) and end point (dx, dy) in the square is Minstep=abs (TX-DX) +abs (Ty-dy);
Given the number of steps T, from the starting point to the end point, if T < Minstep, not, if T =minstep, a no more walk, if t> minstep, then more walk extra step, extra=t-minstep;
After deduction, it can be proved that the extra must be even-numbered step.
Now, if you have taken the n step and reached the position (x, y), now the minimum distance from the end point is L=abs (X-DX) +abs (Y-dy); Now also can walk is t-n step, if (t-n) <0, then can't go to place, if (t-n)-L is odd, then can't go out even step to reach the specified position, so this is not possible;
At the same time, the case of the timing card is relatively tight, using DFS is actually the construction of feasible tree, there is M step finally has 2^m leaves, so before entering the next layer of DFS to determine whether feasible can reduce a large portion of leaves;
The code is as follows:
1 ??????????2#include <iostream>3#include <math.h>4 5 using namespacestd;6 intN,m,t,k,dx,dy;7 intP,q,tm;8 intdir[4][2]={{-1,0},{0,1},{1,0},{0,-1}};//constructs an array of positions, traversing around four points;9 Chara[8][8];Ten voidDfsintXintYintcount) One { A inttemp; -Temp=t-count-abs (dx-x)-abs (dy-y); - if(temp<0|| temp%2==1)//determine if there are even steps, do not exit; the return ; - - intTx,ty; - for(intI=0;i<4; i++)//Loop through the surrounding four nodes, first pre-judgment whether to go beyond the boundary, and then determine whether to find the results, if found not to go in; + { -tx=x+dir[i][0]; +ty=y+dir[i][1]; A if(a[tx][ty]=='D'&&count==t-1) at { -k=1; - return ; - } - if(a[tx][ty]=='.'&& (tx>=0&&tx<n) && (ty>=0&&ty<m)) - { ina[tx][ty]='X'; -DFS (tx,ty,count+1); toa[tx][ty]='.'; + if(k==1)//if there is no definite result in the DFS, come out and determine whether there is a result, so that the DFS steps can be reduced; - return ; the } * } $ Panax Notoginseng } - intMain () the { + while(cin>>n>>m>>t&&n!=0|| m!=0|| t!=0) A { thetm=0; + for(intI=0; i<n;i++) - { $ for(intj=0; j<m;j++) $ { -Cin>>A[i][j]; - if(a[i][j]=='S') the { -p=i;q=J;Wuyi } the if(a[i][j]=='D') - { Wudx=i;dy=J; - } About if(a[i][j]=='X') $tm++; - } - } -k=0; A if(n*m-tm<=t)//start to determine whether there is enough space to walk, not directly skip Dfs; + { theprintf"no\n"); - Continue; $ } theDFS (P,Q,0); the if(k==1) theprintf"yes\n"); the Else -printf"no\n"); in } the return 0; the}
View Code
By the way a few DFS topics
hdoj:1010 1015 1016 1045 1175 1181 1241 1272 1421 1455 1518 1728
hdoj--1010<dfs+ Parity pruning >