1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
| def choisir_sommet(graphe:dict, actif:str, pred:str) -> str:
"""Renvoie l'étiquette de la première destination disponible"""
successeurs = graphe[f"{actif} depuis {pred}"]
for sommet in successeurs: # Pour chaque sommet dans la liste d'adjacence reçue
if sommet != pred: # Si ce sommet n'est pas celui d'où on vient
return sommet # Renvoyer ce sommet
return pred # Sinon, c'est un cul de sac, on repart d'où on vient...
def marche_avant(graphe:dict, depart:str, sortie:str) -> None:
"""Simule une marche (un peu moins) naïve"""
actif = depart
pred = ""
while actif != sortie:
print(f"Position actuelle : {actif} en venant de {pred}")
input("ENTREE pour effectuer le mouvement suivant")
avant_dpct = actif
actif = choisir_sommet(graphe, actif, pred)
pred = avant_dpct
print(f"Bravo, vous êtes en {actif}")
# Programme principal
graphe = {}
graphe['a depuis '] = ['c', 'b', 'd']
graphe['a depuis d'] = ['c', 'b', 'd'] # liste d'adjacence de a si on provient de d
graphe['a depuis b'] = ['d', 'c', 'b']
graphe['a depuis c'] = ['b', 'd', 'c']
graphe['b depuis '] = ['c', 'i', 'a']
graphe['b depuis a'] = ['c', 'i', 'a']
graphe['b depuis c'] = ['i', 'a', 'c']
graphe['b depuis i'] = ['a', 'c', 'i']
graphe['c depuis '] = ['b', 'a']
graphe['c depuis a'] = ['e', 'b', 'a']
graphe['c depuis b'] = ['a', 'e', 'b']
graphe['c depuis e'] = ['b', 'a', 'e']
graphe['d depuis '] = ['f', 'e', 'a']
graphe['d depuis a'] = ['f', 'e', 'a']
graphe['d depuis f'] = ['e', 'a', 'd']
graphe['d depuis e'] = ['a', 'f', 'e']
graphe['e depuis '] = ['h', 'c', 'd']
graphe['e depuis d'] = ['h', 'c', 'd']
graphe['e depuis h'] = ['c', 'h', 'd']
graphe['e depuis c'] = ['d', 'h', 'c']
graphe['f depuis '] = ['d', 'g']
graphe['f depuis g'] = ['d', 'g']
graphe['f depuis d'] = ['g', 'd']
graphe['g depuis '] = ['f', 'h']
graphe['g depuis h'] = ['f', 'h']
graphe['g depuis f'] = ['h', 'f']
graphe['h depuis '] = ['e', 'g', 'k', 'j']
graphe['h depuis j'] = ['e', 'g', 'k', 'j']
graphe['h depuis g'] = ['k', 'j', 'e', 'g']
graphe['h depuis e'] = ['g', 'k', 'j', 'e']
graphe['h depuis k'] = ['j', 'e', 'g', 'k']
graphe['i depuis '] = ['b']
graphe['i depuis b'] = ['b']
graphe['j depuis '] = ['h', 'k']
graphe['j depuis k'] = ['h', 'k']
graphe['j depuis h'] = ['k', 'h']
graphe['k depuis '] = ['h', 'j']
graphe['k depuis j'] = ['h', 'j']
graphe['k depuis h'] = ['j', 'h']
marche_avant(graphe, 'd', 'i')
|