找到任意一对之间曼哈顿距离相等的 4 个点
给定一个整数N ,在具有整数坐标的 2D 平面中找到 4 个点,使得任意一对点之间的曼哈顿距离等于N 。
例子:
Input: N = 6
Output: { {0, -3}, {3, 0}, {-3, 0}, {0, 3} }
Explanation: It can be easily calculated that Manhattan distance between all possible pairs is 6.
Input: N = 11
Output: -1
Explanation: It is not possible to locate 4 points such that Manhattan distance between all pairs is 11
方法:解决问题的想法基于以下观察:
Square having all 4 vertices on all 4 co-ordinate axes (1 vertex on each axis) equidistant from origin have the same distance between any pair of vertices which is the double of the distance between any vertex and origin.
If N is odd, it can not be divided into 2 equal integral parts. So, a square can not be formed such that opposite vertices are at equal distance from the origin (i.e. N/2), with distance between them being N, for such a case 4 points satisfying given condition cannot be found.
请按照以下步骤解决问题:
- 检查N是奇数还是偶数。
- 如果N是奇数,则找不到这样的 4 个点。
- 否则将四个点设置为{N/2, 0}, {-N/2, 0}, {0, N/2}, {0, -N/2}
下面是上述方法的实现。
C++
// C++ code for above approach
#include
using namespace std;
// Function to find 4 points such that
// manhattan distance between
// any two of them is equal
vector > findPoints(int N)
{
// Initializing vector of pairs to
// store the 4 pairs
vector > points;
// If N is odd, it is impossible
// to find 4 such points
if (N % 2 == 1)
return points;
// Initializing a variable
// with value equal to N/2
int point = N / 2;
// Pushing all the 4 pairs into
// vector "points" such that distance
// between any two is equal
points.push_back({ 0, point });
points.push_back({ 0, -point });
points.push_back({ point, 0 });
points.push_back({ -point, 0 });
// Returning "points" vector
return points;
}
// Function to print
void print(int N)
{
vector > ans
= findPoints(N);
if (ans.size() == 0)
cout << -1;
for (int i = 0; i < ans.size(); i++) {
cout << ans[i].first << ", "
<< ans[i].second << "\n";
}
}
// Driver Code
int main()
{
int N = 6;
// Calling the print function
print(N);
return 0;
}
Java
// Java code for above approach
import java.io.*;
class GFG {
// Function to find 4 points such that
// manhattan distance between
// any two of them is equal
static int[][] findPoints(int N)
{
// Initializing vector of pairs to
// store the 4 pairs
int[][]points=new int[4][2];
// If N is odd, it is impossible
// to find 4 such points
if (N % 2 == 1)
return points;
// Initializing a variable
// with value equal to N/2
int point = N / 2;
// Pushing all the 4 pairs into
// vector "points" such that distance
// between any two is equal
points[0][0] = 0;
points[0][ 1] = point;
points[1][0] = 0;
points[1][1] = -point;
points[2][0] = point;
points[2][1] = 0;
points[3][0] = -point;
points[3][1] = 0;
// Returning "points" vector
return points;
}
// Function to print
static void print(int N)
{
int[][] ans = findPoints(N);
if (ans.length == 0)
System.out.print(-1);
for (int i = 0; i < ans.length; i++) {
System.out.println(ans[i][0] + ", " + ans[i][1]);
}
}
// Driver Code
public static void main (String[] args) {
int N = 6;
// Calling the print function
print(N);
}
}
// This code is contributed by hrithikgarg03188.
Python3
# Python code for the above approach
# Function to find 4 points such that
# manhattan distance between
# any two of them is equal
def findPoints(N):
# Initializing vector of pairs to
# store the 4 pairs
points = []
# If N is odd, it is impossible
# to find 4 such points
if (N % 2 == 1):
return points
# Initializing a variable
# with value equal to N/2
point = (N // 2)
# Pushing all the 4 pairs into
# vector "points" such that distance
# between any two is equal
points.append([0,point ])
points.append([0,-1 * point ])
points.append([point,0 ])
points.append([-1 * point,0 ])
# Returning "points" vector
return points
# Function to print
def Print(N):
ans = findPoints(N)
if (len(ans) == 0):
print(-1)
for i in range(len(ans)):
print(str(ans[i][0]) + ", " + str(ans[i][1]))
# Driver Code
N = 6
# Calling the print function
Print(N)
# This code is contributed by shinjanpatra
C#
// C# code for above approach
using System;
class GFG {
// Function to find 4 points such that
// manhattan distance between
// any two of them is equal
static int[, ] findPoints(int N)
{
// Initializing vector of pairs to
// store the 4 pairs
int[, ] points = new int[4, 2];
// If N is odd, it is impossible
// to find 4 such points
if (N % 2 == 1)
return points;
// Initializing a variable
// with value equal to N/2
int point = N / 2;
// Pushing all the 4 pairs into
// vector "points" such that distance
// between any two is equal
points[0, 0] = 0;
points[0, 1] = point;
points[1, 0] = 0;
points[1, 1] = -point;
points[2, 0] = point;
points[2, 1] = 0;
points[3, 0] = -point;
points[3, 1] = 0;
// Returning "points" vector
return points;
}
// Function to print
static void print(int N)
{
int[, ] ans = findPoints(N);
if (ans.GetLength(0) == 0)
Console.Write(-1);
for (int i = 0; i < ans.GetLength(0); i++) {
Console.WriteLine(ans[i, 0] + ", " + ans[i, 1]);
}
}
// Driver Code
public static void Main()
{
int N = 6;
// Calling the print function
print(N);
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
0, 3
0, -3
3, 0
-3, 0
时间复杂度: O(1)
辅助空间: O(1)