Minimum number of flips required such that the last cell of matrix can be reached from any other cell | Function to calculate the minimum number of flips required ; Dimensions of mat [ ] [ ] ; Initialize answer ; Count all ' D ' s in the last row ; Count all ' R ' s in the last column ; Print answer ; Given matrix ; Function call ; Print answer
def countChanges ( mat ) : NEW_LINE INDENT n = len ( mat ) NEW_LINE m = len ( mat [ 0 ] ) NEW_LINE ans = 0 NEW_LINE for j in range ( m - 1 ) : NEW_LINE INDENT if ( mat [ n - 1 ] [ j ] != ' R ' ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT DEDENT for i in range ( n - 1 ) : NEW_LINE INDENT if ( mat [ i ] [ m - 1 ] != ' D ' ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT arr = [ [ ' R ' , ' R ' , ' R ' , ' D ' ] , [ ' D ' , ' D ' , ' D ' , ' R ' ] , [ ' R ' , ' D ' , ' R ' , ' F ' ] ] NEW_LINE cnt = countChanges ( arr ) NEW_LINE print ( cnt ) NEW_LINE