forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path6.cpp
128 lines (119 loc) Β· 3.76 KB
/
6.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
124
125
126
127
128
#include <bits/stdc++.h>
using namespace std;
int n; // 볡λμ ν¬κΈ°
char board[6][6]; // 볡λ μ 보 (N x N)
vector<pair<int, int> > teachers; // λͺ¨λ μ μλ μμΉ μ 보
vector<pair<int, int> > spaces; // λͺ¨λ λΉ κ³΅κ° μμΉ μ 보
// νΉμ λ°©ν₯μΌλ‘ κ°μλ₯Ό μ§ν (νμ λ°κ²¬: true, νμ λ―Έλ°κ²¬: false)
bool watch(int x, int y, int direction) {
// μΌμͺ½ λ°©ν₯μΌλ‘ κ°μ
if (direction == 0) {
while (y >= 0) {
if (board[x][y] == 'S') { // νμμ΄ μλ κ²½μ°
return true;
}
if (board[x][y] == 'O') { // μ₯μ λ¬Όμ΄ μλ κ²½μ°
return false;
}
y -= 1;
}
}
// μ€λ₯Έμͺ½ λ°©ν₯μΌλ‘ κ°μ
if (direction == 1) {
while (y < n) {
if (board[x][y] == 'S') { // νμμ΄ μλ κ²½μ°
return true;
}
if (board[x][y] == 'O') { // μ₯μ λ¬Όμ΄ μλ κ²½μ°
return false;
}
y += 1;
}
}
// μμͺ½ λ°©ν₯μΌλ‘ κ°μ
if (direction == 2) {
while (x >= 0) {
if (board[x][y] == 'S') { // νμμ΄ μλ κ²½μ°
return true;
}
if (board[x][y] == 'O') { // μ₯μ λ¬Όμ΄ μλ κ²½μ°
return false;
}
x -= 1;
}
}
// μλμͺ½ λ°©ν₯μΌλ‘ κ°μ
if (direction == 3) {
while (x < n) {
if (board[x][y] == 'S') { // νμμ΄ μλ κ²½μ°
return true;
}
if (board[x][y] == 'O') { // μ₯μ λ¬Όμ΄ μλ κ²½μ°
return false;
}
x += 1;
}
}
return false;
}
// μ₯μ λ¬Ό μ€μΉ μ΄νμ, ν λͺ
μ΄λΌλ νμμ΄ κ°μ§λλμ§ κ²μ¬
bool process() {
// λͺ¨λ μ μμ μμΉλ₯Ό νλμ© νμΈ
for (int i = 0; i < teachers.size(); i++) {
int x = teachers[i].first;
int y = teachers[i].second;
// 4κ°μ§ λ°©ν₯μΌλ‘ νμμ κ°μ§ν μ μλμ§ νμΈ
for (int i = 0; i < 4; i++) {
if (watch(x, y, i)) {
return true;
}
}
}
return false;
}
bool found; // νμμ΄ ν λͺ
λ κ°μ§λμ§ μλλ‘ μ€μΉν μ μλμ§μ μ¬λΆ
int main(void) {
cin >> n;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> board[i][j];
// μ μλμ΄ μ‘΄μ¬νλ μμΉ μ μ₯
if (board[i][j] == 'T') {
teachers.push_back({i, j});
}
// μ₯μ λ¬Όμ μ€μΉν μ μλ (λΉ κ³΅κ°) μμΉ μ μ₯
if (board[i][j] == 'X') {
spaces.push_back({i, j});
}
}
}
// λΉ κ³΅κ°μμ 3κ°λ₯Ό λ½λ λͺ¨λ μ‘°ν©μ νμΈ
vector<bool> binary(spaces.size());
fill(binary.end() - 3, binary.end(), true);
do {
// μ₯μ λ¬Όλ€μ μ€μΉν΄λ³΄κΈ°
for (int i = 0; i < spaces.size(); i++) {
if (binary[i]) {
int x = spaces[i].first;
int y = spaces[i].second;
board[x][y] = 'O';
}
}
// νμμ΄ ν λͺ
λ κ°μ§λμ§ μλ κ²½μ°
if (!process()) {
// μνλ κ²½μ°λ₯Ό λ°κ²¬ν κ²μ
found = true;
break;
}
// μ€μΉλ μ₯μ λ¬Όμ λ€μ μμ κΈ°
for (int i = 0; i < spaces.size(); i++) {
if (binary[i]) {
int x = spaces[i].first;
int y = spaces[i].second;
board[x][y] = 'X';
}
}
} while(next_permutation(binary.begin(), binary.end()));
if (found) cout << "YES" << '\n';
else cout << "NO" << '\n';
}