Advertisement
pasteah

Untitled

Dec 9th, 2022
2,017
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 2.48 KB | None | 0 0
  1. % A matrix of 9x9 is represented as a list of lists, where each sub-list represents a row of the matrix.
  2. % The matrix is filled with numbers from 1 to 9, with 0 representing an empty cell.
  3.  
  4. % solve(Puzzle) :- Puzzle is a 9x9 matrix representing a sudoku puzzle.
  5. % The solution to the puzzle is returned in the variable Solution.
  6. solve(Puzzle, Solution) :-
  7.     % Convert the matrix into a list of 81 elements, with the empty cells represented as zeros.
  8.     flatten(Puzzle, FlatPuzzle),
  9.     % Create a list of variable names, with each variable representing a cell in the puzzle.
  10.     variables(FlatPuzzle, Vars),
  11.     % Create a list of constraints, representing the rules of sudoku.
  12.     constraints(Vars),
  13.     % Solve the puzzle by finding a satisfying assignment for the variables that satisfies the constraints.
  14.     labeling([ff], Vars),
  15.     % Convert the solution from a list of variables back into a 9x9 matrix.
  16.     matrix(Vars, Solution).
  17.  
  18. % variables(FlatPuzzle, Vars) :- Vars is a list of Prolog variables, with each variable representing a cell in the puzzle.
  19. % The length of the list Vars is equal to the number of cells in the puzzle, and the variables are initialized to the values in FlatPuzzle.
  20. variables([], []).
  21. variables([0|Tail], [Var|Vars]) :-
  22.     Var #= 0,
  23.     variables(Tail, Vars).
  24. variables([Val|Tail], [Var|Vars]) :-
  25.     Var #= Val,
  26.     variables(Tail, Vars).
  27.  
  28. % constraints(Vars) :- Vars is a list of Prolog variables representing the cells of a sudoku puzzle.
  29. % This predicate creates a list of constraints that must be satisfied in order to solve the puzzle.
  30. constraints(Vars) :-
  31.     % Each row must contain the numbers 1 to 9, without repetitions.
  32.     ( for(I,1,9), param(Vars) do
  33.         Row is Vars[(I-1)*9+1..I*9],
  34.         all_different(Row)
  35.     ),
  36.     % Each column must contain the numbers 1 to 9, without repetitions.
  37.     ( for(I,1,9), param(Vars) do
  38.         Col is Vars[I..81+I-9..9],
  39.         all_different(Col)
  40.     ),
  41.     % Each 3x3 sub-grid must contain the numbers 1 to 9, without repetitions.
  42.     ( for(I,0,2), param(Vars) do
  43.         ( for(J,0,2), param(Vars,I) do
  44.             BlockRowStart is 1 + I*3,
  45.             BlockRowEnd is BlockRowStart + 2,
  46.             BlockColStart is 1 + J*3,
  47.             BlockColEnd is BlockColStart + 2,
  48.             Block is Vars[BlockRowStart..BlockRowEnd][BlockColStart..BlockColEnd],
  49.             flatten(Block, FlattenedBlock),
  50.             all_different(FlattenedBlock)
  51.         )
  52.     ).
  53.  
  54. % matrix(Vars, Puzzle) :- Puzzle
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement