C++ Program to Find all rectangles filled with 0
Last Updated :
18 Aug, 2023
We have one 2D array, filled with zeros and ones. We have to find the starting point and ending point of all rectangles filled with 0. It is given that rectangles are separated and do not touch each other however they can touch the boundary of the array.A rectangle might contain only one element.
Examples:
input = [
[1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 0, 0, 0, 1],
[1, 0, 1, 0, 0, 0, 1],
[1, 0, 1, 1, 1, 1, 1],
[1, 0, 1, 0, 0, 0, 0],
[1, 1, 1, 0, 0, 0, 1],
[1, 1, 1, 1, 1, 1, 1]
]
Output:
[
[2, 3, 3, 5], [3, 1, 5, 1], [5, 3, 6, 5]
]
Explanation:
We have three rectangles here, starting from
(2, 3), (3, 1), (5, 3)
Input = [
[1, 0, 1, 1, 1, 1, 1],
[1, 1, 0, 1, 1, 1, 1],
[1, 1, 1, 0, 0, 0, 1],
[1, 0, 1, 0, 0, 0, 1],
[1, 0, 1, 1, 1, 1, 1],
[1, 1, 1, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1, 1],
[1, 1, 0, 1, 1, 1, 0]
]
Output:
[
[0, 1, 0, 1], [1, 2, 1, 2], [2, 3, 3, 5],
[3, 1, 4, 1], [5, 3, 5, 6], [7, 2, 7, 2],
[7, 6, 7, 6]
]
Step 1: Look for the 0 row-wise and column-wise
Step 2: When you encounter any 0, save its position in the output array, and using loop change all related 0 with this position in any common number so that we can exclude it from processing next time.
Step 3: When you change all related 0 in Step 2, store the last processed 0's location in the output array in the same index.
Step 4: Take Special care when you touch the edge, by not subtracting -1 because the loop has broken on the exact location.
Below is the implementation of the above approach:
C++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
void findend(int i, int j, vector<vector<int> >& a,
vector<vector<int> >& output, int index)
{
int x = a.size();
int y = a[0].size();
// flag to check column edge case,
// initializing with 0
int flagc = 0;
// flag to check row edge case,
// initializing with 0
int flagr = 0;
int n, m;
for (m = i; m < x; m++) {
// loop breaks where first 1 encounters
if (a[m][j] == 1) {
flagr = 1; // set the flag
break;
}
// pass because already processed
if (a[m][j] == 5)
continue;
for (n = j; n < y; n++) {
// loop breaks where first 1 encounters
if (a[m][n] == 1) {
flagc = 1; // set the flag
break;
}
// fill rectangle elements with any
// number so that we can exclude
// next time
a[m][n] = 5;
}
}
if (flagr == 1)
output[index].push_back(m - 1);
else
// when end point touch the boundary
output[index].push_back(m);
if (flagc == 1)
output[index].push_back(n - 1);
else
// when end point touch the boundary
output[index].push_back(n);
}
void get_rectangle_coordinates(vector<vector<int> > a)
{
// retrieving the column size of array
int size_of_array = a.size();
// output array where we are going
// to store our output
vector<vector<int> > output;
// It will be used for storing start
// and end location in the same index
int index = -1;
for (int i = 0; i < size_of_array; i++) {
for (int j = 0; j < a[0].size(); j++) {
if (a[i][j] == 0) {
// storing initial position
// of rectangle
output.push_back({ i, j });
// will be used for the
// last position
index = index + 1;
findend(i, j, a, output, index);
}
}
}
cout << "[";
int aa = 2, bb = 0;
for (auto i : output) {
bb = 3;
cout << "[";
for (int j : i) {
if (bb)
cout << j << ", ";
else
cout << j;
bb--;
}
cout << "]";
if (aa)
cout << ", ";
aa--;
}
cout << "]";
}
// Driver code
int main()
{
vector<vector<int> > tests = {
{ 1, 1, 1, 1, 1, 1, 1 }, { 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 0, 0, 0, 1 }, { 1, 0, 1, 0, 0, 0, 1 },
{ 1, 0, 1, 1, 1, 1, 1 }, { 1, 0, 1, 0, 0, 0, 0 },
{ 1, 1, 1, 0, 0, 0, 1 }, { 1, 1, 1, 1, 1, 1, 1 }
};
get_rectangle_coordinates(tests);
return 0;
}
// This code is contributed by mohit kumar 29.
Output:
[[2, 3, 3, 5], [3, 1, 5, 1], [5, 3, 6, 5]]
Time Complexity:O(n^2)
Space Complexity: O(1)
Please refer complete article on Find all rectangles filled with 0 for more details!
Similar Reads
C++ Program to Find Diagonal of a Rectangle
Given two positive integers i.e, the length and breadth of the rectangle, the task is to find the length of the Diagonal of a Rectangle. Example: Input: length=4 breadth=3 output: The Diagonal is 5 The diagonal of a rectangle is sqrt (a*a+ b*b) C++ // C++ Program to Find Diagonal of a Rectangle #inc
1 min read
C++ Program To Find Area And Perimeter Of Rectangle
A rectangle is a flat figure in a plane. It has four sides and four equal angles of 90 degrees each. In a rectangle all four sides are not of equal length like squares, sides opposite to each other have equal length. Both diagonals of the rectangle have equal lengths. Examples: Input: 4 5 Output: Ar
1 min read
Total number of unit cells covered by all given Rectangles
Given a matrix A[][] consisting of coordinates of N rectangles such that {A[i][0], A[i][1]} representing bottom left coordinate of rectangle and {A[i][2], A[i][3]} representing top right coordinate of rectangle, the task is to find the total number of cells covered by all rectangles. Examples: Input
8 min read
Draw an rectangle using OpenCV in C++
In this article, the task is to draw an rectangle using OpenCV in C++. The rectangle() function from OpenCV C++ library will be used. Syntax: rectangle( img, pt1, pt2, color, thickness, line Type, shift) Parameters: image: It is the image on which the rectangle is to be drawn.start(pt1): It is the t
4 min read
C++ Program To Print The Diamond Shape
Given a number n, write a program to print a diamond shape with 2n-1 rows.Examples : Input: 5Output: C++ // C++ program to print diamond shape // with 2n-1 rows #include <bits/stdc++.h> using namespace std; // Prints diamond pattern with 2n-1 rows void printDiamond(int n) { int space = n - 1;
2 min read
Find vertex coordinates of all possible rectangles with a given vertex and dimensions
Given two integers L and B representing the length and breadth of a rectangle and a coordinate (X, Y) representing a point on the cartesian plane, the task is to find coordinates of all rectangles having a vertex as (X, Y) of the given dimensions. Example: Input: X=9, Y=9, L=5, B=3Output:(9, 9), (14
8 min read
C++ Program For Sorting A Linked List Of 0s, 1s And 2s
Given a linked list of 0s, 1s and 2s, sort it.Examples: Input: 1 -> 1 -> 2 -> 0 -> 2 -> 0 -> 1 -> NULL Output: 0 -> 0 -> 1 -> 1 -> 1 -> 2 -> 2 -> NULL Input: 1 -> 1 -> 2 -> 1 -> 0 -> NULL Output: 0 -> 1 -> 1 -> 1 -> 2 -> NULL So
3 min read
C++ Program To Print Triangle Pattern
Here we will see how to print triangle patterns using a C++ program. There are 4 patterns discussed here: Right Triangle.Inverted Right Triangle.Equilateral Triangle.Inverted Equilateral Triangle.Inverted Mirrored Right Triangle. Let's start discussing each of these in detail. 1. Right Triangle Belo
6 min read
Count the number of rhombi possible inside a rectangle of given size
Given a rectangle of height H and width W which has the bottom left corner at (0, 0). The task is to count the number of distinct Rhombi that have all points inside or on the border of the rectangle satisfying the following conditions exists: Have non-zero area.Have diagonals parallel to the x and y
5 min read
Sum of Area of all possible square inside a rectangle
Given two integers L and B denoting the length and breadth of a rectangle respectively. The task is to calculate the sum of the area of all possible squares that comes into the rectangle. Examples: Input: L = 4, B = 3 Output: 54 Input: L = 2, B = 5 Output: 26 The idea is to observe the count of numb
6 min read