Is there any way to check if the neighbors of edge element in an 2-D matrix array?

I’m trying to find the neighbors of each element in an 2-D NumPy matrix array for the Breadth-First Search algorithm. I have come to a code that was able to walk through an array in which the number of rows equal to number of columns. However, I failed to implement the code when the number of rows does not equal to the number of columns. Can someone give me any suggestion to make my code works properly?

def BreadthFirstSearch(max_row, max_col):     visited = list()     nam_queue = list()     start = (0, 0)     nam_queue.append(start)     while len(nam_queue) != 0:         coor = nam_queue[0]         nam_queue.pop(0)         visited.append(coor)          dx = coor[0]         dy = coor[1]          # Check Neighbors         if dx >= max_row-1:             if dy >= max_col-1:                 continue             else:                 below_val = (dx, dy+1)                 if below_val not in nam_queue and below_val not in visited:                     nam_queue.append(below_val)         elif dy >= max_col-1:             if dx >= max_col-1:                 continue             else:                 next_val = (dx+1, dy)                 if next_val not in nam_queue and next_val not in visited:                     nam_queue.append(next_val)         else:             next_val = (dx+1, dy)             below_val = (dx, dy+1)             if next_val not in nam_queue and next_val not in visited:                 nam_queue.append(next_val)              if below_val not in nam_queue and below_val not in visited:                 nam_queue.append(below_val)      return visited 
Add Comment
0 Answer(s)

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.