标签:
1 #include<iostream> 2 #include<stdio.h> 3 #include<stdlib.h> 4 using namespace std; 5 6 int a,b; 7 bool flag; 8 int start_x,start_y,end_x,end_y; 9 int d[4][2]={0,1,1,0,0,-1,-1,0}; 10 char map[10][10]; 11 12 void DFS(int x,int y,int t) 13 { 14 int i; 15 if(flag==true) return; 16 if(t<abs(end_x-x)+abs(end_y-y)||(t-abs(end_x-x)+abs(end_y-y))%2) return; 17 else if(t==0) 18 { 19 if(x==end_x&&y==end_y) 20 { 21 flag=true; 22 return; 23 } 24 else return; 25 } 26 else 27 { 28 for(i=0;i<4;i++) 29 { 30 int nx=x+d[i][0],ny=y+d[i][1]; 31 if(nx>=0&&nx<a&&ny>=0&&ny<b&&(map[nx][ny]==‘.‘||map[nx][ny]==‘D‘)) 32 { 33 map[nx][ny]=‘X‘; 34 DFS(nx,ny,t-1); 35 map[nx][ny]=‘.‘; 36 } 37 } 38 return; 39 } 40 } 41 42 int main() 43 { 44 int i,j,t; 45 while(cin>>a>>b>>t&&(a||b||t)) 46 { 47 for(i=0;i<a;i++) 48 cin>>map[i]; 49 for(i=0;i<a;i++) 50 for(j=0;j<b;j++) 51 { 52 if(map[i][j]==‘S‘) 53 { 54 start_x=i; 55 start_y=j; 56 } 57 else if(map[i][j]==‘D‘) 58 { 59 end_x=i; 60 end_y=j; 61 } 62 else continue; 63 } 64 flag=false; 65 DFS(start_x,start_y,t); 66 if(flag==false)cout<<"NO"<<endl; 67 else cout<<"YES"<<endl; 68 } 69 return 0; 70 }
HDU 1010 Temper of the bone(深搜+剪枝)
标签:
原文地址:http://www.cnblogs.com/homura/p/4668740.html