给定一个由{X i , Y i , R i }形式的N行组成的二维数组arr[][]使得(X i , Y i )表示塔的位置, R i表示塔的网络范围塔。给定两个整数X和Y ,任务是检查点(X, Y) 是否在塔的网络范围内。
例子:
Input: arr[][] = { {1, 1, 3}, {10, 10, 5}, {15, 15, 15} }, X = 5, Y = 5
Output: True
Explanation:
Distance of point(5, 5) from (arr[0][0], arr[0][1]) = 5.65685 and the range of first tower is 3. Therefore, the point(X, Y) does not lie in the network-range of the first tower.
Distance of point(5, 5) from (arr[1][0], arr[1][1]) = 7.07107 and the range of second tower is 5. Therefore, the point(X, Y) does not lie in the network-range of the second tower.
Distance of point(5, 5) from (arr[2][0], arr[2][1]) = 14.1421 and the range of third tower is 15. Therefore, the point(X, Y) lies in the network-range of the third tower.
Since, point (X, Y) lies in the range of the third tower. Therefore, the required output is True.
Input: arr[][] = { {1, 1, 3}, {10, 10, 3}, {15, 15, 3} }, X = 5, Y = 5
Output: False
方法:按照下面给出的步骤解决问题:
- 遍历数组,对于遍历的每一行(塔),检查sqrt((arr[i][0] – x) 2 + (arr[i][1] – Y) 2 ) 的值是否大于arr[ i][2]与否。如果发现为真,则打印True 。
- 否则,打印False 。
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to check if the point (X, Y)
// exists in the towers network-range or not
bool checkPointRange(int arr[][3], int X,
int Y, int N)
{
// Traverse the array arr[]
for (int i = 0; i < N; i++) {
// Stores distance of the
// point (X, Y) from i-th tower
double dist
= sqrt((arr[i][0] - X) * (arr[i][0] - X)
+ (arr[i][1] - Y) * (arr[i][1] - Y));
// If dist lies within the
// range of the i-th tower
if (dist <= arr[i][2]) {
return true;
}
}
// If the point (X, Y) does not lie
// in the range of any of the towers
return false;
}
// Driver Code
int main()
{
int arr[][3] = { { 1, 1, 3 },
{ 10, 10, 3 },
{ 15, 15, 15 } };
int X = 5, Y = 5;
int N = sizeof(arr) / sizeof(arr[0]);
// If point (X, Y) lies in the
// range of any of the towers
if (checkPointRange(arr, X, Y, N)) {
cout << "True";
}
// Otherwise
else {
cout << "False";
}
}
Java
// Java program to implement
// the above approach
import java.util.*;
class GFG{
// Function to check if the point (X, Y)
// exists in the towers network-range or not
static boolean checkPointRange(int arr[][], int X,
int Y, int N)
{
// Traverse the array arr[]
for(int i = 0; i < N; i++)
{
// Stores distance of the
// point (X, Y) from i-th tower
double dist = Math.sqrt((arr[i][0] - X) *
(arr[i][0] - X) +
(arr[i][1] - Y) *
(arr[i][1] - Y));
// If dist lies within the
// range of the i-th tower
if (dist <= arr[i][2])
{
return true;
}
}
// If the point (X, Y) does not lie
// in the range of any of the towers
return false;
}
// Driver Code
public static void main(String[] args)
{
int arr[][] = { { 1, 1, 3 },
{ 10, 10, 3 },
{ 15, 15, 15 } };
int X = 5, Y = 5;
int N = arr.length;
// If point (X, Y) lies in the
// range of any of the towers
if (checkPointRange(arr, X, Y, N))
{
System.out.print("True");
}
// Otherwise
else
{
System.out.print("False");
}
}
}
// This code is contributed by code_hunt
Python3
# Python3 program to implement
# the above approach
from math import sqrt
# Function to check if the point (X, Y)
# exists in the towers network-range or not
def checkPointRange(arr, X, Y, N):
# Traverse the array arr[]
for i in range(N):
# Stores distance of the
# point (X, Y) from i-th tower
dist = sqrt((arr[i][0] - X) *
(arr[i][0] - X) +
(arr[i][1] - Y) *
(arr[i][1] - Y))
# If dist lies within the
# range of the i-th tower
if (dist <= arr[i][2]):
return True
# If the point (X, Y) does not lie
# in the range of any of the towers
return False
# Driver Code
if __name__ == '__main__':
arr = [ [ 1, 1, 3 ],
[ 10, 10, 3 ],
[ 15, 15, 15 ] ]
X = 5
Y = 5
N = len(arr)
# If point (X, Y) lies in the
# range of any of the towers
if (checkPointRange(arr, X, Y, N)):
print("True")
# Otherwise
else:
print("False")
# This code is contributed by bgangwar59
C#
// C# program to implement
// the above approach
using System;
class GFG{
// Function to check if the point (X, Y)
// exists in the towers network-range or not
static bool checkPointRange(int[,] arr, int X,
int Y, int N)
{
// Traverse the array arr[]
for(int i = 0; i < N; i++)
{
// Stores distance of the
// point (X, Y) from i-th tower
double dist = Math.Sqrt((arr[i, 0] - X) *
(arr[i, 0] - X) +
(arr[i, 1] - Y) *
(arr[i, 1] - Y));
// If dist lies within the
// range of the i-th tower
if (dist <= arr[i, 2])
{
return true;
}
}
// If the point (X, Y) does not lie
// in the range of any of the towers
return false;
}
// Driver Code
public static void Main()
{
int[,] arr = { { 1, 1, 3 },
{ 10, 10, 3 },
{ 15, 15, 15 } };
int X = 5, Y = 5;
int N = arr.Length;
// If point (X, Y) lies in the
// range of any of the towers
if (checkPointRange(arr, X, Y, N))
{
Console.Write("True");
}
// Otherwise
else
{
Console.Write("False");
}
}
}
// This code is contributed by susmitakundugoaldanga
Javascript
True
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live