经典的深搜
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1010
#include<stdio.h> #include<stdlib.h> char map[10][10]; int n,m,t; int sx,sy,dx,dy; int found; int dir[4][2]={{-1,0},{1,0},{0,1},{0,-1}}; void dfs(int x,int y,int k) { if(t==k&&dx==x&&dy==y) found=1; if(found) return; int v=t-k-abs(dx-x)-abs(dy-y); if(v<0||v&1) return; for(int i=0;i<4;i++) { int xx=dir[i][0]+x; int yy=dir[i][1]+y; if(xx>=0&&xx<n&&yy>=0&&yy<m&&map[xx][yy]!='X') { map[xx][yy]='X'; dfs(xx,yy,k+1); map[xx][yy]='.'; } } } int main() { while(scanf("%d %d %d",&n,&m,&t)) { if(n==0&&m==0&&t==0) break; int remain=0; found=0; int i,j; getchar(); for(i=0;i<n;i++) { for(j=0;j<m;j++) { scanf("%c",&map[i][j]); if(map[i][j]=='S') { sx=i; sy=j; } else if(map[i][j]=='D') { dx=i; dy=j; remain++; } else if(map[i][j]=='.') remain++; } getchar(); } map[sx][sy]='X'; if(remain>=t) dfs(sx,sy,0); if(found) printf("YES\n"); else printf("NO\n"); } return 0; }