-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathC_3.py
78 lines (73 loc) · 2.25 KB
/
C_3.py
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
from itertools import combinations
n = int(input())
a = []
teachers = []
spaces = []
for i in range(n):
a.append(list(input().split()))
for j in range(n):
# 선생님이 존재하는 위치 저장
if a[i][j] == 'T':
teachers.append((i, j))
# 장애물을 설치할 수 있는 위치 저장
if a[i][j] == 'X':
spaces.append((i, j))
# 특정 방향으로 감시를 진행 (학생 발견: True, 학생 미발견: False)
def watch(x, y, direction):
# 왼쪽 방향으로 감시
if direction == 0:
while y >= 0:
if a[x][y] == 'S': # 학생이 있는 경우
return True
if a[x][y] == 'O': # 장애물이 있는 경우
return False
y -= 1
# 오른쪽 방향으로 감시
if direction == 1:
while y < n:
if a[x][y] == 'S': # 학생이 있는 경우
return True
if a[x][y] == 'O': # 장애물이 있는 경우
return False
y += 1
# 위쪽 방향으로 감시
if direction == 2:
while x >= 0:
if a[x][y] == 'S': # 학생이 있는 경우
return True
if a[x][y] == 'O': # 장애물이 있는 경우
return False
x -= 1
# 아래쪽 방향으로 감시
if direction == 3:
while x < n:
if a[x][y] == 'S': # 학생이 있는 경우
return True
if a[x][y] == 'O': # 장애물이 있는 경우
return False
x += 1
return False
# 장애물 설치 이후에, 한 명이라도 학생이 감지되는지 검사
def process():
# 모든 선생의 위치를 하나씩 확인
for x, y in teachers:
# 4가지 방향으로 학생을 감지할 수 있는지 확인
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:
a[x][y] = 'O'
# 학생이 한 명도 감지되지 않는 경우
if not process():
# 원하는 경우를 발견한 것임
find = True
break
for x, y in data:
a[x][y] = 'X'
if find:
print('YES')
else:
print('NO')