Find the minimum number of moves needed to move from one cell of matrix to another | Python3 program to find the minimum numbers of moves needed to move from source to destination . ; add edge to graph ; Level BFS function to find minimum path from source to sink ; Base case ; make initial distance of all vertex - 1 from source ; Create a queue for BFS ; Mark the source node level [ s ] = '0 ; it will be used to get all adjacent vertices of a vertex ; Dequeue a vertex from queue ; Get all adjacent vertices of the dequeued vertex s . If a adjacent has not been visited ( level [ i ] < '0' ) , then update level [ i ] = = parent_level [ s ] + 1 and enqueue it ; Else , continue to do BFS ; return minimum moves from source to sink ; Returns minimum numbers of moves from a source ( a cell with value 1 ) to a destination ( a cell with value 2 ) ;  ; create graph with n * n node each cell consider as node Number of current vertex ; connect all 4 adjacent cell to current cell ; source index ; destination index ; find minimum moves ; Driver Code
class Graph : NEW_LINE INDENT def __init__ ( self , V ) : NEW_LINE INDENT self . V = V NEW_LINE self . adj = [ [ ] for i in range ( V ) ] NEW_LINE DEDENT def addEdge ( self , s , d ) : NEW_LINE INDENT self . adj [ s ] . append ( d ) NEW_LINE self . adj [ d ] . append ( s ) NEW_LINE DEDENT def BFS ( self , s , d ) : NEW_LINE INDENT if ( s == d ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT level = [ - 1 ] * self . V NEW_LINE queue = [ ] NEW_LINE level [ s ] = 0 NEW_LINE queue . append ( s ) NEW_LINE while ( len ( queue ) != 0 ) : NEW_LINE INDENT s = queue . pop ( ) NEW_LINE i = 0 NEW_LINE while i < len ( self . adj [ s ] ) : NEW_LINE INDENT if ( level [ self . adj [ s ] [ i ] ] < 0 or level [ self . adj [ s ] [ i ] ] > level [ s ] + 1 ) : NEW_LINE INDENT level [ self . adj [ s ] [ i ] ] = level [ s ] + 1 NEW_LINE queue . append ( self . adj [ s ] [ i ] ) NEW_LINE DEDENT i += 1 NEW_LINE DEDENT DEDENT return level [ d ] NEW_LINE DEDENT DEDENT def isSafe ( i , j , M ) : NEW_LINE INDENT global N NEW_LINE if ( ( i < 0 or i >= N ) or ( j < 0 or j >= N ) or M [ i ] [ j ] == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT return True NEW_LINE DEDENT def MinimumPath ( M ) : NEW_LINE INDENT global N NEW_LINE s , d = None , None NEW_LINE V = N * N + 2 NEW_LINE g = Graph ( V ) NEW_LINE k = 1 NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( N ) : NEW_LINE INDENT if ( M [ i ] [ j ] != 0 ) : NEW_LINE INDENT if ( isSafe ( i , j + 1 , M ) ) : NEW_LINE INDENT g . addEdge ( k , k + 1 ) NEW_LINE DEDENT if ( isSafe ( i , j - 1 , M ) ) : NEW_LINE INDENT g . addEdge ( k , k - 1 ) NEW_LINE DEDENT if ( j < N - 1 and isSafe ( i + 1 , j , M ) ) : NEW_LINE INDENT g . addEdge ( k , k + N ) NEW_LINE DEDENT if ( i > 0 and isSafe ( i - 1 , j , M ) ) : NEW_LINE INDENT g . addEdge ( k , k - N ) NEW_LINE DEDENT DEDENT if ( M [ i ] [ j ] == 1 ) : NEW_LINE INDENT s = k NEW_LINE DEDENT if ( M [ i ] [ j ] == 2 ) : NEW_LINE INDENT d = k NEW_LINE DEDENT k += 1 NEW_LINE DEDENT DEDENT return g . BFS ( s , d ) NEW_LINE DEDENT N = 4 NEW_LINE M = [ [ 3 , 3 , 1 , 0 ] , [ 3 , 0 , 3 , 3 ] , [ 2 , 3 , 0 , 3 ] , [ 0 , 3 , 3 , 3 ] ] NEW_LINE print ( MinimumPath ( M ) ) NEW_LINE