-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0429.py
More file actions
55 lines (54 loc) · 2.16 KB
/
0429.py
File metadata and controls
55 lines (54 loc) · 2.16 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
def Solution(arr):
a = 1
N = len(arr)
M = len(arr[0])
dx = [-1,+1,0,0]
dy = [0,0,-1,+1]
ax = [-2,+2,0,0]
ay = [0,0,-2,+2]
x,y = [0,0]
answer = []
for _ in range(N*M):
z = 0
while x != M and y != N:
for i in arr:
if i[x][y] == 0:
for j in range(4):
nx = x + dx[j]
ny = y + dy[j]
cx = x + ax[j]
cy = y + ay[j]
if nx >=0 and nx <= M and ny >= 0 and ny <= N:
if i[nx][y] == 0:
x = nx
z += 1
break
if i[x][ny] == 0:
y = ny
z += 1
break
else:
if a != 0:
if cx >=0 and cx <= M and cy >= 0 and cy <= N:
if i[cx][y] == 0:
if cx < x:
x = cx + 1
z += 1
break
else:
x = cx -1
z += 1
break
if i[x][cy] == 0:
if cy < y:
y = cy +1
z += 1
break
else:
y = cy -1
z += 1
break
else:
return -1
answer.append(z)
return min(answer)