The problem is how to find the longest route in a matrix of 0 and 1
We don't have any destination and source , We must find the longest possible route with 1 in matrix
For example in matrix below , the length of our longest way is 8 :
1 0 0 1
1 0 0 1
1 1 1 1
0 1 0 1
Or in this matrix , it's 6 :
0 0 0 1
1 1 0 0
0 1 0 0
1 1 1 1
How can we do that in python?
Now, the fist thing is to define a function which takes as input the matrix you want to "explore" and a starting point:
def take_a_step(matrix, pos=(0,0), available=None):the third arg should be either a matrix with the available places (the points not touched yet), or None for the first iteration; you could also initialize it to a standard value like a matrix of
Trues directly in the arguments, but I'd prefer to do it in the function body after checking forNoneso that the input matrix can be different sizes without causing unnecessary problems.So it would come to something like:
Obviously the code needs checking and testing, and there might be more efficient ways of doing the same thing, but it should be enough to let you start