def calcPath(trace_map, x, y):
n = len(trace_map)
count = 0
if x > n - 1 or y > n - 1:
pass
elif x < n and y < n:
if x + trace_map[x][y] == (n - 1) and y == (n - 1):
count += 1
elif x == (n - 1) and y + trace_map[x][y] == (n - 1):
count += 1
else:
calcPath(trace_map, x + trace_map[x][y], y)
calcPath(trace_map, x, y + trace_map[x][y])
return count
if __name__ == "__main__":
trace_map = [
[1, 2, 9, 4, 9],
[9, 9, 9, 9, 9],
[9, 3, 9, 9, 2],
[9, 9, 9, 9, 9],
[9, 9, 9, 1, 0],
]
print(calcPath(trace_map, 0, 0))
trace_map = [[1, 1, 1], [1, 1, 2], [1, 2, 0]]
print(calcPath(trace_map, 0, 0))
I want to count the existing routes of the given maze. (anyway, the problem itself is not that important) Problem is, I tried to count the number of cases that fit the conditions within the recursive functions.
These are two conditions that have to be counted.
if x + trace_map[x][y] == (n - 1) and y == (n - 1):
if x == (n - 1) and y + trace_map[x][y] == (n - 1):
I tried counting the conditions like this
count = 0
if condition = True:
count +=1
But since I'm using recursive functions, if I declare count = 0 in the function, the count value stays 0.
Shortly, I just want to keep the counter unaffected by the recursive function.
calcPath.counter
and initialize it outside the function. β Metaphrasecount += calcPath(...)
instead of discarding the returned count β Hyperform