📌  相关文章
📜  通过从给定的一组点平行于 X 和 Y 轴绘制的线计算在给定矩形中生成的矩形

📅  最后修改于: 2021-09-07 02:13:57             🧑  作者: Mango

给定一个二维数组 rectangle[][] 表示维度为L * B的矩形 {(0, 0), (L, 0), (0, B), (L, B)} 和另一个二维数组的顶点, 笛卡尔坐标系中大小为N 的点[][] 。从给定阵列的每个点绘制一条平行于X 轴的水平线和一条平行于Y 轴的垂直线。任务是计算给定矩形中存在的所有可能的矩形。

例子

方法:想法是遍历数组并计算通过给定点 [][] 数组的所有不同的水平线和垂直线。最后,返回(不同水平线的计数 – 1) * (垂直线的计数 – 1)的乘法值。请按照以下步骤解决问题:

  • 创建两个集合,比如cntHorcntVer来存储所有通过points[][]数组的不同水平线和垂直线。
  • 遍历 points[][] 数组。
  • 使用cntHor 中的元素计数计算所有不同的水平线。
  • 使用cntVer 中的元素计数计算所有不同的垂直线。
  • 最后,打印(count of elements in cntHor – 1) * (count of elements in cntVer – 1) 的值

下面是上述方法的实现:

C++
_ _ _ _ _ 
5|_|_ _|_ _|
4| |   |3,4|
3|_|_ _|_ _| 
2| |1,2|   |
1|_|_ _|_ _|
 0 1 2 3 4 5


Java
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to get the count
// of ractangles
int cntRect(int points[][2], int N,
             int rectangle[][2])
{
    // Store distinct
    // horizontal lines
    unordered_set cntHor;  
     
    // Store distinct
    // Vertical lines
    unordered_set cntVer;  
     
    // Insert horizontal line
    // passing through 0
    cntHor.insert(0);
     
    // Insert vertical line
    // passing through 0.
    cntVer.insert(0);
     
    // Insert horizontal line
    // passing through rectangle[3][0]
    cntHor.insert(rectangle[3][0]);
     
    // Insert vertical line
    // passing through rectangle[3][1]
    cntVer.insert(rectangle[3][1]);
     
    // Insert all horizontal and
    // vertical lines passing through
    // the given array
    for (int i = 0; i < N; i++) {
         
        // Insert all horizontal lines
        cntHor.insert(points[i][0]);
         
        // Insert all vertical lines
        cntVer.insert(points[i][1]);
    }
     
    return (cntHor.size() - 1) *
              (cntVer.size() - 1);
}
 
// Driver Code
int main()
{
    int rectangle[][2] = {{0, 0}, {0, 5},
                          {5, 0}, {5, 5}};
    int points[][2] = {{1, 2}, {3, 4}};
     
    int N = sizeof(points) / sizeof(points[0]);
    cout<


Python3
// Java program to implement
// the above approach
import java.io.*;
import java.util.*;
 
class GFG{
     
// Function to get the count
// of ractangles
public static int cntRect(int points[][], int N,
                          int rectangle[][])
{
     
    // Store distinct
    // horizontal lines
    HashSet cntHor = new HashSet<>();
 
    // Store distinct
    // Vertical lines
    HashSet cntVer = new HashSet<>();
 
    // Insert horizontal line
    // passing through 0
    cntHor.add(0);
 
    // Insert vertical line
    // passing through 0.
    cntVer.add(0);
 
    // Insert horizontal line
    // passing through rectangle[3][0]
    cntHor.add(rectangle[3][0]);
 
    // Insert vertical line
    // passing through rectangle[3][1]
    cntVer.add(rectangle[3][1]);
 
    // Insert all horizontal and
    // vertical lines passing through
    // the given array
    for(int i = 0; i < N; i++)
    {
         
        // Insert all horizontal lines
        cntHor.add(points[i][0]);
 
        // Insert all vertical lines
        cntVer.add(points[i][1]);
    }
    return (cntHor.size() - 1) *
           (cntVer.size() - 1);
}
 
// Driver Code
public static void main(String args[])
{
    int rectangle[][] = { { 0, 0 }, { 0, 5 },
                          { 5, 0 }, { 5, 5 } };
    int points[][] = { { 1, 2 }, { 3, 4 } };
 
    int N = points.length;
     
    System.out.println(cntRect(points, N,
                               rectangle));
}
}
 
// This code is contributed by hemanth gadarla


C#
# Python3 program to implement
# the above approach
 
# Function to get the count
# of ractangles
def cntRect(points, N,
            rectangle):
 
    # Store distinct
    # horizontal lines
    cntHor = set([]) 
     
    # Store distinct
    # Vertical lines
    cntVer = set([])
     
    # Insert horizontal line
    # passing through 0
    cntHor.add(0)
     
    # Insert vertical line
    # passing through 0.
    cntVer.add(0)
     
    # Insert horizontal line
    # passing through rectangle[3][0]
    cntHor.add(rectangle[3][0])
     
    # Insert vertical line
    # passing through rectangle[3][1]
    cntVer.add(rectangle[3][1])
     
    # Insert all horizontal and
    # vertical lines passing through
    # the given array
    for i in range (N):
         
        # Insert all horizontal lines
        cntHor.add(points[i][0])
         
        # Insert all vertical lines
        cntVer.add(points[i][1])
    
    return ((len(cntHor) - 1) *
            (len(cntVer) - 1))
 
# Driver Code
if __name__ == "__main__":
 
    rectangle = [[0, 0], [0, 5],
                 [5, 0], [5, 5]]
    points = [[1, 2], [3, 4]]   
    N = len(points)
    print (cntRect(points, N, rectangle))
 
# This code is contributed by Chitranayal


Javascript
// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
class GFG{
     
// Function to get the count
// of ractangles
public static int cntRect(int [,]points,
                          int N, int [,]rectangle)
{
  // Store distinct
  // horizontal lines
  HashSet cntHor = new HashSet();
 
  // Store distinct
  // Vertical lines
  HashSet cntVer = new HashSet();
 
  // Insert horizontal line
  // passing through 0
  cntHor.Add(0);
 
  // Insert vertical line
  // passing through 0.
  cntVer.Add(0);
 
  // Insert horizontal line
  // passing through rectangle[3,0]
  cntHor.Add(rectangle[3, 0]);
 
  // Insert vertical line
  // passing through rectangle[3,1]
  cntVer.Add(rectangle[3, 1]);
 
  // Insert all horizontal and
  // vertical lines passing through
  // the given array
  for(int i = 0; i < N; i++)
  {
    // Insert all horizontal lines
    cntHor.Add(points[i, 0]);
 
    // Insert all vertical lines
    cntVer.Add(points[i, 1]);
  }
  return (cntHor.Count - 1) *
         (cntVer.Count - 1);
}
 
// Driver Code
public static void Main(String []args)
{
  int [,]rectangle = {{0, 0}, {0, 5},
                      {5, 0}, {5, 5}};
  int [,]points = {{1, 2}, {3, 4}};
  int N = points.GetLength(0);
  Console.WriteLine(cntRect(points, N,
                            rectangle));
}
}
 
// This code is contributed by 29AjayKumar


输出:

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live