Tempter of the Bone

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

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 #include <algorithm>
 5 #include <cmath>
 6 using namespace std;
 7 int n,m,t;
 8 char maze[10][10];
 9 int maps[10][10];
10 int a[]={1,-1,0,0};
11 int b[]={0,0,1,-1};
12 int ans;
13 int edx,edy;
14 void dfs(int x,int y,int step)
15 {
16     if(ans) return ;//已经成功了。
17     int ex,ey;
18     if(x==edx&&y==edy)
19     {
20         if(step==t) ans=1;
21         return ;
22     }
23     if(step>=t) return ;
24     int temp=t-step-abs(edx-x)-abs(edy-y);
25     if(temp<0||temp&1)  return ;
26     for(int i=0;i<4;i++)
27     {
28         ex=x+a[i];
29         ey=y+b[i];
30         if(ex<1||ex>n||ey<1||ey>m)
31             continue;
32         if(maps[ex][ey]==0&&maze[ex][ey]!='X')
33         {
34             maps[ex][ey]=1;
35           //  cout<<ex<<ey<<endl;
36             dfs(ex,ey,step+1);
37             if(ans) return ;
38             maps[ex][ey]=0;
39         }
40     }
41     return ;
42 }
43 int main()
44 {
45     int stax,stay;
46     while(scanf("%d%d%d",&n,&m,&t)!=EOF)
47     {
48         getchar();
49         if(n==0) break;
50         ans=0;
51         int wall=0;
52         memset(maps,0,sizeof(maps));
53         for(int i=1;i<=n;i++)
54         {
55             for(int j=1;j<=m;j++)
56             {
57                 maze[i][j]=getchar();
58                 if(maze[i][j]=='S')
59                 {
60                     stax=i;
61                     stay=j;
62                 }
63                 if(maze[i][j]=='D')
64                 {
65                     edx=i;
66                     edy=j;
67                 }
68                 if(maze[i][j]=='X')
69                     wall++;
70             }
71             getchar();
72         }
73     /*   for(int i=1;i<=n;i++)
74         {
75             for(int j=1;j<=m;j++)
76             {
77                 printf("%c",maze[i][j]);
78             }
79             printf("
");
80         }
81     */
82         if(t>n*m-wall)
83             printf("NO
");
84         else if (abs(edx-stax)+abs(edy-stay)>=t &&(stax+stay+edx+edy+t)%2==1) printf("NO
");
85         else
86         {
87             memset(maps,0,sizeof(maps));
88             maps[stax][stay]=1;
89             dfs(stax,stay,0);
90             if(ans)
91                 printf("YES
");
92             else
93                 printf("NO
");
94         }
95     }
96     return 0;
97 }
View Code
原文地址:https://www.cnblogs.com/mile-star/p/10597220.html