📜  所有给定矩形覆盖的单元格总数

📅  最后修改于: 2021-10-23 08:15:07             🧑  作者: Mango

给定一个矩阵A[][]N 个矩形的坐标组成,使得{A[i][0], A[i][1]}表示矩形的左下角坐标和{A[i][2], A [i][3]}表示矩形的右上角坐标,任务是找到所有矩形覆盖的单元格总数。

例子:

方法:
请按照以下步骤解决问题:

  1. 创建一个布尔矩阵arr[MAX][MAX] ,其中 arr[i][j] = 1 如果(i, j) 被任何给定的矩形包围。否则,arr[i][j] = 0。
  2. 对于每个矩形,从左下角右上角迭代,对于所有 (i, j) 框,将它们标记为 arr[i][j] = 1。
  3. 最后,我们将维护一个变量area来存储封闭的单元格数量。如果arr[i][j] == 1 ,则增加区域
  4. 打印area的最终值。

下面是上述方法的实现:

C++
// C++ program to find the
// number of cells enclosed
// by the given rectangles
 
#include 
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 > 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


Python3
# Python3 program to find the
# number of cells enclosed
# by the given rectangles
MAX = 1001
arr = [[False for i in range(MAX)]
              for j in range(MAX)]
             
# Update the coordinates lying
# within the rectangle
def updateArray(x1, y1, x2, y2):
     
    for i in range(x1, x2):
        for j in range(y1, y2):
             
            # 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
def findAreaCovered():
 
    # Stores the number
    # of cells
    area = 0
     
    for i in range(MAX):
        for j in range(MAX):
             
            # arr[i]][[j]==1 means that grid
            # is filled by some rectangle
            if arr[i][j]:
                area += 1
  
    return area
     
# Driver code
if __name__=="__main__":
     
    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
    A = [ [ 1, 3, 4, 5 ],
          [ 3, 1, 7, 4 ],
          [ 5, 3, 8, 6 ] ];
         
    # Update the coordinates that
    # lie within the rectangle
    for i in range(N):
        updateArray(A[i][0], A[i][1],
                    A[i][2], A[i][3]);
 
    area = findAreaCovered();
    print(area)
 
# This code is contributed by rutvik_56


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


Javascript


输出:
24

时间复杂度: O (N 3 )
辅助空间: O (N 2 )