C++ program to find out the maximum number of cells a cleaning robot can clean in a grid

C++Server Side ProgrammingProgramming

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
raja
Updated on 23-Feb-2022 07:31:48

Advertisements