-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdfsbfs-20.py
More file actions
66 lines (50 loc) · 1.26 KB
/
dfsbfs-20.py
File metadata and controls
66 lines (50 loc) · 1.26 KB
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
"""
@file dfsbfs-20.py
@brief 감시 피하기
@desc
선생과 학생을 제외한 빈 곳 중에서 3곳을 선택하여 벽을 세우고
각 선생이 학생을 감시할 수 있는지 체크 - 네 방향으로 탐색
"""
from itertools import combinations as cb
N = int(input())
graph = [input().split() for _ in range(N)]
dx = [-1, 0, 1, 0]
dy = [0, -1, 0, 1]
# 빈 곳 좌표
empty = []
# 선생님 좌표
teachers = []
for i in range(N):
for j in range(N):
if graph[i][j] == 'X':
empty.append((i, j))
elif graph[i][j] == 'T':
teachers.append((i, j))
def search():
for t in teachers:
for i in range(4):
nx, ny = t
while 1:
nx += dx[i]
ny += dy[i]
if nx < 0 or nx > N-1 or ny < 0 or ny > N-1 or graph[nx][ny] == 'O':
break
if graph[nx][ny] == 'S':
return False
return True
answer = 0
# 벽 3개 뽑은 경우의 수
for walls in cb(empty, 3):
# 벽 만들기
for x, y in walls:
graph[x][y] = 'O'
# 탐색
if search():
answer = 1
break
for x, y in walls:
graph[x][y] = 'X'
if answer == 1:
print('YES')
else:
print('NO')