-
Notifications
You must be signed in to change notification settings - Fork 0
/
boj18428.cpp
82 lines (63 loc) · 1.47 KB
/
boj18428.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
#include <iostream>
#include <vector>
using namespace std;
int n;
bool possible;
char map[6][6];
vector<pair<int, int>> teachers, blank;
int dx[] = { 0, 0, -1, 1 };
int dy[] = { -1, 1, 0, 0 };
bool CanMove(int x, int y)
{
if (x < 0 || x > n - 1 || y < 0 || y > n - 1)
return false;
return true;
}
bool CanAvoid()
{
for (pair<int, int> teacher : teachers) {
int x = teacher.first;
int y = teacher.second;
for (int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
while (CanMove(nx, ny) && map[ny][nx] == 'X') {
nx += dx[i];
ny += dy[i];
}
if (CanMove(nx, ny) && map[ny][nx] == 'S')
return false;
}
}
return true;
}
void Watch(int idx, int cnt)
{
if (cnt == 3) {
if (CanAvoid()) possible = true;
return;
}
for (int i = idx; i < blank.size(); i++) {
int x = blank[i].first;
int y = blank[i].second;
map[y][x] = 'O';
Watch(i + 1, cnt + 1);
map[y][x] = 'X';
}
}
int main()
{
cin >> n;
for (int y = 0; y < n; y++) {
for (int x = 0; x < n; x++) {
cin >> map[y][x];
if (map[y][x] == 'T')
teachers.push_back({ x, y });
if (map[y][x] == 'X')
blank.push_back({ x, y });
}
}
Watch(0, 0);
cout << (possible ? "YES" : "NO");
return 0;
}