-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path16236.py
60 lines (47 loc) · 1.21 KB
/
16236.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
# 아기 상어
import sys
import heapq
input = sys.stdin.readline
sys.setrecursionlimit(10**6)
N = int(input())
space = []
for y in range(N):
line = list(map(int, input().split()))
for x in range(N):
if line[x] == 9:
startY, startX = y, x
line[x] = 0
space.append(line)
sum_time = 0
size = 2
def move(y, x):
global sum_time, size
check = [[False] * N for _ in range(N)]
eat = False
next_move = []
heapq.heappush(next_move, [0, y, x])
time = 0
while next_move:
t, y, x = heapq.heappop(next_move)
if x < 0 or y < 0 or x >= N or y >= N:
continue
if space[y][x] > int(size):
continue
if 0 < space[y][x] < int(size):
time = t
eat = True
space[y][x] = 0
break
if check[y][x]:
continue
check[y][x] = True
heapq.heappush(next_move, [t + 1, y - 1, x])
heapq.heappush(next_move, [t + 1, y, x - 1])
heapq.heappush(next_move, [t + 1, y, x + 1])
heapq.heappush(next_move, [t + 1, y + 1, x])
sum_time += time
size += 1 / int(size)
if eat:
move(y, x)
move(startY, startX)
print(sum_time)