-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrabbit (1).py
More file actions
167 lines (135 loc) · 4.92 KB
/
rabbit (1).py
File metadata and controls
167 lines (135 loc) · 4.92 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# 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): #OLD CODE
# 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)
#------------------------------------------------------------------------------------------------------------------------------------------
import numpy as np
import sys
import copy
sys.setrecursionlimit(1000000)
def solve_max(arr):
max_val = np.amax(arr)
max_indices = np.where(arr == max_val)
return solve_all(max_indices[0][0], max_indices[1][0])
# 1 2 3
# 4 5 6
# 7 8 9
mat = []
vis = []
def solve_all(i, j):
global mat, vis
count = 0
cond1, cond2, cond3, cond4 = True, True, True, True
vis[i][j] = 1
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
if mat[i][j]>mat[i][j-1]:
mat[i][j-1] += count
else:
mat[i][j] += count
else:
cond1 = False
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
if mat[i][j]>mat[i][j+1]:
mat[i][j+1] += count
else:
mat[i][j] += count
else:
cond2 = False
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
if mat[i][j]>mat[i-1][j]:
mat[i-1][j] += count
else:
mat[i][j] += count
else:
cond3 = False
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
if mat[i][j]>mat[i+1][j]:
mat[i+1][j] += count
else:
mat[i][j] += count
else:
cond4 = False
if safeindex(i, j-1) and (vis[i][j-1] == 0 or cond1):
solve_all(i, j-1)
if safeindex(i, j+1) and (vis[i][j+1] == 0 or cond2):
solve_all(i, j+1)
if safeindex(i-1, j) and (vis[i-1][j] == 0 or cond3):
solve_all(i-1, j)
if safeindex(i+1, j) and (vis[i+1][j] == 0 or cond4):
solve_all(i+1, j)
return
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())
vis = [[0]*c]*r
mat = [0]*r
for j in range(r): #NEW CODE
arr = list(map(int, input().split()))[:c]
mat[j] = arr
new = copy.deepcopy(mat)
solve_max(mat)
ans = 0
for i in range(r):
for j in range(c):
ans += abs(new[i][j] - mat[i][j])
# print(*new)
print("Case #", end = ""); print(l+1, end = ""); print(":", end = " "); print(ans)