-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path불!.py
More file actions
58 lines (46 loc) · 1.53 KB
/
불!.py
File metadata and controls
58 lines (46 loc) · 1.53 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
import sys
from collections import deque
arr = []
n, m = map(int, input().split())
for i in range(n):
arr.append(list(input().rstrip()))
visited_fire = [[-1 for _ in range(1001)] for _ in range(1001)]
visited_person = [[-1 for _ in range(1001)] for _ in range(1001)]
que_fire = deque()
que_person = deque()
dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]
for i in range(n):
for j in range(m):
if arr[i][j] == 'F':
que_fire.append((i, j))
visited_fire[i][j] = 0
elif arr[i][j] == 'J':
que_person.append((i, j))
visited_person[i][j] = 0
while que_fire:
x, y = que_fire.popleft()
for k in range(4):
nx = x + dx[k]
ny = y + dy[k]
if nx < 0 or nx >= n or ny < 0 or ny >= m:
continue
if visited_fire[nx][ny] >= 0 or arr[nx][ny] == '#':
continue
visited_fire[nx][ny] = visited_fire[x][y] + 1
que_fire.append((nx, ny))
while que_person:
x, y = que_person.popleft()
for k in range(4):
nx = x + dx[k]
ny = y + dy[k]
if nx < 0 or nx >= n or ny < 0 or ny >= m:
print(visited_person[x][y] + 1)
sys.exit()
if visited_person[nx][ny] >= 0 or arr[nx][ny] == '#':
continue
if visited_fire[nx][ny] != -1 and visited_fire[nx][ny] <= visited_person[x][y] + 1:
continue
visited_person[nx][ny] = visited_person[x][y] + 1
que_person.append((nx, ny))
print("IMPOSSIBLE")