-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNumberofPaths.py
30 lines (25 loc) · 895 Bytes
/
NumberofPaths.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
# Created by Elshad Karimov
# Copyright © AppMillers. All rights reserved.
# Number of paths to reach the last cell with given cost in 2D array
def numberOfPaths(twoDArray, row, col, cost):
if cost < 0:
return 0
elif row == 0 and col == 0:
if twoDArray[0][0] - cost == 0:
return 1
else:
return 0
elif row == 0:
return numberOfPaths(twoDArray, 0, col-1, cost - twoDArray[row][col] )
elif col == 0:
return numberOfPaths(twoDArray, row-1, 0, cost - twoDArray[row][col] )
else:
op1 = numberOfPaths(twoDArray, row -1, col, cost - twoDArray[row][col] )
op2 = numberOfPaths(twoDArray, row, col-1, cost - twoDArray[row][col] )
return op1 + op2
TwoDList = [[4,7,1,6],
[5,7,3,9],
[3,2,1,2],
[7,1,6,3]
]
print(numberOfPaths(TwoDList, 3,3, 25))