Dada una array 2D arr[][] que consta de N filas de la forma {X i , Y i , R i } tal que (X i , Y i ) representa la posición de una torre y R i representa el rango de red de esa torre. Dados dos enteros X e Y , la tarea es verificar si el punto (X, Y) se encuentra en el rango de la red de las torres o no.
Ejemplos:
Entrada: arr[][] = { {1, 1, 3}, {10, 10, 5}, {15, 15, 15} }, X = 5, Y = 5
Salida: Verdadero
Explicación:
Distancia del punto ( 5, 5) de (arr[0][0], arr[0][1]) = 5,65685 y el rango de la primera torre es 3. Por lo tanto, el punto (X, Y) no se encuentra en el rango de la red de la primera torre.
Distancia del punto (5, 5) desde (arr[1][0], arr[1][1]) = 7.07107 y el rango de la segunda torre es 5. Por lo tanto, el punto (X, Y) no se encuentra en el rango de red de la segunda torre.
Distancia del punto (5, 5) desde (arr[2][0], arr[2][1]) = 14,1421 y el rango de la tercera torre es 15. Por lo tanto, el punto (X, Y) se encuentra en la red -alcance de la tercera torre.
Dado que el punto (X, Y) se encuentra en el rango de la tercera torre. Por lo tanto, la salida requerida es True.Entrada: arr[][] = { {1, 1, 3}, {10, 10, 3}, {15, 15, 3} }, X = 5, Y = 5
Salida: Falso
Enfoque: siga los pasos que se indican a continuación para resolver el problema:
- Recorra la array y para cada fila (torre) recorrida, compruebe si el valor de sqrt((arr[i][0] – x) 2 + (arr[i][1] – Y) 2 ) es mayor que arr[ i][2] o no. Si se encuentra que es verdadero, imprima Verdadero .
- De lo contrario, imprime Falso .
C++
// C++ program to implement // the above approach #include <bits/stdc++.h> 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
<script> // JavaScript program to implement // the above approach // Function to check if the point (X, Y) // exists in the towers network-range or not function checkPointRange(arr, X, Y, N) { // Traverse the array arr[] for(let i = 0; i < N; i++) { // Stores distance of the // point (X, Y) from i-th tower let 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 let arr = [[ 1, 1, 3 ], [ 10, 10, 3 ], [ 15, 15, 15 ]]; let X = 5, Y = 5; let N = arr.length; // If point (X, Y) lies in the // range of any of the towers if (checkPointRange(arr, X, Y, N)) { document.write("True"); } // Otherwise else { document.write("False"); } </script>
True
Complejidad temporal: O(N)
Espacio auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por grand_master y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA