forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path6.py
More file actions
83 lines (76 loc) Β· 2.65 KB
/
6.py
File metadata and controls
83 lines (76 loc) Β· 2.65 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
from itertools import combinations
n = int(input()) # 볡λμ ν¬κΈ°
board = [] # 볡λ μ 보 (N x N)
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))
# νΉμ λ°©ν₯μΌλ‘ κ°μλ₯Ό μ§ν (νμ λ°κ²¬: True, νμ λ―Έλ°κ²¬: False)
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:
# 4κ°μ§ λ°©ν₯μΌλ‘ νμμ κ°μ§ν μ μλμ§ νμΈ
for i in range(4):
if watch(x, y, i):
return True
return False
find = False # νμμ΄ ν λͺ
λ κ°μ§λμ§ μλλ‘ μ€μΉν μ μλμ§μ μ¬λΆ
# λΉ κ³΅κ°μμ 3κ°λ₯Ό λ½λ λͺ¨λ μ‘°ν©μ νμΈ
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')