백준
1. Python
조합, DFS
from sys import stdin
from itertools import combinations
from copy import deepcopy
dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]
n = int(input())
board = [list(map(str, stdin.readline().split())) for _ in range(n)]
emptys = []
teachers = []
students = []
for i in range(n):
for j in range(n):
if board[i][j] == 'X':
emptys.append([i, j])
elif board[i][j] == 'T':
teachers.append([i, j])
elif board[i][j] == 'S':
students.append([i, j])
def DFS(board, x, y, idx):
global n
if x < 0 or x >= n or y < 0 or y >= n or board[x][y] == 'O':
return
else:
board[x][y] = 'T'
DFS(board, x + dx[idx], y + dy[idx], idx)
def check():
copy_board = deepcopy(board)
for [x, y] in teachers:
for i in range(4):
DFS(copy_board, x, y, i)
for [x, y] in students:
if copy_board[x][y] != 'S':
return False
return True
for case in list(combinations(emptys, 3)):
for [x, y] in case:
board[x][y] = 'O'
if check():
print("YES")
exit()
for [x, y] in case:
board[x][y] = 'X'
print("NO")
DFS
n = int(input())
data = []
teacher = []
result = "NO"
dx = [-1,1,0,0]
dy = [0,0,-1,1]
for i in range(n):
data.append(list(map(str, input().split())))
for j in range(n):
if data[i][j] == 'T':
teacher.append((i,j))
def check():
global teacher, data
for t in teacher:
for i in range(4):
x, y = t
while 0<= x < n and 0<= y < n:
if data[x][y] == 'O':
break
elif data[x][y] == 'S':
return False
x += dx[i]
y += dy[i]
return True
def dfs(count):
global teacher, data, result
if count > 3:
return
if count == 3:
if check():
result = "YES"
return
else:
result = "NO"
for i in range(n):
for j in range(n):
if data[i][j] == 'X':
data[i][j] = 'O'
dfs(count+1)
if result == "YES":
return
data[i][j] = 'X'
dfs(0)
print(result)
조합
from itertools import combinations
n = int(input())
board = []
teachers = []
spaces = []
for i in range(n):
board.append(list(input().split()))
for j in range(n):
if board[i][j] == 'T':
teachers.append((i, j))
if board[i][j] == 'X':
spaces.append((i, j))
def watch(x, y, 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
def process():
for x, y in teachers:
for i in range(4):
if watch(x, y, i):
return True
return False
find = False
for data in combinations(spaces, 3):
for x, y in data:
board[x][y] = 'O'
if not process():
find = True
break
for x, y in data:
board[x][y] = 'X'
if find:
print('YES')
else:
print('NO')
2. C++
#include <bits/stdc++.h>
using namespace std;
int n;
char board[6][6];
vector<pair<int, int> > teachers;
vector<pair<int, int> > spaces;
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;
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});
}
}
}
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';
}