📌  相关文章
📜  找到曼哈顿距离至少为 N 的整数点 (x, y)

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

给定一个数字 N,任务是找到整数点 (x, y),使得 0 <= x, y <= N 并且任意两点之间的曼哈顿距离至少为 N。
例子:

Input: N = 3
Output: (0, 0) (0, 3) (3, 0) (3, 3)

Input: N = 4
Output: (0, 0) (0, 4) (4, 0) (4, 4) (2, 2)

方法:

  • 两点 (x 1 , y 1 ) 和 (x 2 , y 2 ) 之间的曼哈顿距离为:
    |x 1 – x 2 | + |y 1 – y 2 |
  • 对于所有点对,此距离至少为 N。
  • 由于0 <= x <= N0 <= y <= N,所以我们可以想象一个边长为 N 的正方形,其左下角为 (0, 0),右上角为 (N, N)。
  • 因此,如果我们在这个角落放置 4 个点,那么曼哈顿距离将至少为 N。
  • 现在我们必须最大化点的数量,我们必须检查正方形内是否有任何可用的点。
  • 如果 N 是偶数,则 (N/2, N/2) 方格的中点是整数点,否则,当 N 是奇数时,N/2 不是整数,因此它将是浮点值。
  • 所以唯一可用的位置是中间点,只有当 N 是偶数时我们才能在那里放置一个点。
  • 因此,如果 N 为奇数,则点数将为 4,如果 N 为偶数,则点数将为 5。

下面是上述方法的实现:

C++
// C++ code to Find the integer points (x, y)
// with Manhattan distance atleast N
 
#include 
using namespace std;
 
// C++ function to find all possible point
vector > FindPoints(int n)
{
 
    vector > v;
 
    // Find all 4 corners of the square
    // whose side length is n
    v.push_back({ 0, 0 });
    v.push_back({ 0, n });
    v.push_back({ n, 0 });
    v.push_back({ n, n });
 
    // If n is even then the middle point
    // of the square will be an integer,
    // so we will take that point
    if (n % 2 == 0)
        v.push_back({ n / 2, n / 2 });
 
    return v;
}
 
// Driver Code
int main()
{
 
    int N = 8;
 
    vector > v
        = FindPoints(N);
 
    // Printing all possible points
    for (auto i : v) {
        cout << "(" << i.first << ", "
             << i.second << ") ";
    }
    return 0;
}


Java
// Java code to Find the integer points (x, y)
// with Manhattan distance atleast N
import java.util.*;
 
class GFG
{
 
static class pair
{
    int first, second;
    public pair(int first, int second)
    {
        this.first = first;
        this.second = second;
    }
}
 
// Java function to find all possible point
static Vector FindPoints(int n)
{
    Vector v = new Vector();
 
    // Find all 4 corners of the square
    // whose side length is n
    v.add(new pair( 0, 0 ));
    v.add(new pair( 0, n ));
    v.add(new pair( n, 0 ));
    v.add(new pair( n, n ));
 
    // If n is even then the middle point
    // of the square will be an integer,
    // so we will take that point
    if (n % 2 == 0)
        v.add(new pair( n / 2, n / 2 ));
 
    return v;
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 8;
 
    Vector v = FindPoints(N);
 
    // Printing all possible points
    for (pair i : v)
    {
        System.out.print("(" + i.first + ", " +
                               i.second + ") ");
    }
}
}
 
// This code is contributed by PrinciRaj1992


Python3
# Python3 code to Find the integer points (x, y)
# with Manhattan distance atleast N
 
# function to find all possible point
def FindPoints(n) :
 
    v = [];
 
    # Find all 4 corners of the square
    # whose side length is n
    v.append([ 0, 0 ]);
    v.append([ 0, n ]);
    v.append([ n, 0 ]);
    v.append([ n, n ]);
 
    # If n is even then the middle point
    # of the square will be an integer,
    # so we will take that point
    if (n % 2 == 0) :
        v.append([ n // 2, n // 2 ]);
 
    return v;
 
# Driver Code
if __name__ == "__main__" :
 
    N = 8;
 
    v = FindPoints(N);
 
    # Printing all possible points
    for element in v :
        print("(", element[0],
              ",", element[1], ")", end = " ");
 
# This code is contributed by AnkitRai01


C#
// C# code to Find the integer points (x, y)
// with Manhattan distance atleast N
using System;
using System.Collections.Generic;
 
class GFG
{
 
class pair
{
    public int first, second;
    public pair(int first, int second)
    {
        this.first = first;
        this.second = second;
    }
}
 
// Function to find all possible point
static List FindPoints(int n)
{
    List v = new List();
 
    // Find all 4 corners of the square
    // whose side length is n
    v.Add(new pair( 0, 0 ));
    v.Add(new pair( 0, n ));
    v.Add(new pair( n, 0 ));
    v.Add(new pair( n, n ));
 
    // If n is even then the middle point
    // of the square will be an integer,
    // so we will take that point
    if (n % 2 == 0)
        v.Add(new pair( n / 2, n / 2 ));
 
    return v;
}
 
// Driver Code
public static void Main(String[] args)
{
    int N = 8;
 
    List v = FindPoints(N);
 
    // Printing all possible points
    foreach (pair i in v)
    {
        Console.Write("(" + i.first + ", " +
                            i.second + ") ");
    }
}
}
 
// This code is contributed by Rajput-Ji


Javascript


输出:
(0, 0) (0, 8) (8, 0) (8, 8) (4, 4)

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