hdu1010 dfs

 1 #include<stdio.h>
 2 #include<string.h>
 3 int xx[]={0,0,1,-1};
 4 int yy[]={1,-1,0,0};
 5 int used[15][15];
 6 char map[15][15];
 7 int n,m,t,x2,y2;
 8 typedef struct dian
 9 {
10     int x,y,step;
11 };
12 int abs(int x)
13 {
14     return(x>0?x:-x);
15 }
16 int dfs(int x1,int y1,int step)
17 {
18     int i,j,tx,ty;
19     if (t-step-abs(x2-x1)-abs(y2-y1)<0) return(0);
20     if ((t-step-abs(x2-x1)-abs(y2-y1))%2==1) return(0);
21     if (x1==x2&&y1==y2&&step==t) return(1);
22     for (i=0;i<4;i++)
23     {
24         tx=x1+xx[i]; ty=y1+yy[i];
25         if (tx<0||tx>=n||ty<0||ty>=m||used[tx][ty]==1||map[tx][ty]=='X') continue;
26         used[tx][ty]=1;
27         if (dfs(tx,ty,step+1)==1) return(1);
28         used[tx][ty]=0;
29     }
30     return(0);
31 }
32 int main()
33 {
34     int x1,y1,i,j;
35     while (~scanf("%d%d%d",&n,&m,&t)&&(n!=0||m!=0||t!=0))
36     {
37         getchar();
38         for (i=0;i<n;i++) 
39         {
40             for (j=0;j<m;j++) scanf("%c",&map[i][j]);
41             getchar();
42         }
43         for (i=0;i<n;i++)
44          for (j=0;j<m;j++) 
45          if (map[i][j]=='S') {x1=i; y1=j; }
46          else if (map[i][j]=='D') {x2=i; y2=j; }
47         memset(used,0,sizeof(used));  
48         used[x1][y1]=1;
49         if (dfs(x1,y1,0)==1) printf("YES\n");
50         else printf("NO\n");
51     }
52 }

http://acm.hdu.edu.cn/showproblem.php?pid=1010

posted on 2014-07-16 16:00  xiao_xin  阅读(95)  评论(0)    收藏  举报

导航