Я вышел с этим решением, я использовал методы, чтобы быть более ясным и сухим:
def bordering(start, shape):
PATTERN = [[-1,-1], [-1, 0], [-1, 1], [0, 0], [0, -1], [0, 1], [1, -1], [1, 0], [1, 1]]
border = [[start[0] + x, start[1] + y] for x, y in PATTERN]
valid_border = [item for item in border if item[0] >= 0 and item[1] >= 0 and item[0] <= shape[0] - 1 and item[1] <= shape[1] - 1 ] # removes borders out of the matrix boundaries
return valid_border
def smaller_from(start, matrix):
shape = [len(matrix), len(matrix[0])]
borders = bordering(start, shape)
minimum = min([ [matrix[x][y], [x,y]] for x,y in borders ]) # maps the values and coordinates in valid borders and returns the minimum
return minimum
def find_minimum(start, matrix):
result = []
while True:
val_coords = smaller_from(start, matrix)
result = val_coords
if val_coords[0] >= matrix[start[0]][start[1]]:
break
else:
start = val_coords[1]
return result
matrix = [
[8,90,91,73],
[60,6,32,84],
[50,4,45,94],
[12,85,3,2]]
start = [0, 0]
find_minimum(start, matrix) #=> [2, [3, 3]]