Skip to content

Commit 80b720d

Browse files
committed
[BOJ] #18428. 감시 피하기 / 골드5 / 60분 / 실패
1 parent 9fe01ef commit 80b720d

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import sys
2+
input = sys.stdin.readline
3+
4+
# 감시 영역을 확인하는 함수
5+
def is_safe(n, hallway, teachers):
6+
# 선생님 위치로부터 상하좌우 방향을 장애물/벽을 만날 때까지 계속 이동해보면서 학생 존재 여부 탐색
7+
for x, y in teachers: # (0, 4), (1, 0), (3, 1), (4, 2)
8+
for dx, dy in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
9+
nx, ny = x, y
10+
while 0 <= nx < n and 0 <= ny < n:
11+
if hallway[nx][ny] == 'O': # 장애물 만나면 즉시 종료
12+
break
13+
if hallway[nx][ny] == 'S': # 학생이 있으면 감시됨
14+
return False
15+
# 한 쪽 방향으로 장애물 또는 벽을 만날 때까지 계속 이동
16+
nx += dx
17+
ny += dy
18+
# 학생이 없다면 True 반환
19+
return True
20+
21+
def set_obstacles(count):
22+
if count == 3: # 장애물 3개가 설치되면 감시가 안전한지 확인
23+
return is_safe(n, hallway, teachers)
24+
25+
for i in range(n):
26+
for j in range(n):
27+
if hallway[i][j] == 'X':
28+
hallway[i][j] = 'O'
29+
if set_obstacles(count + 1):
30+
return True
31+
hallway[i][j] = 'X'
32+
33+
return False
34+
35+
n = int(input()) # 복도 크기
36+
hallway = [] # 복도 정보
37+
teachers = [] # 선생님 위치 리스트
38+
39+
for i in range(n):
40+
row = list(map(str, input().split()))
41+
hallway.append(row)
42+
for j in range(n):
43+
if hallway[i][j] == 'T':
44+
teachers.append((i, j))
45+
46+
if set_obstacles(0):
47+
print("YES")
48+
else:
49+
print("NO")

0 commit comments

Comments
 (0)