-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsudoku_solver.pl
46 lines (40 loc) · 1.05 KB
/
sudoku_solver.pl
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
% REGLAS
% Definir una cuadrícula de Sudoku 4x4 válida
valid_sudoku(Board) :-
valid_rows(Board),
transpose(Board, Columns),
valid_rows(Columns),
valid_regions(Board).
% Definir filas válidas
valid_rows([]).
valid_rows([Row | Rest]) :-
permutation([1, 2, 3, 4], Row),
valid_rows(Rest).
% Definir regiones válidas (subcuadrículas 2x2)
valid_regions(Board) :-
valid_region(Board, 0, 0),
valid_region(Board, 0, 2),
valid_region(Board, 2, 0),
valid_region(Board, 2, 2).
valid_region(Board, RowStart, ColStart) :-
Region = [A, B, C, D],
Row1 is RowStart,
Row2 is RowStart + 1,
Col1 is ColStart,
Col2 is ColStart + 1,
nth0(Row1, Board, R1),
nth0(Row2, Board, R2),
nth0(Col1, R1, A),
nth0(Col2, R1, B),
nth0(Col1, R2, C),
nth0(Col2, R2, D),
permutation([1, 2, 3, 4], Region).
% Predicado para resolver el sudoku
sudoku(Board) :-
valid_sudoku(Board),
print_board(Board).
% Imprimir el tablero
print_board([]).
print_board([Row | Rest]) :-
write(Row), nl,
print_board(Rest).