Test instructions: A maze of n*m, from the starting point to the end point, ask whether it can arrive at T unit time;
Idea: DFS traversal various possible situations, parity pruning;
#include <cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<algorithm>using namespacestd;Charmm[ -][ -];intvis[ -][ -];intdir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};intN,m,t,s1,s2,e1,e2,flag;voidDfsintT1,intT2,intStep) { inti,j,k; if(flag)return; if(t1==e1&&t2==e2&&step==t)//The remaining number of steps is the same as the remaining time parity, which can be reached{printf ("yes\n"); flag=1;return; } if((ABS (E1-T1) +abs (e2-t2))%2! = (t-step)%2) { return; } for(i=0;i<4; i++) { intxx=t1+dir[i][0]; intyy=t2+dir[i][1]; if(xx<0|| xx>=n| | yy<0|| yy>=m| | vis[xx][yy]| | mm[xx][yy]=='X')Continue; VIS[XX][YY]=1; DFS (Xx,yy,step+1); VIS[XX][YY]=0; }}intMain () {inti,j,k,cnt; while(SCANF ("%d%d%d", &n,&m,&t)! =EOF) { if(n==0&&m==0&&t==0) Break; memset (MM,0,sizeof(mm)); Cnt=0; for(i=0; i<n;i++) { for(j=0; j<m;j++) {scanf ("%c",&Mm[i][j]); if(mm[i][j]=='S') {S1=i,s2=J; } if(mm[i][j]=='D') {E1=i,e2=j;cnt++; } Else if(mm[i][j]=='.') {CNT++; }}} memset (Vis,0,sizeof(VIS)); VIS[S1][S2]=1;//Initial, mark off the starting point, important!!! flag=0; if(cnt>=t) DFS (S1,S2,0);//number of steps can be greater than time, search, small pruning if(flag==0) printf ("no\n"); } return 0;}
HDU 1010 Tempter of the Bone