hdu1010 Tempter of the Bone

 1 #include <iostream>
 2 #include <string.h>
 3 #include <string>
 4 #include <stdio.h>
 5 #include <queue>
 6 #define aabs(x) (x)>0?(x):-(x)
 7 using namespace std;
 8 int dir[4][2]={
 9     1,0,-1,0,
10     0,1,0,-1};
11 char gird[10][10];
12 int m,n,t;
13 bool vit[10][10];
14 bool success;
15 void init(){
16     int i,j;
17     success=0;
18     for(i=0;i<n;++i)
19         for(j=0;j<m;++j){
20             if(gird[i][j]=='X') vit[i][j]=1;
21             else                vit[i][j]=0;
22         }
23     return ;
24 }
25 
26 void dfs(int x,int y,int step){
27     //cout<<"x="<<x<<" y="<<y<<endl;
28     int i,j;
29     int tx,ty;
30     if(success==1) return;
31     if(gird[x][y]=='D'&&step!=t) return;
32     if(gird[x][y]=='D'&&step==t) {success=1;return;}
33     for(i=0;i<4;++i){
34         tx=x+dir[i][0];    ty=y+dir[i][1];
35         if(tx>=0&&tx<n&&ty<m&&ty>=0&&vit[tx][ty]==0){
36             vit[tx][ty]=1;
37             dfs(tx,ty,step+1);
38             vit[tx][ty]=0;
39         }
40     }
41     return ;
42 }
43 int main(){
44     int i,j;
45     int bx,by;
46     int ex,ey;
47     while(cin>>n>>m>>t){
48     if(n==0&&m==0&&t==0) break;
49         for(i=0;i<n;++i)
50             for(j=0;j<m;++j){
51                 cin>>gird[i][j];
52                 if(gird[i][j]=='S'){
53                     bx=i;    by=j;
54                 }else if(gird[i][j]=='D'){
55                     ex=i;    ey=j;
56                 }
57             }
58         if(  ( (bx+ex)+(by+ey) )%2 != t%2 ) {cout<<"NO"<<endl; continue;}
59         init();
60         vit[bx][by]=1;
61         dfs(bx,by,0);
62         if(success==1) cout<<"YES"<<endl;
63         else           cout<<"NO"<<endl;
64     }
65 
66     return 0;
67 
68 }
69         
原文地址:https://www.cnblogs.com/symons1992/p/3329829.html