Skip to content
Snippets Groups Projects
Commit ee7cc9fe authored by Lucas B's avatar Lucas B
Browse files

Fin de l'amelioration

parent ba0ddd2e
No related branches found
No related tags found
No related merge requests found
0 0 5 0 0 0 9 6 0
0 6 0 3 0 0 0 2 0
0 9 0 0 0 0 0 0 0
1 0 8 0 0 0 0 0 7
0 0 0 0 0 2 0 0 3
0 0 0 8 0 0 5 0 0
0 0 0 2 6 9 0 0 0
4 0 0 0 5 0 0 0 0
5 7 0 0 3 0 0 0 0
......@@ -84,8 +84,8 @@ class Sudoku_Problem:
def initial_state(self) -> State:
"""Renvoie l’état initial"""
file_path = self.choisir_fichier_au_hasard("grid")
#file_path = "grid/test/sudoku_75.txt" # grille de test
#file_path = self.choisir_fichier_au_hasard("grid")
file_path = "grid/sudoku_7.txt" # grille de test
try:
with open(file_path, 'r') as file:
grid = []
......@@ -102,6 +102,10 @@ class Sudoku_Problem:
except ValueError:
assert False, "Invalid file format. Ensure the file contains a 9x9 grid of integers."
def est_valide(self, state: State, action: Tuple[Tuple[int, int], int]) -> bool:
return self.est_vide(state, action) and self.accept_carre(state,action) and self.accept_column(state, action) and self.accept_row(state, action)
def est_vide(self, state: State, action: Tuple[Tuple[int, int], int]) -> bool:
return state.grid[action[0][0]][action[0][1]] == 0
......@@ -264,6 +268,7 @@ class Sudoku_Problem:
if (row, col) not in cells and state.grid[row][col] == 0:
for number in combined_notes:
if number not in set.union(*(notes[cell] for cell in cells)):
if self.est_valide(state, ((row, col), number)):
actions_possible.append(((row, col), number))
# Ajouter les solutions pour les singletons nus
......@@ -271,6 +276,7 @@ class Sudoku_Problem:
for cell, candidates in notes.items():
if len(candidates) == 1:
number = candidates.pop()
if self.est_valide(state, (cell, number)):
actions_possible.append((cell, number))
# Vérifier dans chaque ligne, colonne et bloc
......@@ -322,14 +328,16 @@ class Sudoku_Problem:
if candidate_size == 1:
for number in numbers:
for position in candidate_positions[number]:
if self.est_valide(state, (position, number)):
actions_possible.append((position, number))
else:
# Retirer les autres candidats des cases partagées
for position in combined_positions:
row, col = position
possible_numbers = set(numbers)
actions_possible.extend([(position, num) for num in possible_numbers])
for number in possible_numbers:
if self.est_valide(state, (position, number)):
actions_possible.append((position, number))
# Vérifier dans chaque ligne, colonne et bloc
for i in range(state.N_ROWS):
process_group([(i, j) for j in range(state.N_COLS)]) # Ligne
......@@ -405,8 +413,10 @@ class Sudoku_Problem:
for row in range(state.N_ROWS):
if row not in (row1, row2):
if state.grid[row][col1] == 0:
if self.est_valide(state, ((row, col1), number)):
actions_possible.append(((row, col1), number))
if state.grid[row][col2] == 0:
if self.est_valide(state, ((row, col2), number)):
actions_possible.append(((row, col2), number))
return actions_possible
......@@ -441,8 +451,8 @@ class Sudoku_Problem:
shared_note = common_note[0]
# Éliminer la note commune dans la case où les pinces se croisent
for row, col in [(r1, c2), (r2, c1)]:
if state.grid[row][col] == 0 and shared_note in self.get_candidates(state, row,
col):
if state.grid[row][col] == 0 and shared_note in self.get_candidates(state, row, col):
if self.est_valide(state, ((row, col), shared_note)):
actions_possible.append(((row, col), shared_note))
return actions_possible
......@@ -477,6 +487,7 @@ class Sudoku_Problem:
if row not in row_combination:
for col in cols_involved:
if state.grid[row][col] == 0:
if self.est_valide(state, ((row, col), number)):
actions_possible.append(((row, col), number))
return actions_possible
......@@ -532,8 +543,11 @@ class Sudoku_Problem:
for col in range(state.N_COLS):
boolean, action = self.last_possible_number(state, (row, col))
if boolean:
if self.est_vide(state, action) and self.accept_carre(state, action) and self.accept_column(state,action) and self.accept_row(state, action):
actions_possible.append(action)
return actions_possible # Retour immédiat dès qu'une action est trouvée
else:
return []
actions_possible = self.naked_candidates(state, 1)
if actions_possible:
......@@ -659,7 +673,6 @@ class Node:
def expand(self, problem: Sudoku_Problem) -> List["Node"]:
"""Renvoie la liste des nœuds accessibles en une seule étape à partir de ce nœud."""
n = problem.actions(self.state)
return [self.child_node(problem, action) for action in problem.actions(self.state)]
def solution(self) -> List[Tuple[int, int]]:
......@@ -718,7 +731,7 @@ def depth_first_tree_search(problem):
for nouveauNoeud in noeud.expand(problem):
if tuple(map(tuple, nouveauNoeud.state.grid)) not in noeud_deja_vu and (nouveauNoeud not in noeud_a_traiter):
noeud_a_traiter.append(nouveauNoeud)
#print(len(noeud_deja_vu), len(noeud_a_traiter))
print(len(noeud_deja_vu), len(noeud_a_traiter))
print("aucune solution n'existe")
......@@ -729,11 +742,12 @@ knights_problem = Sudoku_Problem()
start = time.time()
solution_node = depth_first_tree_search(knights_problem)
end = time.time()
"""
if solution_node:
for node in solution_node.path():
print(node)
else:
print(knights_problem.initial_state())
print(knights_problem.initial_state())"""
def convertir_ms(ms):
minutos = ms // 60 # División entera para obtener los minutos
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment