- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Program to find out the number of operations to maximize the number of even-numbered cells in a grid
Suppose, we are given a grid of dimensions h * w. Every cell in the grid has a specific value assigned to it. We have to maximize the cells having an even value. To do that, we can select a cell that has not been selected before, and then decrease the value by 1 of the current cell and increase the value by 1 of another cell that is located vertically or horizontally adjacent to the current cell. We print out the number of operations and the cell numbers of the increase and decrease operations. The output will be in the format below −
number of operations
1st (decreased cell position) - (increased cell position)
....
nth (decreased cell position) - (increased cell position)
So, if the input is like h = 3, w = 3, grid = {{2, 3, 4}, {2, 0, 1}, {1, 2, 3}}, then the output will be
4 (0, 1) - (0, 2) (2, 0) - (2, 1) (2, 1) - (2, 2) (0, 2) - (1, 2)
Steps
To solve this, we will follow these steps −
Define a new array result that contains a tuple for initialize i := 0, when i < h, update (increase i by 1), do: tp := 0 for initialize j := 0, when j < w, update (increase j by 1), do: if tp > 0, then: insert tuple(i, j - 1, i, j) at the end of result grid[i, j] := grid[i, j] + tp if grid[i, j] mod 2 is same as 1 and j < w-1, then: grid[i, j] := grid[i, j] - 1 tp := 1 Otherwise tp := 0 tp := 0 for initialize i := 0, when i < h, update (increase i by 1), do: if tp > 0, then: insert tuple(i - 1, w - 1, i, w - 1) at the end of result grid[i, w - 1] := grid[i, w - 1] + tp if grid[i, w - 1] mod 2 is same as 1, then: grid[i, w - 1] := grid[i, w - 1] - 1 tp := 1 Otherwise tp := 0 print(size of result) for initialize i := 0, when i < size of result, update (increase i by 1), do: print('(' + first value of result[i] + ',' + second value of result[i] + '- (' + third value of result[i] + ',' + fourth value of result[i])
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; void solve(int h, int w, vector<vector<int>>grid){ vector<tuple<int,int,int,int>> result; for(int i = 0; i < h; i++){ int tp = 0; for(int j = 0; j < w; j++){ if(tp > 0){ result.push_back(make_tuple(i, j-1, i, j)); grid[i][j] += tp; } if(grid[i][j]%2 == 1 && j < w-1){ grid[i][j] -= 1; tp = 1; } else tp = 0; } } int tp = 0; for(int i = 0; i < h; i++){ if(tp > 0){ result.push_back(make_tuple(i-1, w-1, i, w-1)); grid[i][w-1] += tp; } if(grid[i][w-1]%2 == 1){ grid[i][w-1] -= 1; tp = 1; } else tp = 0; } cout << (int)result.size() << endl; for(int i = 0; i < (int)result.size(); i++){ cout << "(" << get<0>(result[i]) << ", " << get<1>(result[i]) << ")" << " - (" << get<2>(result[i]) << ", " << get<3>(result[i]) << ")"; cout << '\n'; } } int main() { int h = 3, w = 3 ; vector<vector<int>> grid = {{2, 3, 4}, {2, 0, 1}, {1, 2, 3}}; solve(h, w, grid); return 0; }
Input
3, 3, {{2, 3, 4}, {2, 0, 1}, {1, 2, 3}}
Output
4 (0, 1) - (0, 2) (2, 0) - (2, 1) (2, 1) - (2, 2) (0, 2) - (1, 2)
- 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 cells to block in a grid to create a path
- C++ program to find out the maximum number of cells a cleaning robot can clean in a grid
- Program to Find Out the Number of Squares in a Grid in Python
- C++ Program to find out the number of sides that a polygon has inside a grid
- C++ Program to find out the minimum number of operations required to defeat an enemy
- C++ program to find out the maximum number of cells that can be illuminated
- C++ program to find out the number of ways a grid with boards can be colored
- C++ program to find out the number of iterations needed to convert all cells to black
- 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 maximum number of moves to reach a unblocked cell to another unblocked cell in a grid
- C++ Program to find sum of even factors of a number?
- To find sum of even factors of a number in C++ Program?
- C++ program to find out number of changes required to get from one end to other end in a grid
