-1

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?

MSH
  • 5
  • 1
  • if you don't care about speed, the easy way would be checking at every point the value of the three possible moves and forking every time, having a counter that tells you how many steps you took. I'd suggest starting the search from the four corners of the matrix first, though you'll have to check all possible start positions to make sure you catch the longest path – dbac Nov 24 '21 at 15:27
  • Thanks for your help but how about a recursive function? Is there any way to use it instead of a loop? – MSH Nov 24 '21 at 17:38
  • Yes, it can absolutely work. You will likely need to find a way to pass to the function some sort of state array (probably a boolean mask in this specific case) so you know which points you already passed over – dbac Nov 24 '21 at 17:46
  • Can you help me write that code? – MSH Nov 24 '21 at 18:22
  • This feels very much like a school assignment, so I'd like you to read a couple posts here on meta before giving a full answer. If this is not a school homework or if you still decide to ask, I will try to provide a complete answer as soon as I have time https://softwareengineering.meta.stackexchange.com/questions/6166/open-letter-to-students-with-homework-problems https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions – dbac Nov 24 '21 at 19:01
  • No man,absolutely it's not a school homework and i'm not a high school student too , I have just started learning Python and I am practice every day to improve myself,I'm be so thankful if you help me to improve my skills – MSH Nov 24 '21 at 19:09

1 Answers1

0

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 for None so that the input matrix can be different sizes without causing unnecessary problems.
So it would come to something like:

    if(matrix[pos[0]][pos[1]]==0): 
        return 0 #out of path
    if(available is None):
        #list comprehension to make a new with same dim as the input
        available=[[True for i in range(len(matrix[0]))] for j in range(len(matrix))]
    for i in range(4):
        available[pos[0]][pos[1]]=False #remove current position from available ones
        newpos=pos #+something in each direction
        if(available[newpos[0]][newpos[1]]):
            take_a_step(matrix, newpos, available)
    #save the results for each route, use max()
    return maxres+1

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

dbac
  • 328
  • 1
  • 10
  • Thanks for your help , I will work on it to improve it , again i need to say thanks,it,s so helpful – MSH Nov 26 '21 at 13:18