-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
110 lines (90 loc) · 3.48 KB
/
Copy pathtest.py
File metadata and controls
110 lines (90 loc) · 3.48 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import heapq
class Noeud:
def __init__(self, state, parent=None, coup=0):
self.state = state
self.parent = parent
self.coup = coup
self.heuristique_n = 0
self.etat_final = [
[1, 2, 3],
[4, 5, 6],
[7, 8, "X"]
]
self.heuristique()
self.total_cost = self.coup + self.heuristique_n
def __lt__(self, other):
return self.total_cost < other.total_cost
def is_goal(self):
return self.state == self.etat_final
def heuristique(self):
"""Calculer l'heuristique basée sur la distance de Manhattan."""
h = 0
for i, row in enumerate(self.state):
for j, valeur in enumerate(row):
if valeur != "X":
for x, row_final in enumerate(self.etat_final):
if valeur in row_final:
y = row_final.index(valeur)
h += abs(i - x) + abs(j - y)
break
self.heuristique_n = h
def get_neighbors(self):
"""Retourne les voisins en déplaçant 'X' dans toutes les directions possibles."""
x_pos = None
for i, lst in enumerate(self.state):
if "X" in lst:
x_pos = (i, lst.index("X"))
break
if x_pos is None:
return []
i, j = x_pos
moves = [(-1, 0), (1, 0), (0, -1), (0, 1)]
neighbors = []
for di, dj in moves:
ni, nj = i + di, j + dj
if 0 <= ni < 3 and 0 <= nj < 3:
new_state = [row[:] for row in self.state]
new_state[i][j], new_state[ni][nj] = new_state[ni][nj], new_state[i][j]
neighbors.append(Noeud(new_state, parent=self, coup=self.coup + 1))
return neighbors
def game_taquin(init_state):
"""Algorithme A* pour résoudre le Taquin."""
visited = {}
border = []
heapq.heappush(border, (init_state.total_cost, init_state))
while border:
_, current_node = heapq.heappop(border)
if current_node.is_goal():
return current_node
current_state_tuple = tuple(map(tuple, current_node.state))
# Marquer cet état comme visité avec le coût associé
if current_state_tuple in visited and visited[current_state_tuple] <= current_node.total_cost:
continue
visited[current_state_tuple] = current_node.total_cost
for neighbor in current_node.get_neighbors():
neighbor_state_tuple = tuple(map(tuple, neighbor.state))
# Si le voisin n'est pas visité ou a un coût inférieur, ajouter à la frontière
if neighbor_state_tuple not in visited or neighbor.total_cost < visited[neighbor_state_tuple]:
heapq.heappush(border, (neighbor.total_cost, neighbor))
return None # Retourne None si aucune solution n'est trouvée
# Exemple d'utilisation
init_state = Noeud([
[3, 6, 8],
[1, 2, 7],
[5, 4, "X"]
])
result = game_taquin(init_state)
# Affichage du résultat
if result:
solution_path = []
while result:
solution_path.append(result.state)
result = result.parent
solution_path.reverse()
print("Chemin vers la solution :")
for state in solution_path:
for row in state:
print(row)
print("----")
else:
print("Aucune solution trouvée.")