Minimum number of flips required such that the last cell of matrix can be reached from any other cell | C ++ program for the above approach ; 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 ; Driver Code ; Given matrix ; Function call ; Print answer
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countChanges ( vector < vector < char > > mat ) { int n = mat . size ( ) ; int m = mat [ 0 ] . size ( ) ; int ans = 0 ; for ( int j = 0 ; j < m - 1 ; j ++ ) { if ( mat [ n - 1 ] [ j ] != ' R ' ) ans ++ ; } for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( mat [ i ] [ m - 1 ] != ' D ' ) ans ++ ; } return ans ; } int main ( ) { vector < vector < char > > arr = { { ' R ' , ' R ' , ' R ' , ' D ' } , { ' D ' , ' D ' , ' D ' , ' R ' } , { ' R ' , ' D ' , ' R ' , ' F ' } } ; int cnt = countChanges ( arr ) ; cout << cnt << endl ; return 0 ; }