-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrabbit.py
More file actions
55 lines (50 loc) · 1.91 KB
/
rabbit.py
File metadata and controls
55 lines (50 loc) · 1.91 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 safeindex(q, p):
if q >= 0 and q < r and p >= 0 and p < c:
return True
else:
return False
t = int(input())
for l in range(t):
r, c = map(int, input().split())
mat = [0]*r
for j in range(r):
arr = list(map(int, input().split()))[:c]
mat[j] = arr
i, j = 0, 0
count = 0
ans = 0
for i in range(r):
for j in range(c):
if safeindex(i, j-1):
if abs(mat[i][j] - mat[i][j-1]) > 1:
count = abs(mat[i][j] - mat[i][j-1]) - 1
ans += count
if mat[i][j]>mat[i][j-1]:
mat[i][j-1] += count
else:
mat[i][j] += count
if safeindex(i, j+1):
if abs(mat[i][j] - mat[i][j+1]) > 1:
count = abs(mat[i][j] - mat[i][j+1]) - 1
ans += count
if mat[i][j]>mat[i][j+1]:
mat[i][j+1] += count
else:
mat[i][j] += count
if safeindex(i-1, j):
if abs(mat[i][j] - mat[i-1][j]) > 1:
count = abs(mat[i][j] - mat[i-1][j]) - 1
ans += count
if mat[i][j]>mat[i-1][j]:
mat[i-1][j] += count
else:
mat[i][j] += count
if safeindex(i+1, j):
if abs(mat[i][j] - mat[i+1][j]) > 1:
count = abs(mat[i][j] - mat[i+1][j]) - 1
ans += count
if mat[i][j]>mat[i+1][j]:
mat[i+1][j] += count
else:
mat[i][j] += count
print("Case #", end = ""); print(l+1, end = ""); print(":", end = " "); print(ans)