Last active
December 10, 2015 02:58
-
-
Save SFantasy/4371792 to your computer and use it in GitHub Desktop.
I learned it from the book 'Seven languages in seven weeks', it is so fun solving Sudoku using Prolog! 一种用来快速解决Sudoku的编程方式--逻辑编程,Programming Logically! 如果用来解决6*6或者9*9的Sudoku,更改构造的Puzzle即可。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*验证该列表中没有重复的数字*/ | |
valid([]). | |
valid([Head|Tail]) :- | |
fd_all_different(Head), | |
valid(Tail). | |
/*构造Puzzle*/ | |
sudoku(Puzzle, Solution) :- | |
Solution = Puzzle, | |
Puzzle = [S11, S12, S13, S14, | |
S21, S22, S23, S24, | |
S31, S32, S33, S34, | |
S41, S42, S43, S44], | |
fd_domain(Puzzle, 1, 4), | |
Row1 = [S11, S12, S13, S14], | |
Row2 = [S21, S22, S23, S24], | |
Row3 = [S31, S32, S33, S34], | |
Row4 = [S41, S42, S43, S44], | |
Col1 = [S11, S21, S31, S41], | |
Col2 = [S12, S22, S32, S42], | |
Col3 = [S13, S23, S33, S43], | |
Col4 = [S14, S24, S34, S44], | |
Square1 = [S11, S12, S21, S22], | |
Square2 = [S13, S14, S23, S24], | |
Square3 = [S31, S32, S41, S42], | |
Square4 = [S33, S34, S43, S44], | |
valid([Row1, Row2, Row3, Row4, | |
Col1, Col2, Col3, Col4, | |
Square1, Square2, Square3, Square4]). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment