http://acm.hdu.edu.cn/showproblem.php?pid=1010
Tortured me all afternoon
The main topic: from S point to D Point can just walk K step at the beginning thought is wide search, actually is deep search.
DFS multi-optimization before it's over.
#include <stdio.h>#include<stdlib.h>#include<algorithm>#include<math.h>#include<string.h>#include<iostream>#include<queue>#include<ctype.h>using namespacestd;#defineN 10#defineMemset (A, B) memset (A,b,sizeof (a))#defineLson r<<1|1#defineRson r<<1structnode{intX,y,step;} S,e;intn,m,k;intdis[4][2]={{1,0},{-1,0},{0,1},{0,-1}};intVis[n][n];CharMaps[n][n];int is=0;voidDFS (node T) {if( is) return; if(t.x==e.x && t.y==e.y && t.step==k) { is=true; return; } if(t.step>=k)return; intMin= (int) (ABS (t.x-e.x) +abs (t.y-e.y)); if(min>k-t.step)return; if(min%2! = (k-t.step)%2) return; if(T.x==e.x && t.y==e.y)return; Node q; for(intI=0;i<4; i++) {q.x=t.x+dis[i][0]; Q.y=t.y+dis[i][1]; Q.step=t.step+1; if( is|| q.x<0|| Q.x>=n | | q.y<0|| Q.y>=m | | maps[q.x][q.y]=='X') Continue; if(q.x==e.x && q.y==e.y && q.step==k) { is=true; return; } CharCh=MAPS[Q.X][Q.Y]; MAPS[Q.X][Q.Y]='X'; DFS (q); MAPS[Q.X][Q.Y]=ch; }}intMain () { while(SCANF (" %d%d%d", &n,&m,&k), n+m+k) {memset (maps,0); intans=0; for(intI=0; i<n;i++) {scanf ("%s", Maps[i]); for(intj=0; j<m;j++) { if(maps[i][j]=='S') {s.x=i; S.y=J; S.step=0; } Else if(maps[i][j]=='D') {e.x=i; E.y=J; } Elseans++; } } if(K-1>ans) {printf ("no\n"); Continue; } Maps[s.x][s.y]='X'; is=0; DFS (s); if( is==true) printf ("yes\n"); Elseprintf ("no\n"); } return 0;}
Tempter of the Bone---hdu1010 (dfs+ pruning)