-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy path1010.cpp
123 lines (117 loc) · 2.4 KB
/
1010.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<cstdlib>
#include<iomanip>
#include<vector>
#include<list>
#include<map>
#include<queue>
#include<algorithm>
using namespace std;
int n,m,ex,ey,t;
bool success;
char maze[10][10];
/*
stx ---->开始x坐标
sty ---->开始y坐标
dt ---->花掉时间
*/
void dfs(int stx,int sty,int dt )
{
if(stx<=0||stx>n||sty<=0||sty>m)
return ;
if(stx==ex&&sty==ey&&dt==t)
success=true;
if(success) return ;
int temp=(t-dt)-abs(ex-stx)-abs(ey-sty);
if(temp<0||temp&1) //奇偶剪枝
return ;
/*
0 1 0 1 0 1 0 1 0
1 0 1 0 1 0 1 0 1
0 1 0 1 0 1 0 1 0
1 0 1 0 1 0 1 0 1
0 1 0 1 0 1 0 1 0
1 0 1 0 1 0 1 0 1
0 1 0 1 0 1 0 1 0
1 0 1 0 1 0 1 0 1
无论是从o 开始还是从1开始,
都是 0--->1 或者1--->0 都是奇数步
0-->0 , 1--->1 都是偶数步
*/
//对上下左右的搜索
if(maze[stx][sty+1]!='X') //向右搜索
{
maze[stx][sty+1]='X';
dfs(stx,sty+1,dt+1);
maze[stx][sty+1]='.';
}
if(maze[stx+1][sty]!='X') //向下搜索
{
maze[stx+1][sty]='X';
dfs(stx+1,sty,dt+1);
maze[stx+1][sty]='.';
}
if(maze[stx][sty-1]!='X') //向左搜索
{
maze[stx][sty-1]='X';
dfs(stx,sty-1,dt+1);
maze[stx][sty-1]='.';
}
if(maze[stx-1][sty]!='X') //向上搜索
{
maze[stx-1][sty]='X';
dfs(stx-1,sty,dt+1);
maze[stx-1][sty]='.';
}
return ;
}
int main()
{
int stx,sty,wall;
while(scanf("%d%d%d",&n,&m,&t),n+m+t)
{
getchar();
wall=0; //统计障碍物的个数
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
scanf("%c",&maze[i][j]);
if(maze[i][j]=='S')
{
stx=i; // 标注开始的x轴的位置
sty=j; // 标注开始的y轴的位置
}
else
if(maze[i][j]=='D')
{
ex=i; // 标注结束的x轴的位置
ey=j; // 标注结束的y轴的位置
}
else if(maze[i][j]=='X')
{
wall++;
}
}
getchar();
}
success=false;
maze[stx][sty]='X'; //堵住入口
if( n*m-wall<=t ) //因为只有在t时刻door才打开
printf("NO\n");
else
{
dfs(stx,sty,0);
if(success)
printf("YES\n");
else
printf("NO\n");
}
}
return 0;
}