Dadas las coordenadas de 5 puntos bidimensionales, verifica si forman una forma de T cerrada. Escriba ‘Sí’ si tienen forma de T y ‘No’ en caso contrario. Nota: Las coordenadas deben ser distintas y enteras.
Hay 4 tipos de formaciones en forma de T posibles según las condiciones dadas:
Ejemplos:
Input: [[7, 5], [8, 5], [6, 5], [7, 7], [7, 6]] Output: Yes Input: [[0, 0], [1, 0], [2, 0], [1, -1], [1, -2]] Output: Yes
Acercarse:
- Considere el primer punto de la lista dada como el centro (x, y) (es decir, la intersección de las dos líneas que forman una T).
- Luego verifique si todos los puntos que se necesitan para formar una forma de T de la cual (x, y) son el centro están presentes en la lista de puntos dados o no.
- Verifique esto para los 4 patrones posibles de forma de T.
A continuación se muestra la implementación del enfoque anterior:
Python3
# Python3 code to check if given 5 # 2-D points form T-shape or not # Import the function to initialize the # dictionary with a specific value from collections import defaultdict # This function checks if the points # form T-shape pointing up def isUpDirected(point, x, y): return (point[(x-1, y)] and point[(x, y)] and point[(x + 1, y)] and point[(x, y-1)] and point[(x, y-2)]) # This function checks if the points # form T-shape pointing down def isDownDirected(point, x, y): return (point[(x-1, y)] and point[(x, y)] and point[(x + 1, y)] and point[(x, y + 1)] and point[(x, y + 2)]) # This function checks if the points # form T-shape pointing left def isLeftDirected(point, x, y): return (point[(x, y + 1)] and point[(x, y)] and point[(x, y-1)] and point[(x + 1, y)] and point[(x + 2, y)]) # This function checks if the points # form T-shape pointing right def isRightDirected(point, x, y): return (point[(x, y + 1)] and point[(x, y)] and point[(x, y-1)] and point[(x-1, y)] and point[(x-2, y)]) # This function checks if given points # form a T-shape or not def solve(grid): # Initialize the dictionary with False value point = defaultdict(lambda: False) flag = False for i in range(len(grid)): # Assign True value to the points which # are present in the given list point[(grid[i][0], grid[i][1])] = True for i in range(len(grid)): # Check if the given points form any of the # 4 possible T-shaped formations if isUpDirected(point, grid[i][0], grid[i][1]) or isDownDirected(point, grid[i][0], grid[i][1]) or isLeftDirected(point, grid[i][0], grid[i][1]) or isRightDirected(point, grid[i][0], grid[i][1]): flag = True break if flag == True: return 'Yes' else: return 'No' print(solve([[7, 5], [8, 5], [6, 5], [7, 7], [7, 6]])) print(solve([[0, 0], [1, 0], [2, 0], [1, -1], [1, -2]]))
Javascript
// JavaScript code to check if given 5 // 2-D points form T-shape or not // Import the function to initialize the // dictionary with a specific value // This function checks if the points // form T-shape pointing up function isUpDirected(point, x, y){ return (point.has([x-1, y].join()) && point.has([x, y].join()) && point.has([x+1, y].join()) && point.has([x, y-1].join()) && point.has([x, y-2].join())); } // This function checks if the points // form T-shape pointing down function isDownDirected(point, x, y){ return (point.has([x-1, y].join()) && point.has([x, y].join()) && point.has([x+1, y].join()) && point.has([x, y+1].join()) && point.has([x, y+2].join())); } // This function checks if the points // form T-shape pointing left function isLeftDirected(point, x, y){ return (point.has([x, y+1].join()) && point.has([x, y].join()) && point.has([x, y-1].join()) && point.has([x+1, y].join()) && point.has([x+2, y].join())); } // This function checks if the points // form T-shape pointing right function isRightDirected(point, x, y){ return (point.has([x, y+1].join()) && point.has([x, y].join()) && point.has([x, y-1].join()) && point.has([x+1, y].join()) && point.has([x+2, y].join())); } // This function checks if given points // form a T-shape or not function solve(grid){ // Initialize the dictionary with False value let point = new Map(); let flag = false; for(let i = 0; i < grid.length; i++){ // Assign True value to the points which // are present in the given list point.set([grid[i][0], grid[i][1]].join(), true); } for(let i = 0; i < grid.length; i++){ // Check if the given points form any of the // 4 possible T-shaped formations if(isUpDirected(point, grid[i][0], grid[i][1]) || isDownDirected(point, grid[i][0], grid[i][1]) || isLeftDirected(point, grid[i][0], grid[i][1]) || isRightDirected(point, grid[i][0], grid[i][1])){ flag = true; break; } } if (flag == true){ return 'Yes'; } else{ return 'No'; } } // Driver code console.log(solve([[7, 5], [8, 5], [6, 5], [7, 7], [7, 6]])); console.log(solve([[0, 0], [1, 0], [2, 0], [1, -1], [1, -2]])); // The code is contributed by Nidhi goel
Producción:
Yes Yes
Complejidad de tiempo: O(1)
Publicación traducida automáticamente
Artículo escrito por rituraj_jain y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA