📌  相关文章
📜  检查在距N个给定点最多曼哈顿距离为K的平面中是否存在任何点

📅  最后修改于: 2021-04-17 17:49:30             🧑  作者: Mango

给定两个数组A []B [],它们由一个平面中N个不同点的XY坐标以及一个正整数K组成,任务是检查平面中是否存在任何点P ,以使曼哈顿之间的距离为该点和所有给定的点最多为K。如果存在任何这样的点P ,则打印“是” 。否则,打印“否”

例子:

方法:可以通过找到每N个给定点对之间的曼哈顿距离来解决给定问题。检查所有成对的点之后,如果成对的点之间的距离计数最多为K,则打印“是” 。否则,打印“否”

下面是上述方法的实现:

C++
// C++ program for the above approach
#include
using namespace std;
 
// Function to check if there
// exists any point with at most
// K distance from N given points
string find(int a[], int b[], int N, int K)
{
     
    // Traverse the given N points
    for(int i = 0; i < N; i++)
    {
         
        // Stores the count of pairs
        // of coordinates having
        // Manhattan distance <= K
        int count = 0;
 
        for(int j = 0; j < N; j++)
        {
             
            // For the same coordinate
            if (i == j)
            {
                continue;
            }
 
            // Calculate Manhattan distance
            long long int dis = abs(a[i] - a[j]) +
                                abs(b[i] - b[j]);
 
            // If Manhattan distance <= K
            if (dis <= K)
            {
                count++;
            }
 
            // If all coordinates
            // can meet
            if (count == N - 1)
            {
                return "Yes";
            }
        }
    }
 
    // If all coordinates can't meet
    return "No";
}
 
// Driver Code
int main()
{
    int N = 5;
    int A[] = { 1, 0, 2, 1, 1 };
    int B[] = { 1, 1, 1, 0, 2 };
    int K = 1;
 
    cout << find(A, B, N, K) << endl;
}
 
// This code is contributed by bgangwar59


Java
// Java program for the above approach
 
import java.io.*;
 
class GFG {
 
    // Function to check if there
    // exists any point with at most
    // K distance from N given points
    public static String find(
        int[] a, int[] b, int N, int K)
    {
        // Traverse the given N points
        for (int i = 0; i < N; i++) {
 
            // Stores the count of pairs
            // of coordinates having
            // Manhattan distance <= K
            int count = 0;
 
            for (int j = 0; j < N; j++) {
 
                // For the same coordinate
                if (i == j) {
                    continue;
                }
 
                // Calculate Manhattan distance
                long dis = Math.abs(a[i] - a[j])
                           + Math.abs(b[i] - b[j]);
 
                // If Manhattan distance <= K
                if (dis <= K) {
 
                    count++;
                }
 
                // If all coordinates
                // can meet
                if (count == N - 1) {
                    return "Yes";
                }
            }
        }
 
        // If all coordinates can't meet
        return "No";
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int N = 5;
        int[] A = { 1, 0, 2, 1, 1 };
        int[] B = { 1, 1, 1, 0, 2 };
        int K = 1;
 
        System.out.println(
            find(A, B, N, K));
    }
}


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to check if there
// exists any point with at most
// K distance from N given points
public static String find(int[] a, int[] b,
                          int N, int K)
{
     
    // Traverse the given N points
    for(int i = 0; i < N; i++)
    {
         
        // Stores the count of pairs
        // of coordinates having
        // Manhattan distance <= K
        int count = 0;
 
        for(int j = 0; j < N; j++)
        {
             
            // For the same coordinate
            if (i == j)
            {
                continue;
            }
 
            // Calculate Manhattan distance
            long dis = Math.Abs(a[i] - a[j]) +
                       Math.Abs(b[i] - b[j]);
 
            // If Manhattan distance <= K
            if (dis <= K)
            {
                count++;
            }
 
            // If all coordinates
            // can meet
            if (count == N - 1)
            {
                return "Yes";
            }
        }
    }
 
    // If all coordinates can't meet
    return "No";
}
 
// Driver Code
public static void Main(string[] args)
{
    int N = 5;
    int[] A = { 1, 0, 2, 1, 1 };
    int[] B = { 1, 1, 1, 0, 2 };
    int K = 1;
 
    Console.WriteLine(find(A, B, N, K));
}
}
 
// This code is contributed by ukasp


输出:
Yes

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