Posted By

13tazer31 on 02/05/11


Tagged

sudoku


Versions (?)

Sudoku Solver


 / Published in: Prolog
 

URL: http://13tazer31.wordpress.com/2011/02/05/sudoku-solver/

  1. sudoku(Puzzle, Solution) :-
  2. Puzzle = Solution,
  3. Puzzle = [S11, S12, S13, S14, S15, S16, S17, S18, S19,
  4. S21, S22, S23, S24, S25, S26, S27, S28, S29,
  5. S31, S32, S33, S34, S35, S36, S37, S38, S39,
  6. S41, S42, S43, S44, S45, S46, S47, S48, S49,
  7. S51, S52, S53, S54, S55, S56, S57, S58, S59,
  8. S61, S62, S63, S64, S65, S66, S67, S68, S69,
  9. S71, S72, S73, S74, S75, S76, S77, S78, S79,
  10. S81, S82, S83, S84, S85, S86, S87, S88, S89,
  11. S91, S92, S93, S94, S95, S96, S97, S98, S99],
  12.  
  13. valid_domain(Puzzle, 1, 9),
  14.  
  15. Row1 = [S11, S12, S13, S14, S15, S16, S17, S18, S19],
  16. Row2 = [S21, S22, S23, S24, S25, S26, S27, S28, S29],
  17. Row3 = [S31, S32, S33, S34, S35, S36, S37, S38, S39],
  18. Row4 = [S41, S42, S43, S44, S45, S46, S47, S48, S49],
  19. Row5 = [S51, S52, S53, S54, S55, S56, S57, S58, S59],
  20. Row6 = [S61, S62, S63, S64, S65, S66, S67, S68, S69],
  21. Row7 = [S71, S72, S73, S74, S75, S76, S77, S78, S79],
  22. Row8 = [S81, S82, S83, S84, S85, S86, S87, S88, S89],
  23. Row9 = [S91, S92, S93, S94, S95, S96, S97, S98, S99],
  24.  
  25. Col1 = [S11, S21, S31, S41, S51, S61, S71, S81, S91],
  26. Col2 = [S12, S22, S32, S42, S52, S62, S72, S82, S92],
  27. Col3 = [S13, S23, S33, S43, S53, S63, S73, S83, S93],
  28. Col4 = [S14, S24, S34, S44, S54, S64, S74, S84, S94],
  29. Col5 = [S15, S25, S35, S45, S55, S65, S75, S85, S95],
  30. Col6 = [S16, S26, S36, S46, S56, S66, S76, S86, S96],
  31. Col7 = [S17, S27, S37, S47, S57, S67, S77, S87, S97],
  32. Col8 = [S18, S28, S38, S48, S58, S68, S78, S88, S98],
  33. Col9 = [S19, S29, S39, S49, S59, S69, S79, S89, S99],
  34.  
  35. Squ1 = [S11, S12, S13, S21, S22, S23, S31, S32, S33],
  36. Squ2 = [S14, S15, S16, S24, S25, S26, S34, S35, S36],
  37. Squ3 = [S17, S18, S19, S27, S28, S29, S37, S38, S39],
  38. Squ4 = [S41, S42, S43, S51, S52, S53, S61, S62, S63],
  39. Squ5 = [S44, S45, S46, S54, S55, S56, S64, S65, S66],
  40. Squ6 = [S47, S48, S49, S57, S58, S59, S67, S68, S69],
  41. Squ7 = [S71, S72, S73, S81, S82, S83, S91, S92, S93],
  42. Squ8 = [S74, S75, S76, S84, S85, S86, S94, S95, S96],
  43. Squ9 = [S77, S78, S79, S87, S88, S89, S97, S98, S99],
  44.  
  45.  
  46.  
  47. valid([Row1, Row2, Row3, Row4, Row5, Row6, Row7, Row8, Row9, Col1, Col2, Col3, Col4, Col5, Col6, Col7, Col8, Col9, Squ1, Squ2, Squ3, Squ4, Squ5, Squ6, Squ7, Squ8, Squ9]),
  48.  
  49. lab(Puzzle),
  50.  
  51. show([Row1, Row2, Row3, Row4, Row5, Row6, Row7, Row8, Row9]).
  52.  
  53.  
  54. valid_domain(List, Min, Max) :-
  55. fd_domain(List, Min, Max).
  56.  
  57. valid([]).
  58. valid([Head|Tail]) :-
  59. fd_all_different(Head),
  60. valid(Tail).
  61.  
  62.  
  63. show([]).
  64. show([Head|Tail]) :-
  65. show_row(Head),
  66. write('|'), nl,
  67. show(Tail).
  68.  
  69. show_row([]).
  70. show_row([Head|Tail]) :-
  71. write('|'),
  72. write(Head),
  73. show_row(Tail).
  74.  
  75. lab(List) :-
  76. fd_labeling(List).

Report this snippet  

You need to login to post a comment.