While I have a matrix as below, from top left corner to find all paths until get '0'. 'D' stands for the bottom element is the next step in trace. 'R' stands for the right element. 'B' stands for both the bottom and the right elements are alternative. 'C' stands for the element on the bottom right corner.
[['R' 'C' 'D' 'B' 'D' 'C' '0']
['C' 'B' 'C' 'B' 'D' 'D' '0']
['B' 'D' 'B' 'B' 'C' 'D' '0']
['R' 'C' 'B' 'D' 'B' 'C' '0']
['B' 'B' 'R' 'C' 'B' 'D' '0']
['R' 'C' 'B' 'R' 'R' 'C' '0']
['C' 'R' 'C' 'B' 'B' 'B' '0']
['0' '0' '0' '0' '0' '0' '0']]
In this case, there are two paths meet requirement, which are
(0, 0)R(0, 1)C(1, 2)C(2, 3)B(2, 4)C(3, 5)C(4, 6)0
(0, 0)R(0, 1)C(1, 2)C(2, 3)B(3, 3)D(4, 3)C(5, 4)R(5, 5)C(6, 6)0
I attempted to use recursive function to find all 'C' in these two paths, since there is a fork in (2, 3)B, the function only returns one of paths completely after joint.
(5, 5)C(4, 3)C
(3, 5)C(2, 4)C(1, 2)C(0, 1)C
So how could I revise my code to obtain whole result?
def printC(matrix, i=0, j=0):
if matrix[i][j] == '0':
print()
return None
elif matrix[i][j] == 'B':
printC(matrix, i+1, j)
printC(matrix, i, j+1)
elif matrix[i][j] == 'D':
printC(matrix, i+1, j)
elif matrix[i][j] == 'R':
printC(matrix, i, j+1)
elif matrix[i][j] == 'C':
printC(matrix, i+1, j+1)
print((i,j), end='')
print(matrix[i][j], end='')
printC(matrix)
print()
Thanks