-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
60 lines (48 loc) · 1.29 KB
/
test.py
File metadata and controls
60 lines (48 loc) · 1.29 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
from collections import deque
N = int(input())
arr = [list(map(int,input().split())) for i in range(N)]
for i,j in enumerate(arr):
if 9 in j:
sx,sy= i,j.index(9)
level = 2
cnt = 2
time = 0
dx=[1,-1,0,0]
dy=[0,0,1,-1]
visted=[[0]*N for _ in range(N)]
visted[sx][sy]=1
d = deque()
d.append([sx,sy,0])
ans =[]
while d:
X,Y,count= d.popleft()
for i in range(4):
a=X+dx[i]
b=Y+dy[i]
if 0<=a<N and 0<=b<N:
if visted[a][b]==0 and arr[a][b]<=level:
visted[a][b]=1
if 0<arr[a][b]<level:
ans.append([a,b,count+1])
else:
d.append((a,b,count+1))
print(d,ans)
if not d:
if ans:
ans= sorted(ans,key=lambda x: (x[2],abs(x[0]-sx)+abs(x[1]-sy),x[0],x[1]))
idx,idy = ans[0][0],ans[0][1]
time+=ans[0][2]
cnt-=1
arr[sx][sy]=0
arr[idx][idy]=9
sx,sy = idx,idy
d=deque()
d.append([idx,idy,0])
ans=[]
# count=[[0]*N for _ in range(N)]
visted=[[0]*N for _ in range(N)]
visted[sx][sy]=1
if cnt==0:
level+=1
cnt=level
print(time)