-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path16236.py
More file actions
69 lines (50 loc) · 1.31 KB
/
Copy path16236.py
File metadata and controls
69 lines (50 loc) · 1.31 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
59
60
61
62
63
64
65
66
67
68
import sys
dir_y = [0,0,-1,1]
dir_x = [-1,1,0,0]
N = int(sys.stdin.readline())
board = []
shark = []
for i in range(N):
temp = list(map(int,sys.stdin.readline().split()))
board.append(temp)
if 9 in temp:
shark = [i,temp.index(9),0]
board[shark[0]][shark[1]] = 0
shark_size = 2
belly = 0
update = True
while(update):
update = False
visit = [[0] * N for i in range(N)]
queue = [shark]
visit[shark[0]][shark[1]] = 1
candi_y, candi_x = 20, 20
candi_t = -1
while(queue):
cur = queue.pop(0)
cur_y = cur[0]
cur_x = cur[1]
cur_t = cur[2]
if candi_t != -1 and candi_t < cur_t:
break
if board[cur_y][cur_x] < shark_size and board[cur_y][cur_x] != 0:
update = True
if candi_y > cur_y or (candi_y == cur_y and candi_x > cur_x):
candi_y, candi_x, candi_t = cur_y, cur_x, cur_t
for i in range(4):
ny = cur_y + dir_y[i]
nx = cur_x + dir_x[i]
nt = cur_t + 1
if ny < 0 or ny >= N or nx < 0 or nx >= N:
continue
if visit[ny][nx] == 0 and shark_size >= board[ny][nx]:
visit[ny][nx] = 1
queue.append([ny,nx,nt])
if(update):
shark = [candi_y,candi_x,candi_t]
belly += 1
if belly == shark_size:
shark_size += 1
belly = 0
board[shark[0]][shark[1]] = 0
print(shark[2])