📜  生成位于矩形内的所有积分点

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

给定一个长度为L 、宽度为W的矩形,任务是生成位于矩形 pf 尺寸L * W内的所有积分坐标 (X, Y),其顶点之一位于原点(0, 0)

例子:

方法:可以通过使用 rand()函数为X坐标生成从0L范围内的所有整数以及对于Y坐标生成从0W范围内的所有整数来解决该问题。请按照以下步骤解决问题:

  1. 创建一组对来存储位于矩形内的所有坐标(X,Y)。
  2. 使用等式rand() % L生成位于0L之间的所有整数和rand() % W生成位于0W之间的所有整数。
  3. 打印矩形内所有可能的L × W坐标 (X, Y)。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Function to generate coordinates
// lying within the rectangle
void generatePoints(int L, int W)
{
    // Store all possible coordinates
    // that lie within the rectangle
    set > hash;
 
    // Stores the number of possible
    // coordinates that lie within
    // the rectangle
    int total = (L * W);
 
    // Use current time as seed
    // for random generator
    srand(time(0));
 
    // Generate all possible
    // coordinates
    while (total--) {
        // Generate all possible
        // X-coordinates
        int X = rand() % L;
 
        // Generate all possible
        // Y-coordinates
        int Y = rand() % W;
 
        // If coordinates(X, Y) has
        // not been generated already
        while (hash.count({ X, Y })) {
            X = rand() % L;
            Y = rand() % W;
        }
 
        // Insert the coordinates(X, Y)
        hash.insert({ X, Y });
    }
 
    // Print the coordinates
    for (auto points : hash) {
        cout << "(" << points.first << ", "
             << points.second << ") ";
    }
}
 
// Driver Code
int main()
{
    // Rectangle dimensions
    int L = 3, W = 2;
 
    generatePoints(L, W);
}


Python3
# Python3 program to implement
# the above approach
import time
import random
 
random.seed(time.time())
 
# Function to generate coordinates
# lying within the rectangle
def generatePoints(L, W):
     
    # Store all possible coordinates
    # that lie within the rectangle
    hash = {}
 
    # Stores the number of possible
    # coordinates that lie within
    # the rectangle
    total = (L * W)
 
    # Generate all possible
    # coordinates
    for i in range(total):
         
        # Generate all possible
        # X-coordinates
        X = random.randint(0, L) % L
 
        # Generate all possible
        # Y-coordinates
        Y = random.randint(0, W) % W
 
        # If coordinates(X, Y) has
        # not been generated already
        while ((X, Y) in hash):
            X = random.randint(0, L) % L
            Y = random.randint(0, W) % W
 
        # Insert the coordinates(X, Y)
        hash[(X, Y)] = 1
 
    # Print the coordinates
    for points in sorted(hash):
        print("(", points[0],
             ", ", points[1],
             ") ", end = "")
 
# Driver code
if __name__ == '__main__':
     
    # Rectangle dimensions
    L, W = 3, 2
 
    generatePoints(L, W)
 
# This code is contributed by mohit kumar 29


C#
// C# program to implement
// the above approach
using System;
using System.Collections;
using System.Collections.Generic;
 
class GFG{
 
public class store : IComparer>
{
    public int Compare(KeyValuePair x,
                       KeyValuePair y)
    {
        if (x.Key != y.Key)
        {
            return x.Key.CompareTo(y.Key);   
        }
        else
        {
            return x.Value.CompareTo(y.Value);   
        }
    }
}
      
// Function to generate coordinates
// lying within the rectangle
static void generatePoints(int L, int W)
{
     
    // Store all possible coordinates
    // that lie within the rectangle
    SortedSet> hash = new SortedSet>(new store());
  
    // Stores the number of possible
    // coordinates that lie within
    // the rectangle
    int total = (L * W);
  
    // For random generator
    Random rand = new Random();
  
    // Generate all possible
    // coordinates
    while ((total--) != 0)
    {
         
        // Generate all possible
        // X-coordinates
        int X = rand.Next() % L;
  
        // Generate all possible
        // Y-coordinates
        int Y = rand.Next() % W;
  
        // If coordinates(X, Y) has
        // not been generated already
        while (hash.Contains(
            new KeyValuePair(X, Y)))
        {
            X = rand.Next() % L;
            Y = rand.Next() % W;
        }
  
        // Insert the coordinates(X, Y)
        hash.Add(new KeyValuePair(X, Y));
    }
  
    // Print the coordinates
    foreach(KeyValuePair x in hash)
    {
        Console.Write("(" + x.Key + ", " +
                          x.Value + ") ");
    }
}
 
// Driver Code
public static void Main(string[] args)
{
     
    // Rectangle dimensions
    int L = 3, W = 2;
  
    generatePoints(L, W);
}
}
 
// This code is contributed by rutvik_56


输出:
(0, 0) (0, 1) (1, 0) (1, 1) (2, 0) (2, 1)




时间复杂度: O(L * W)
辅助空间: O(L * W)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程