Minimize flips required to make all shortest paths from top | C ++ program for the above approach ; Function to count the minimum number of flips required ; Dimensions of matrix ; Stores the count the flips ; Check if element is same or not ; Return the final count ; Driver Code ; Given Matrix ; Given path as a string ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minFlips ( vector < vector < int > > & mat , string s ) { int N = mat . size ( ) ; int M = mat [ 0 ] . size ( ) ; int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( mat [ i ] [ j ] != s [ i + j ] - '0' ) { count ++ ; } } } return count ; } int main ( ) { vector < vector < int > > mat = { { 1 , 0 , 1 } , { 0 , 1 , 1 } , { 0 , 0 , 0 } } ; string s = "10001" ; cout << minFlips ( mat , s ) ; return 0 ; }