Check if elements of a Binary Matrix can be made alternating | Function to create the possible grids ; Function to test if any one of them matches with the given 2 - D array ; Function to print the grid , if possible ; Function to check if the grid can be made alternating or not ; Grids to store the possible grids ; Driver Code
def createGrid ( grid , is1 , N , M ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT for j in range ( M ) : NEW_LINE INDENT if ( is1 ) : NEW_LINE INDENT grid [ i ] [ j ] = '0' NEW_LINE is1 = False NEW_LINE DEDENT else : NEW_LINE INDENT grid [ i ] [ j ] = '1' NEW_LINE is1 = True NEW_LINE DEDENT DEDENT if ( M % 2 == 0 ) : NEW_LINE INDENT is1 = True if is1 == False else False NEW_LINE DEDENT DEDENT DEDENT def testGrid ( testGrid , Grid , N , M ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT for j in range ( M ) : NEW_LINE INDENT if ( Grid [ i ] [ j ] != ' * ' ) : NEW_LINE INDENT if ( Grid [ i ] [ j ] != testGrid [ i ] [ j ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT DEDENT DEDENT return True NEW_LINE DEDENT def printGrid ( grid , N , M ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT for j in range ( M ) : NEW_LINE INDENT print ( grid [ i ] [ j ] , end = " ▁ " ) NEW_LINE DEDENT print ( " " , end ▁ = ▁ " " ) NEW_LINE DEDENT DEDENT def findPossibleGrid ( N , M , grid ) : NEW_LINE INDENT gridTest1 = [ [ ' ' for i in range ( 1001 ) ] for j in range ( N ) ] NEW_LINE gridTest2 = [ [ ' ' for i in range ( 1001 ) ] for j in range ( N ) ] NEW_LINE createGrid ( gridTest1 , True , N , M ) NEW_LINE createGrid ( gridTest2 , False , N , M ) NEW_LINE if ( testGrid ( gridTest1 , grid , N , M ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE printGrid ( gridTest1 , N , M ) NEW_LINE DEDENT elif ( testGrid ( gridTest2 , grid , N , M ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE printGrid ( gridTest2 , N , M ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 4 NEW_LINE M = 4 NEW_LINE grid = [ [ ' * ' , ' * ' , '1' , '0' ] , [ ' * ' , ' * ' , ' * ' , ' * ' ] , [ ' * ' , ' * ' , ' * ' , ' * ' ] , [ ' * ' , ' * ' , '0' , '1' ] ] NEW_LINE findPossibleGrid ( N , M , grid ) NEW_LINE DEDENT