From 54a04af93c23fca3e0d9453e11d63ff16ae01631 Mon Sep 17 00:00:00 2001 From: Tipragot Date: Wed, 28 Feb 2024 08:08:02 +0100 Subject: [PATCH] Fin exercice 14 --- Chapitre 4 - Graphes/C08_labyrinthe.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/Chapitre 4 - Graphes/C08_labyrinthe.py b/Chapitre 4 - Graphes/C08_labyrinthe.py index 0dfb85f..5bb1f04 100644 --- a/Chapitre 4 - Graphes/C08_labyrinthe.py +++ b/Chapitre 4 - Graphes/C08_labyrinthe.py @@ -2,7 +2,7 @@ # Chapitre 8 - Exercice Initié 14 - Sortie de labyrinthe # -from random import randint +from random import randint, choice from math import sqrt, ceil, floor # Changements de directions possibles depuis les @@ -51,7 +51,7 @@ class Labyrinthe: return f"Labyrinthe de {len(self.salles)} x {len(self.salles[0])} : {self.coord_entree} vers {self.coord_sortie}" - def salle(self, coords): + def salle(self, coords) -> Salle: """ Renvoie la Salle correspondant aux `coords` passées en argument, si elle existe. """ @@ -207,3 +207,19 @@ def sortie_main_droite(l: Labyrinthe): print(labyrinthe_simple.montrer()) print(sortie_main_droite(labyrinthe_simple)) + +def sortie_tremaux(l: Labyrinthe): + nodes = [l.coord_entree] + l.salle(nodes[-1]).visites += 1 + while nodes[-1] != l.coord_sortie: + voisins = [coord for coord in l.voisins(nodes[-1]) if not l.salle(coord).mur] + voisins_non_visite = [coord for coord in voisins if l.salle(coord).visites == 0] + if voisins_non_visite: + nodes.append(choice(voisins_non_visite)) + l.salle(nodes[-1]).visites += 1 + else: + nodes.pop() + return nodes + +print(labyrinthe_complexe_1.montrer()) +print(sortie_tremaux(labyrinthe_complexe_1)) \ No newline at end of file