
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ program to find minimum number of operations needed to make all cells at r row c columns black
Suppose we have two numbers r, c and a grid of size n x m. Some cells are in black and remaining are white. In one operation, we can select some black cells and can do exactly one of these two −
- Color all cells in its row black, or
- color all cells in its column black.
We have to find the minimum number of operations needed to make the cells in row r and column c black. If impossible, return -1.
So, if the input is like
W | B | W | W | W |
B | B | B | W | B |
W | W | B | B | B |
r = 0 and c = 3
then the output will be 1, because we can change the first row to make this like −
B | B | B | B | B |
B | B | B | W | B |
W | W | B | B | B |
Steps
To solve this, we will follow these steps −
n := row count of grid m := column count of grid ans := inf for initialize i := 0, when i < n, update (increase i by 1), do: for initialize j := 0, when j < m, update (increase j by 1), do: if matrix[i, j] is same as 'B', then: ans := minimum of ans and (1 if i and r are different, otherwise 0) + (1 if j and c are different, otherwise 0) if ans > 2, then: return -1 Otherwise return ans
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; int solve(vector<vector<char>> matrix, int r, int c) { int n = matrix.size(); int m = matrix[0].size(); int ans = 999999; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { if (matrix[i][j] == 'B') { ans = min(ans, (i != r) + (j != c)); } } } if (ans > 2) { return -1; } else return ans; } int main() { vector<vector<char>> matrix = { { 'W', 'B', 'W', 'W', 'W' }, { 'B', 'B', 'B', 'W', 'B' }, { 'W', 'W', 'B', 'B', 'B' } }; int r = 0, c = 3; cout << solve(matrix, r, c) << endl; }
Input
{ { 'W', 'B', 'W', 'W', 'W' }, { 'B', 'B', 'B', 'W', 'B' }, { 'W', 'W', 'B', 'B', 'B' } }, 0, 3
Output
1
- Related Articles
- C++ program to find minimum how many operations needed to make number 0
- C++ program to find out the number of iterations needed to convert all cells to black
- C++ program to count minimum number of operations needed to make number n to 1
- Find minimum operations needed to make an Array beautiful in C++
- Program to find minimum operations needed to make two arrays sum equal in Python
- Program to count minimum number of operations to flip columns to make target in Python
- C++ program to find minimum number of punches are needed to make way to reach target
- Program to find minimum number of operations to make string sorted in Python
- C++ program to count expected number of operations needed for all node removal
- Program to find minimum number of operations required to make one number to another in Python
- Program to find minimum number of swaps needed to arrange all pair of socks together in C++
- Minimum number of operations on an array to make all elements 0 using C++.
- Program to find number of operations needed to decrease n to 0 in C++
- Program to find minimum number of operations required to make lists strictly Increasing in python
- Find minimum number of merge operations to make an array palindrome in C++

Advertisements