
- 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 out the maximum number of cells a cleaning robot can clean in a grid
Suppose, we are making a cleaning robot that works on a grid. The grid is of dimensions h x w. There are m dirty cells that need to be cleaned that are given in an array of integer pairs 'dirt'. The cleaning robot, if placed in a particular cell; can clean every cell in that particular row and column. So, our task is to clean the maximum number of dirty cells. We have to find out the count and display it as output.
So, if the input is like h = 3, w = 3, m = 3, dirt = {{0, 0}, {1, 1}, {2, 1}}, then the output will be 3. If we place the cleaning robot at cell {1, 0} then it will be able to clean all the dirty cells in the grid.
To solve this, we will follow these steps −
Define one map Define two arrays hcount and wcount of size: 100 and initialize with 0 maxh = 0, maxw = 0, res = 0 Define two arrays p, q for initialize i := 0, when i < m, update (increase i by 1), do: a := first value of dirt[i] b := second value of dirt[i] pairMap[pair (a, b)] := 1 (increase hcount[a] by 1) (increase wcount[b] by 1) for initialize i := 0, when i < h, update (increase i by 1), do: maxh := maximum of maxh and hcount[i] for initialize i := 0, when i < w, update (increase i by 1), do: maxw := maximum of maxw and wcount[i] for initialize i := 0, when i < h, update (increase i by 1), do: if hcount[i] is same as maxh, then: insert i at the end of p for initialize i := 0, when i < w, update (increase i by 1), do: if wcount[i] is same as maxw, then: insert i at the end of q for each element i in p, do: for each element j in q, do: if pairMap[pair (i, j)] is non-zero, then: res := maxh + maxw - 1 Otherwise, res := maxh + maxw return res return res
Example
Let us see the following implementation to get a better understanding −
#include <bits/stdc++.h> using namespace std; const int INF = 1e9; int solve(int h, int w, int m, vector<pair<int, int>> dirt){ map<pair<int, int>, int> pairMap; int hcount[100] = {0}, wcount[100] = {0}, maxh = 0, maxw = 0, res = 0; vector<int>p, q; for (int i = 0; i < m; i++) { int a = dirt[i].first; int b = dirt[i].second; pairMap[make_pair(a, b)] = 1; hcount[a]++; wcount[b]++; } for (int i = 0; i < h; i++) maxh = max(maxh, hcount[i]); for (int i = 0; i < w; i++) maxw = max(maxw, wcount[i]); for (int i = 0; i < h; i++){ if (hcount[i] == maxh) p.push_back(i); } for (int i = 0; i < w; i++) { if (wcount[i] == maxw) q.push_back(i); } for (auto i : p) { for (auto j : q) { if (pairMap[make_pair(i, j)]) res = maxh + maxw - 1; else { res = maxh + maxw; return res; } } } return res; } int main() { int h = 3, w = 3, m = 3; vector<pair<int, int>> dirt = {{0, 0}, {1, 1}, {2, 1}}; cout<< solve(h, w, m, dirt); return 0; }
Input
3, 3, 3, {{0, 0}, {1, 1}, {2, 1}}
Output
3
- Related Articles
- C++ Program to find out the number of illuminated cells in a grid
- C++ Program to find out the number of border cells in a grid
- C++ Program to find out the number of jumps needed for a robot to reach a particular cell in a grid
- C++ Program to find out the number of cells to block in a grid to create a path
- C++ program to find out the maximum number of cells that can be illuminated
- C++ Program to find out the number of operations to maximize the number of even-numbered cells in a grid
- C++ Program to find out the total cost required for a robot to make a trip in a grid
- Program to Find Out the Number of Squares in a Grid in Python
- C++ program to find out the number of ways a grid with boards can be colored
- C++ Program to find out the maximum number of moves to reach a unblocked cell to another unblocked cell in a grid
- Program to find out the cells containing maximum value in a matrix in Python
- C++ Program to find out the number of sides that a polygon has inside a grid
- C++ Program to find out if there is a pattern in a grid
- C++ program to find out number of changes required to get from one end to other end in a grid
- Program to Find Out the Maximum Final Power of a List in Python

Advertisements