affiche juste le code, pas d'explications !
- User
System: affiche juste le code, pas d'explications ! User: Tu es un développeur python3 expérimenté, et tu dois concevoir un programme en python (avec des commentaires en français, sans utiliser de librairies externes, sans
if __name__=="__main__":, avec type hints, et en privilégiant les algorythmes a complexité constante (O(1)) et/ou complexité linéaire (O(n))). Le programme va recevoir les données en entrée sur la stdin, et doit répondre sur la stdout. Fabrique moi un programme python qui résout le problème (énoncé en markdown) suivant :Climbing
At the climbing site, the route setters didn't finish the final route, and now it's up to you to complete it.
The wall has a starting hold, a finishing hold, and numerous other holds in between.
The athletes will climb as efficiently as possible, aiming to minimize the number of holds they use. Any unnecessary holds can be removed.
Your goal is to find which holds to keep, forming the route that allows athletes to climb the wall using the fewest holds.
There is one constraint: the climber’s reach. We assume the climber can only grab holds that are within a certain distance, equal to or less than their reach, from their current hold. Also, the climber can only grab one hold at a time.
Data
Input
All distances and coordinates are in centimeters.
Line 1: an integer
S, the climber's reach (150 <=S<= 200).Line 2: two integers
X``sandY``s(separated by a space), the coordinates of the starting hold.Line 3: two integers
X``fandY``f(separated by a space), the coordinates of the finishing hold.Line 4: an integer
N, the number of other holds on the wall (1 <=N<= 500).The next
Nlines: two integersX``iandY``i(separated by a space), the coordinates of theith hold.The coordinates of the holds are between 0 and 5000 inclusive.
Output
The coordinates of the holds that form the route allowing the climber to ascend the wall using the fewest holds, or
-1if the climber’s reach doesn't allow them to complete the route.Print one hold per line, in the order of the route (including the starting and finishing holds), with the coordinates separated by a space
Example 1
For the input:
175 500 100 500 1000 22 390 187 297 305 297 480 314 622 351 767 404 909 504 250 555 375 602 503 592 608 501 647 472 777 537 879 625 198 700 267 800 333 849 499 830 601 840 690 800 800 750 850 640 950 - Assistant
# Importations nécessaires import sys import math from typing import List, Tuple # Fonction pour calculer la distance entre deux points def distance(p1: Tuple[int, int], p2: Tuple[int, int]) -> float: return math.sqrt((p1[0] - p2[0]) ** 2 + (p1[1] - p2[1]) ** 2) # Fonction principale pour trouver la route la plus courte def find_shortest_route(S: int, start: Tuple[int, int], finish: Tuple[int, int], holds: List[Tuple[int, int]]) -> List[Tuple[int, int]]: # Ajoute ne manière linéaire les distance possible pour chaque point holds.insert(0, start) holds.append(finish) # Initialisation de la dynamique et du chemin n = len(holds) dp = [float('inf')] * n path = [-1] * n dp[0] = 0 # Calcul dynamique pour trouver le chemin avec le moins de prises for i in range(n): for j in range(i + 1, n): if distance(holds[i], holds[j]) <= S: if dp[i] + 1 < dp[j]: dp[j] = dp[i] + 1 path[j] = i # Génération du chemin en remontant if dp[n - 1] == float('inf'): return [-1] route = [] current = n - 1 while current != -1: route.append(holds[current]) current = path[current] route.reverse() return route # Lecture des entrées input = sys.stdin.read data = input().strip().split() S = int(data[0]) # Portée maximale du grimpeur start = (int(data[1]), int(data[2])) # Point de départ finish = (int(data[3]), int(data[4])) # Point de fin N = int(data[5]) # Nombre de prises supplémentaires # Liste des prises supplémentaires holds = [(int(data[i]), int(data[i+1])) for i in range(6, len(data), 2)] # Appel de la fonction pour trouver la solution result = find_shortest_route(S, start, finish, holds) # Impression du résultat for hold in result: if hold == -1: print(hold) else: print(f"{hold[0]} {hold[1]}")