标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 90990 Accepted Submission(s): 24752
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 int n,m,T; 6 char map[10][10],fail,a2,b2; 7 int visit[10][10]; 8 int f[4][2]= {1,0,-1,0,0,1,0,-1}; 9 int abs(int a) //绝对值函数 10 { 11 return a>=0?a:-a; 12 } 13 void DFS(int x,int y,int k) 14 { 15 int i,nx,ny; 16 if(x==a2&&y==b2) //无论是否时间刚好满足题意,此时都需要返回上一层 17 { 18 if(k==T) //时间相等 19 fail=1; 20 return; 21 } 22 if(k>=T) //超过时间的剪枝 23 return; 24 for(i=0; i<4; i++) 25 { 26 nx=x+f[i][0]; 27 ny=y+f[i][1]; 28 if(nx>=1&&ny>=1&&nx<=n&&ny<=m&&map[nx][ny]!=‘X‘&&!visit[nx][ny]) 29 { 30 visit[nx][ny]=1; 31 DFS(nx,ny,k+1); 32 visit[nx][ny]=0; 33 if(fail) return; 34 } 35 } 36 } 37 int main() 38 { 39 int i,j,a1,b1; 40 while(~scanf("%d%d%d",&n,&m,&T)) 41 { 42 if(n==0&&m==0&&T==0) 43 break; 44 for(i=1; i<=n; i++) 45 { 46 for(j=1; j<=m; j++) 47 { 48 cin>>map[i][j]; 49 if(map[i][j]==‘S‘) //记录起点位置 50 { 51 a1=i; 52 b1=j; 53 } 54 else if(map[i][j]==‘D‘) //终点位置 55 { 56 a2=i; 57 b2=j; 58 } 59 } 60 } 61 if((abs(a2-a1)+abs(b2-b1)>T)||((a1+a2+b1+b2+T)%2==1)) //剪枝,步数不够和奇偶剪枝 62 { 63 printf("NO\n"); 64 continue; 65 } 66 memset(visit,0,sizeof(visit)); //标记步数是否走过 67 visit[a1][b1]=1; 68 fail=0; 69 DFS(a1,b1,0); 70 if(fail) printf("YES\n"); 71 else printf("NO\n"); 72 } 73 return 0; 74 }
hdu 1010 Tempter of the Bone(dfs)
标签:
原文地址:http://www.cnblogs.com/pshw/p/4760452.html