46 lines
1.1 KiB
Python

m = [
[131, 673, 234, 103, 18],
[201, 96, 342, 965, 150],
[630, 803, 746, 422, 111],
[537, 699, 497, 121, 956],
[805, 732, 524, 37, 331]
]
def getm(file):
matrix = []
for line in open(file, 'r'):
matrix.append(list(map(lambda x: int(x), line.split(','))))
return matrix
def rev(matrix):
n = []
scale = len(matrix)
for j in range(scale):
line = []
for i in range(scale):
line.append(matrix[i][j])
n.append(line)
return n
def trace(matrix):
short = matrix.pop(0)
for line in matrix:
ns = []
for i in range(len(line)):
vs = 0
vl = [short[i]]
for k in range(i - 1, -1, -1):
vs += line[k]
vl.append(vs + short[k])
vs = 0
for k in range(i + 1, len(line)):
vs += line[k]
vl.append(vs + short[k])
ns.append(min(vl) + line[i])
short = ns
return short
#print(min(trace(rev(m))))
print(min(trace(rev(getm('../resource/matrix.txt')))))