Replace specified matrix elements such that no two adjacent elements are equal | Function to display the valid matrix ; Traverse the matrix ; If the current cell is a free space and is even - indexed ; If the current cell is a free space and is odd - indexed ; Print the matrix ; Given N and M ; Given matrix ; Function call
def Print ( arr , n , m ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT for j in range ( m ) : NEW_LINE INDENT a = arr [ i ] [ j ] NEW_LINE if ( ( i + j ) % 2 == 0 and a == ' F ' ) : NEW_LINE INDENT arr [ i ] [ j ] = '1' NEW_LINE DEDENT elif ( a == ' F ' ) : NEW_LINE INDENT arr [ i ] [ j ] = '2' NEW_LINE DEDENT DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT for j in range ( m ) : NEW_LINE INDENT print ( arr [ i ] [ j ] , end = " " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT n , m = 4 , 4 NEW_LINE arr = [ [ ' F ' , ' F ' , ' F ' , ' F ' ] , [ ' F ' , ' O ' , ' F ' , ' F ' ] , [ ' F ' , ' F ' , ' O ' , ' F ' ] , [ ' F ' , ' F ' , ' F ' , ' F ' ] ] NEW_LINE Print ( arr , n , m ) NEW_LINE