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++
#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();
int flagc = 0;
int flagr = 0;
int n, m;
for (m = i; m < x; m++) {
if (a[m][j] == 1) {
flagr = 1;
break ;
}
if (a[m][j] == 5)
continue ;
for (n = j; n < y; n++) {
if (a[m][n] == 1) {
flagc = 1;
break ;
}
a[m][n] = 5;
}
}
if (flagr == 1)
output[index].push_back(m - 1);
else
output[index].push_back(m);
if (flagc == 1)
output[index].push_back(n - 1);
else
output[index].push_back(n);
}
void get_rectangle_coordinates(vector<vector< int > > a)
{
int size_of_array = a.size();
vector<vector< int > > output;
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) {
output.push_back({ i, j });
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 << "]" ;
}
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;
}
|
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!
Share your thoughts in the comments
Please Login to comment...