-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4179.py
More file actions
49 lines (45 loc) · 1.34 KB
/
4179.py
File metadata and controls
49 lines (45 loc) · 1.34 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
from collections import deque
r, c = map(int, input().split())
board = []
dq_j = deque()
dq_f = deque()
for i in range(r):
board.append(list(input().rstrip()))
for j in range(c):
if board[i][j] == 'J': # 지훈 위치
dq_j.append((i, j))
if board[i][j] == 'F': # 불 위치
dq_f.append((i, j))
dy = [1, -1, 0, 0]
dx = [0, 0, 1, -1]
answer = 0
while dq_j: # 지훈이가 움질일 수 있다면 반복
answer += 1
# 불 BFS 1회
next_fires = []
while dq_f:
y, x = dq_f.popleft()
for i in range(4):
ny = y + dy[i]
nx = x + dx[i]
if 0 <= ny < r and 0 <= nx < c:
if board[ny][nx] == '.' or board[ny][nx] == 'J':
board[ny][nx] = 'F'
next_fires.append((ny, nx))
dq_f = deque(next_fires)
# 지훈 BFS 1회
next_jihoon = []
while dq_j:
y, x = dq_j.popleft()
if y == 0 or y == r - 1 or x == 0 or x == c - 1:
print(answer)
exit()
for i in range(4):
ny = y + dy[i]
nx = x + dx[i]
if 0 <= ny < r and 0 <= nx < c:
if board[ny][nx] == '.':
board[ny][nx] = 'J' # 방문처리
next_jihoon.append((ny, nx))
dq_j = deque(next_jihoon)
print('IMPOSSIBLE')