Prolog 序言:“;假;带着疑问
我想用SWI Prolog解决一个9x9数独难题 我给出了一个空数独的查询,但它没有给出任何错误,只是声明为“false” 代码也可以正确编译 我的代码如下所示:Prolog 序言:“;假;带着疑问,prolog,sudoku,clpfd,Prolog,Sudoku,Clpfd,我想用SWI Prolog解决一个9x9数独难题 我给出了一个空数独的查询,但它没有给出任何错误,只是声明为“false” 代码也可以正确编译 我的代码如下所示: :- use_module(library(clpfd)). valid([]). valid([Head|Tail]) :- all_different(Head), valid(Tail). sudoku(Puzzle, Solution) :- Solution = Puzzle, Puzzle = [S11
:- use_module(library(clpfd)).
valid([]).
valid([Head|Tail]) :-
all_different(Head),
valid(Tail).
sudoku(Puzzle, Solution) :-
Solution = Puzzle,
Puzzle = [S11, S12, S13, S14, S15, S16, S17, S18, S19,
S21, S22, S23, S24, S25, S26, S27, S28, S29,
S31, S32, S33, S34, S35, S36, S37, S38, S39,
S41, S42, S43, S44, S45, S46, S47, S48, S49,
S51, S52, S53, S54, S55, S56, S57, S58, S59,
S61, S62, S63, S64, S65, S66, S67, S68, S69,
S71, S72, S73, S74, S75, S76, S77, S78, S79,
S81, S82, S83, S84, S85, S86, S87, S88, S89,
S91, S92, S93, S94, S95, S96, S97, S98, S99],
Puzzle ins 1..9,
Row1 = [S11, S12, S13, S14, S15, S16, S17, S18, S19],
Row2 = [S21, S22, S23, S24, S25, S26, S27, S28, S29],
Row3 = [S31, S32, S33, S34, S35, S36, S37, S38, S39],
Row4 = [S41, S42, S43, S44, S45, S46, S47, S48, S49],
Row5 = [S51, S52, S53, S54, S55, S56, S57, S58, S59],
Row6 = [S61, S62, S63, S64, S65, S66, S67, S68, S69],
Row7 = [S71, S72, S73, S74, S75, S76, S77, S78, S79],
Row8 = [S81, S82, S83, S84, S85, S86, S87, S88, S89],
Row9 = [S91, S92, S93, S94, S95, S96, S97, S98, S99],
Col1 = [S11, S21, S31, S41, S51, S61, S71, S81, S91],
Col2 = [S21, S22, S32, S42, S52, S62, S72, S82, S92],
Col3 = [S31, S32, S33, S43, S53, S63, S73, S83, S93],
Col4 = [S41, S42, S43, S44, S54, S64, S74, S84, S94],
Col5 = [S51, S52, S53, S54, S55, S65, S75, S85, S95],
Col6 = [S61, S62, S63, S64, S65, S66, S76, S86, S96],
Col7 = [S71, S72, S73, S74, S75, S76, S77, S87, S97],
Col8 = [S81, S82, S83, S84, S85, S86, S87, S88, S98],
Col9 = [S91, S92, S93, S94, S95, S96, S97, S98, S99],
Square1 = [S11, S12, S13, S21, S22, S23, S31, S32, S33],
Square2 = [S14, S15, S16, S24, S25, S26, S34, S35, S36],
Square3 = [S17, S18, S19, S27, S28, S29, S37, S38, S39],
Square4 = [S41, S42, S43, S51, S52, S53, S61, S62, S63],
Square5 = [S44, S45, S46, S54, S55, S56, S64, S65, S66],
Square6 = [S47, S48, S49, S57, S58, S59, S67, S68, S69],
Square7 = [S71, S72, S73, S81, S82, S83, S91, S92, S93],
Square8 = [S74, S75, S76, S84, S85, S86, S94, S95, S96],
Square9 = [S77, S78, S79, S87, S88, S89, S97, S98, S99],
valid([Row1, Row2, Row3, Row4, Row5, Row6, Row7, Row8, Row9,
Col1, Col2, Col3, Col4, Col5, Col6, Col7, Col8, Col9,
Square1, Square2, Square3, Square4, Square5, Square6, Square7, Square8, Square9]).
我的问题是:
sudoku([_, 9, 6, 8, 5, 1, _, 4, _,
1, _, 8, 2, 9, 4, 3, 5, 6,
5, 2, _, 6, _, _, 8, _, 1,
9, _, 5, _, 8, 7, 2, 6, 3,
4, 8, 3, 9, _, 6, 5, _, 7,
_, 6, 7, 3, 1, _, 9, 8, 4,
6, 5, _, 1, 3, 8, _, 7, 9,
7, 4, 9, 5, 6, 2, 1, _, 8,
_, 3, 1, 7, _, 9, _, 2, 5],
Solution).
我已经无数次地查看了代码,不知道为什么它没有返回真值
谢谢我想也许你在ColX's中复制/粘贴的速度太快了。在许多情况下,第一位和第二位数字是交换的。试试这个:
Col1 = [S11, S21, S31, S41, S51, S61, S71, S81, S91],
Col2 = [S12, S22, S32, S42, S52, S62, S72, S82, S92],
Col3 = [S13, S23, S33, S43, S53, S63, S73, S83, S93],
Col4 = [S14, S24, S34, S44, S54, S64, S74, S84, S94],
Col5 = [S15, S25, S35, S45, S55, S65, S75, S85, S95],
Col6 = [S16, S26, S36, S46, S56, S66, S76, S86, S96],
Col7 = [S17, S27, S37, S47, S57, S67, S77, S87, S97],
Col8 = [S18, S28, S38, S48, S58, S68, S78, S88, S98],
Col9 = [S19, S29, S39, S49, S59, S69, S79, S89, S99],
?- sudoku([_, 9, 6, 8, 5, 1, _, 4, _,
1, _, 8, 2, 9, 4, 3, 5, 6,
5, 2, _, 6, _, _, 8, _, 1,
9, _, 5, _, 8, 7, 2, 6, 3,
4, 8, 3, 9, _, 6, 5, _, 7,
_, 6, 7, 3, 1, _, 9, 8, 4,
6, 5, _, 1, 3, 8, _, 7, 9,
7, 4, 9, 5, 6, 2, 1, _, 8,
_, 3, 1, 7, _, 9, _, 2, 5],
Solution).
Solution = [3, 9, 6, 8, 5, 1, 7, 4, 2|...].
也许那个例子是不可解的。尝试放松模式,将其他数字改为
。
@capelical,我很确定这是可以解决的。您是否认为我的查询格式可能是错误的,因此它会返回“false”?Colx
变量是错误的。他们应该注册Col2=[S12,S22,S32…]
但是为什么不maplist(所有不同的,拼图),转置(拼图,T),maplist(所有不同的,T)…
非常感谢!是的,当这个想法开始流行时我很兴奋。。。在看了这么多次之后,我想我已经有了幻觉,再也看不到数字了。