-
Notifications
You must be signed in to change notification settings - Fork 1
/
100000609-01.cpp
120 lines (109 loc) · 1.95 KB
/
100000609-01.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
/*
* hint:
* 尚未通过,但已通过以下测试样例:
*
5
.......A
........
........
........
SSSSSSSS
........
........
U.......
.......A
........
........
........
........
.S......
S.......
US......
....S..A
....S...
....S...
....S...
....S...
....S...
S...S...
US..S...
....S..A
....S...
....SSSS
........
........
.S......
S.......
U.......
....S..A
SS.SS...
....SSSS
SSS.....
...S....
.S.S.S..
S.......
U.......
*/
#include <cstdio>
#define SIZE 8
const int offset_x[] = {-1, 0, -1, 1, -1, 1, 0, 1, 0},
offset_y[] = {1, 1, 0, 1, -1, 0, -1, -1, 0};
bool matrix[SIZE][SIZE]; // true indicating there is a stone
int clock;
void initialize()
{
for (int i = 0; i < SIZE; i++)
for (int j = 0; j < SIZE; j++)
matrix[i][j] = false;
clock = 1;
}
void input()
{
char s[SIZE + 1];
for (int i = 0; i < SIZE; i++)
{
scanf("%s", s);
for (int j = 0; j < SIZE; j++)
if (s[j] == 'S')
matrix[i][j] = true;
}
}
bool is_valid_pos(int x, int y)
{
return (0 <= x && x < SIZE) &&
(0 <= y && y < SIZE) &&
!matrix[x - clock][y] &&
!matrix[x - clock + 1][y];
}
bool dfs(int x, int y)
{
// 递归边界
if (clock >= SIZE || (x == 0 && y == 7)) // 在 8 次坠落中,均存活下来了 or 到达终点
return true;
// 循环分岔口:9 个方向(包含一个停留在原地)
for (int i = 0; i < 8; i++) // for each direction
{
int new_x = x + offset_x[i],
new_y = y + offset_y[i];
if (is_valid_pos(new_x, new_y))
{
clock++;
if (dfs(new_x, new_y))
return true;
clock--;
}
}
return false;
}
int main()
{
int n;
scanf("%d\n", &n);
for (int i = 1; i <= n; i++)
{
initialize();
input();
printf(dfs(SIZE - 1, 0) ? "Case #%d: Yes\n" : "Case #%d: No\n", i);
}
return 0;
}