
- 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 number of iterations needed to convert all cells to black
Suppose, we are given a grid that contains two types of cells; black cells, and white cells. The black cells are represented as '#' and the white cells are represented as '.'. The grid is given to us in an array of strings. Now, we have to perform the following.
We convert each white cell to black that has a side shared with a black cell. We perform this operation until every cell of the grid is black.
We count the number of iterations it takes to convert all cells of the grid to black. The grid at the start must contain one black cell.
So, if the input is like h = 4, w = 4, grid = {"#...", ".#.." , "....", "...#"}
# | . | . | . |
. | # | . | . |
. | . | . | . |
. | . | . | # |
then the output will be 3.
It takes 3 iterations to convert all cells to black.
Steps
To solve this, we will follow these steps −
Define an array dx of size: 4 containing := { 1, 0, - 1, 0 } Define an array dy of size: 4 containing := { 0, 1, 0, - 1 } Define one 2D array distance Define one queue q that contain integer pairs for initialize i := 0, when i < h, update (increase i by 1), do: for initialize j := 0, when j < w, update (increase j by 1), do: if grid[i, j] is same as '#', then: distance[i, j] := 0 insert one pair(i, j) into q while (not q is empty), do: first element of auto now = q delete element from q for initialize dir := 0, when dir < 4, update (increase dir by 1), do: cx := first value of now + dx[dir] cy := second value of now + dy[dir] if cx < 0 or cx >= h or cy < 0 or cy >= w, then: if distance[cx, cy] is same as -1, then: distance[cx, cy] := distance[first value of now, second value of now] + 1 insert one pair (cx, cy) into q ans := 0 for initialize i := 0, when i < h, update (increase i by 1), do: for initialize j := 0, when j < w, update (increase j by 1), do: ans := maximum of ans and distance[i, j] print(ans)
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 <string> grid){ int dx[4] = { 1, 0, -1, 0 }; int dy[4] = { 0, 1, 0, -1 }; vector<vector<int>> distance(h, vector<int>(w, -1)); queue<pair<int, int>> q; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (grid[i][j] == '#') { distance[i][j] = 0; q.push(pair<int, int>(i,j)); } } } while (!q.empty()) { auto now = q.front(); q.pop(); for (int dir = 0; dir < 4; dir++) { int cx = now.first + dx[dir]; int cy = now.second + dy[dir]; if (cx < 0 || cx >= h || cy < 0 || cy >= w) continue; if (distance[cx][cy] == -1) { distance[cx][cy] = distance[now.first][now.second] + 1; q.push(pair<int, int> (cx, cy)); } } } int ans = 0; for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { ans = max(ans, distance[i][j]); } } cout << ans << endl; } int main() { int h = 4, w = 4; vector<string> grid = {"#...", ".#.." , "....", "...#"}; solve(h, w, grid); return 0; }
Input
4, 4, {"#...", ".#.." , "....", "...#"}
Output
3
- Related Articles
- C++ program to find minimum number of operations needed to make all cells at r row c columns black
- 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 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 number of cells to block in a grid to create a path
- Program to find number of operations needed to convert list into non-increasing list in Python
- Program to find minimum number of swaps needed to arrange all pair of socks together in C++
- Python Program to find out the sum of values in hyperrectangle cells
- Program to find number of coins needed to make the changes in Python
- 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 cells a cleaning robot can clean in a grid
- C++ program to find sum of all cells lighten by the lamps
- C++ program to count expected number of operations needed for all node removal
- Program to find number of operations needed to decrease n to 0 in C++
