forked from tony9402/baekjoon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
78 lines (69 loc) · 1.93 KB
/
main.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
# Authored by : yj2221
# Co-authored by : -
# Link : http://boj.kr/bf6009039d6b410291eb253279ce0c8f
from collections import deque
import sys
def input():
return sys.stdin.readline().rstrip()
r, c = map(int, input().split())
board = [[-1] * (c+2) for _ in range(r+2)]
visit = [[False] * (c+2) for _ in range(r+2)]
q = deque()
fire_q = deque()
dy = [0, 0, -1, 1]
dx = [-1, 1, 0, 0]
for i in range(1,r+1):
line = input()
for j in range(1,c+1):
if line[j-1]=='#': board[i][j] = 1
elif line[j-1]=='.': board[i][j] = 0
elif line[j-1]=='J':
board[i][j] = 0
q.append([i,j])
elif line[j-1]=='F':
board[i][j] = 2
fire_q.append([i,j])
def check(cur, board):
y, x = cur
return board[y][x] == -1
def bfs(q, cnt):
global r, c
next_q = deque()
while q:
y, x = q.popleft()
if board[y][x] == 2: continue
for i in range(4):
ny, nx = y + dy[i], x + dx[i]
if ny<0 or ny>=r+2 or nx<0 or nx>=c+2: continue
if board[ny][nx] == 1 or board[ny][nx] == 2: continue
if visit[ny][nx]: continue
if check((ny,nx), board):
return True, next_q
next_q.append([ny, nx])
visit[ny][nx] = True
return False, next_q
def fire_bfs(q):
global r, c
next_q = deque()
while q:
y, x = q.popleft()
for i in range(4):
ny, nx = y + dy[i], x + dx[i]
if ny<=0 or ny>=r+1 or nx<=0 or nx>=c+1: continue
if board[ny][nx] == 1 or board[ny][nx] == 2: continue
next_q.append([ny, nx])
board[ny][nx] = 2
return next_q
def solution():
global q, fire_q
cnt = 0
while True:
cnt += 1
chk, q = bfs(q, cnt)
if chk:
return cnt
if not q:
return 'IMPOSSIBLE'
fire_q = fire_bfs(fire_q)
return 'IMPOSSIBLE'
print(solution())