给定数字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 <= N并且0 <= 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
输出:
(0, 0) (0, 8) (8, 0) (8, 8) (4, 4)