-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmaxpathsum.py
87 lines (76 loc) · 2.33 KB
/
maxpathsum.py
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
# Python3 program to Print all possible paths from
# top left to bottom right of a mXn matrix
def findPaths(M, n, allPaths, directions, aux):
path = [0 for d in range((n*2)-1)]
findPathsUtil(M, n, 0, 0, path, 0, allPaths, directions, aux)
def findPathsUtil(M, n,i,j,path,indx, allPaths, directions, aux):
# if we reach the bottom of M, we can only move right
if i==n-1:
for k in range(j,n):
aux.append(M[i][k])
if(len(aux) != len(path)):
print(*aux, sep=' -> ', end=' -> down\n')
allPaths.append(aux[:])
directions.append(' -> down')
path[indx+k-j] = M[i][k]
# if we hit this block, it means one path is completed.
# Add it to paths list and print
print(*path, sep=' -> ', end=' -> down\n')
allPaths.append(path[:])
directions.append(' -> down')
return
# if we reach to the right most corner, we can only move down
if j == n-1:
aux = []
for k in range(0, indx):
aux.append(path[k])
for k in range(i, n):
aux.append(M[k][j])
if(len(aux) != len(path)):
print(*aux, sep=' -> ', end=' -> right\n')
allPaths.append(aux[:])
directions.append(' -> right')
path[indx+k-i] = M[k][j]
# if we hit this block, it means one path is completed.
# Add it to paths list and print
print(*path, sep=' -> ', end=' -> right\n')
allPaths.append(path[:])
directions.append(' -> right')
return
# add current element to the path list
path[indx] = M[i][j]
aux = []
for k in range(0, indx+1):
aux.append(path[k])
# move down in y direction and call findPathsUtil recursively
findPathsUtil(M, n, i+1, j, path, indx+1, allPaths, directions, aux)
# move down in y direction and call findPathsUtil recursively
findPathsUtil(M, n, i, j+1, path, indx+1, allPaths, directions, aux)
def maxSumPath(paths):
max = 0
result = []
index = 0
for i, path in enumerate(paths):
pathAux = path
while (len(pathAux) != 0):
soma = sum(path)
if(max < soma):
result = pathAux[:]
index = i
max = soma
pathAux.pop(0)
print(*result, sep=' -> ', end=directions[index])
if __name__ == '__main__':
allPaths = []
directions = []
aux = []
M = [[3, 1, -4, 1],
[5,-1, 9, 2]
,[6, 5, -3, -5]
,[-8, -9, -7, -9]
]
findPaths(M, 4, allPaths, directions, aux)
print("")
#print(allPaths, " - ", len(allPaths), " elements")
print("")
maxSumPath(allPaths)