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: N = 2,
A[][] = {{1, 1, 3, 3}, {2, 1, 4, 2}}
Output: 5
Explaination:
First rectangle covers the following 4 cells:
{(1, 1), (1, 2), (2, 2), (2, 1)}, {(1, 2), (1, 3), (2, 3), (2, 2)}, {(2, 1), (2, 2), (3, 2), (3, 1)}, {(2, 2), (2, 3), (3, 3), (3, 2)}
Second rectangle encloses 2 cells:
{(2, 1), (2, 2), (3, 2), (3, 1)}, {(3, 1), (3, 2), (4, 2), (4, 1)}
Hence both the rectnagles cover 5 cells (since, 1 cell overlaps)Input: N = 3,
A[][] = {{1, 3, 4, 5}, {3, 1, 7, 4}, {5, 3, 8, 6}}
Output: 24
Approach:
Follow the steps below to solve the problem:
- Create a boolean matrix arr[MAX][MAX], where arr[i][j] = 1 if(i, j) is enclosed by any of the given rectangles. Otherwise, arr[i][j] = 0.
- For every rectangle, iterate from from bottom left to top right and for all (i, j) boxes mark them arr[i][j] = 1.
- Finally we will maintain a variable area to store the number of cells enclosed. Increment area if arr[i][j] == 1.
- Print the final value of area.
Below is the implementation of the above approach :
C++
// C++ program to find the // number of cells enclosed // by the given rectangles #include <bits/stdc++.h> using namespace std; #define MAX 1001 bool arr[MAX][MAX]; // Update the coordinates lying // within the rectangle void updateArray(int x1, int y1, int x2, int y2) { for (int i = x1; i < x2; i++) { for (int j = y1; j < y2; j++) { // Update arr[i][j] for // all (i, j) lying within // the rectangle arr[i][j] = true; } } } // Function to return the total // area covered by rectangles int findAreaCovered() { // Stores the number // of cells int area = 0; for (int i = 0; i < MAX; i++) { for (int j = 0; j < MAX; j++) { // arr[i]][[j]==1 means that grid // is filled by some rectangle if (arr[i][j] == true) { area++; } } } return area; } // Driver Code int main() { int N = 3; // (A[i][0], A[i][1]) denotes the // coordinate of the bottom // left of the rectangle // (A[i][2], A[i][3]) denotes the // coordinate of upper right // of the rectangle vector<vector<int> > A = { { 1, 3, 4, 5 }, { 3, 1, 7, 4 }, { 5, 3, 8, 6 } }; // Update the coordinates that // lie within the rectangle for (int i = 0; i < N; i++) { updateArray(A[i][0], A[i][1], A[i][2], A[i][3]); } int area = findAreaCovered(); cout << area; return 0; } |
Java
// Java program to find the number // of cells enclosed by the given // rectangles class GFG{ static final int MAX = 1001; static boolean [][]arr = new boolean[MAX][MAX]; // Update the coordinates lying // within the rectangle static void updateArray(int x1, int y1, int x2, int y2) { for(int i = x1; i < x2; i++) { for(int j = y1; j < y2; j++) { // Update arr[i][j] for // all (i, j) lying within // the rectangle arr[i][j] = true; } } } // Function to return the total // area covered by rectangles static int findAreaCovered() { // Stores the number // of cells int area = 0; for(int i = 0; i < MAX; i++) { for(int j = 0; j < MAX; j++) { // arr[i]][[j]==1 means that grid // is filled by some rectangle if (arr[i][j] == true) { area++; } } } return area; } // Driver Code public static void main(String[] args) { int N = 3; // (A[i][0], A[i][1]) denotes the // coordinate of the bottom // left of the rectangle // (A[i][2], A[i][3]) denotes the // coordinate of upper right // of the rectangle int [][]A = { { 1, 3, 4, 5 }, { 3, 1, 7, 4 }, { 5, 3, 8, 6 } }; // Update the coordinates that // lie within the rectangle for(int i = 0; i < N; i++) { updateArray(A[i][0], A[i][1], A[i][2], A[i][3]); } int area = findAreaCovered(); System.out.print(area); } } // This code is contributed by amal kumar choubey |
C#
// C# program to find the number // of cells enclosed by the given // rectangles using System; class GFG{ static readonly int MAX = 1001; static bool [,]arr = new bool[MAX, MAX]; // Update the coordinates lying // within the rectangle static void updateArray(int x1, int y1, int x2, int y2) { for(int i = x1; i < x2; i++) { for(int j = y1; j < y2; j++) { // Update arr[i,j] for // all (i, j) lying within // the rectangle arr[i, j] = true; } } } // Function to return the total // area covered by rectangles static int findAreaCovered() { // Stores the number // of cells int area = 0; for(int i = 0; i < MAX; i++) { for(int j = 0; j < MAX; j++) { // arr[i],[j]==1 means that grid // is filled by some rectangle if (arr[i, j] == true) { area++; } } } return area; } // Driver Code public static void Main(String[] args) { int N = 3; // (A[i,0], A[i,1]) denotes the // coordinate of the bottom // left of the rectangle // (A[i,2], A[i,3]) denotes the // coordinate of upper right // of the rectangle int [,]A = { { 1, 3, 4, 5 }, { 3, 1, 7, 4 }, { 5, 3, 8, 6 } }; // Update the coordinates that // lie within the rectangle for(int i = 0; i < N; i++) { updateArray(A[i, 0], A[i, 1], A[i, 2], A[i, 3]); } int area = findAreaCovered(); Console.Write(area); } } // This code is contributed by Rajput-Ji |
24
Time Complexity : O (N 3)
Auxiliary Space : O (N 2)
Recommended Posts:
- Total number of cells covered in a matrix after D days
- Minimum Numbers of cells that are connected with the smallest path between 3 given cells
- Count of cells in a matrix which give a Fibonacci number when the count of adjacent cells is added
- Count of cells in a matrix whose adjacent cells's sum is prime Number
- Number of unique rectangles formed using N unit squares
- Total area of two overlapping rectangles
- Find the maximum distance covered using n bikes
- Find the distance covered to collect items at equal distances
- Minimum Manhattan distance covered by visiting every coordinates from a source to a final vertex
- Minimum Distance from a given Cell to all other Cells of a Matrix
- Total ways of choosing X men and Y women from a total of M men and W women
- Number of cells in a matrix that satisfy the given condition
- Number of cells in matrix which are equidistant from given two points
- Find all rectangles filled with 0
- Find the sum of the costs of all possible arrangements of the cells
- Size of all connected non-empty cells of a Matrix
- Color a grid such that all same color cells are connected either horizontally or vertically
- Intersecting rectangle when bottom-left and top-right corners of two rectangles are given
- Smallest square formed with given rectangles
- Maximum given sized rectangles that can be cut out of a sheet of paper
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



