Prolog 人工智能语言中文论坛---打造优质Prolog学习交流园地
Would you like to react to this message? Create an account in a few clicks or log in to continue.
Prolog 人工智能语言中文论坛---打造优质Prolog学习交流园地

一个供Prolog爱好者学习与交流的地方


您没有登录。 请登录注册

用prolog解这个数独

2 posters

向下  留言 [第1页/共1页]

1用prolog解这个数独 Empty 用prolog解这个数独 周三 二月 20, 2013 11:07 am

wowsfsf



[_,_,5,_,_,_,4,_,_]
[_,_,2,1,6,4,7,_,_]
[_,1,_,_,9,_,_,3,_]
[4,_,_,9,_,2,_,_,3]
[_,_,9,_,1,_,2,_,_]
[8,_,_,6,_,5,_,_,7]
[_,5,_,_,7,_,_,8,_]
[_,_,8,4,2,6,5,_,_]
[_,_,7,_,_,_,3,_,_]

2用prolog解这个数独 Empty 回复: 用prolog解这个数独 周三 二月 20, 2013 4:29 pm

Mercury Liao


Admin

wowsfsf 写道:[_,_,5,_,_,_,4,_,_]
[_,_,2,1,6,4,7,_,_]
[_,1,_,_,9,_,_,3,_]
[4,_,_,9,_,2,_,_,3]
[_,_,9,_,1,_,2,_,_]
[8,_,_,6,_,5,_,_,7]
[_,5,_,_,7,_,_,8,_]
[_,_,8,4,2,6,5,_,_]
[_,_,7,_,_,_,3,_,_]

这篇有解过数独:
http://prolog.longluntan.net/t38-topic


把这段代码里陈述题目的部分改成你这个题目就可以解出来,其他部分完全不需要改动:

代码:
shudu_rule(Scope, I, Total_cell) :-
  numlist(1, 9, J), repeat_list(9, I, I2, []), repeat_list(9, Total_cell, T, []),
  maplist(choose_number, Scope, T, I2, J).

choose_number(Deter, Total_cell, I, J) :- var(Deter) -> candidate(C, Total_cell, I, J), member(Deter, C) ; true.

candidate(Can, Total_cell, I, J) :-
  nth1(1, Total_cell, Line_set), nth1(2, Total_cell, Column_set), nth1(3, Total_cell, BigCell_set),
  nth1(I, Line_set, Line), nth1(J, Column_set, Column),
  JJ is floor((J - 1) / 3) + 1, II is floor((I - 1) / 3) + 1, K is (II - 1) * 3 + JJ, nth1(K, BigCell_set, BigCell),
  copy_term(Line, SmallLine), list_to_set(SmallLine, Out), numlist(1, 9, A), subtract(A, Out, C),
  copy_term(Column, SmallColumn), list_to_set(SmallColumn, Out2), subtract(C, Out2, Ca),
  copy_term(BigCell, SmallBigCell), list_to_set(SmallBigCell, Out3), subtract(Ca, Out3, Can).

repeat_list(N, Ele, F, A) :- N > 0 -> append(A, [Ele], A1), N1 is N - 1, repeat_list(N1, Ele, F, A1); F = A.


shudu :-

Line1 = [S11,S12,5,S14,S15,S16,4,S18,S19],
Line2 = [S21,S22,2,1,6,4,7,S28,S29],
Line3 = [S31,1,S33,S34,9,S36,S37,3,S39],
Line4 = [4,S42,S43,9,S45,2,S47,S48,3],
Line5 = [S51,S52,9,S54,1,S56,2,S58,S59],
Line6 = [8,S62,S63,6,S65,5,S67,S68,7],
Line7 = [S71,5,S73,S74,7,S76,S77,8,S79],
Line8 = [S81,S82,8,4,2,6,5,S88,S89],
Line9 = [S91,S92,7,S94,S95,S96,3,S98,S99],

Column1 = [S11,S21,S31,4,S51,8,S71,S81,S91],
Column2 = [S12,S22,1,S42,S52,S62,5,S82,S92],
Column3 = [5,2,S33,S43,9,S63,S73,8,7],
Column4 = [S14,1,S34,9,S54,6,S74,4,S94],
Column5 = [S15,6,9,S45,1,S65,7,2,S95],
Column6 = [S16,4,S36,2,S56,5,S76,6,S96],
Column7 = [4,7,S37,S47,2,S67,S77,5,3],
Column8 = [S18,S28,3,S48,S58,S68,8,S88,S98],
Column9 = [S19,S29,S39,3,S59,7,S79,S89,S99],

BigCell1 = [S11,S12,5,S21,S22,2,S31,1,S33],
BigCell2 = [S14,S15,S16,1,6,4,S34,9,S36],
BigCell3 = [4,S18,S19,7,S28,S29,S37,3,S39],
BigCell4 = [4,S42,S43,S51,S52,9,8,S62,S63],
BigCell5 = [9,S45,2,S54,1,S56,6,S65,5],
BigCell6 = [S47,S48,3,2,S58,S59,S67,S68,7],
BigCell7 = [S71,5,S73,S81,S82,8,S91,S92,7],
BigCell8 = [S74,7,S76,4,2,6,S94,S95,S96],
BigCell9 = [S77,8,S79,5,S88,S89,3,S98,S99],

Line_set = [Line1, Line2 ,Line3, Line4, Line5 ,Line6, Line7, Line8 ,Line9],
Column_set = [Column1, Column2 ,Column3, Column4, Column5 ,Column6, Column7, Column8 ,Column9],
BigCell_set = [BigCell1, BigCell2 , BigCell3, BigCell4, BigCell5 , BigCell6, BigCell7, BigCell8 , BigCell9],
Total_cell = [Line_set, Column_set, BigCell_set],

numlist(1, 9, I), repeat_list(9, Total_cell, T, []),
maplist(shudu_rule, Line_set, I, T),

write(Line1), nl, write(Line2), nl, write(Line3), nl,
write(Line4), nl, write(Line5), nl, write(Line6), nl,
write(Line7), nl, write(Line8), nl, write(Line9).


解得:

8 ?- shudu.
[7,9,5,2,3,8,4,6,1]
[3,8,2,1,6,4,7,9,5]
[6,1,4,5,9,7,8,3,2]
[4,7,1,9,8,2,6,5,3]
[5,6,9,7,1,3,2,4,8]
[8,2,3,6,4,5,9,1,7]
[2,5,6,3,7,9,1,8,4]
[1,3,8,4,2,6,5,7,9]
[9,4,7,8,5,1,3,2,6]
true .

http://prolog.longluntan.net

3用prolog解这个数独 Empty 回复: 用prolog解这个数独 周二 二月 26, 2013 4:52 am

wowsfsf



Mercury Liao 写道:
wowsfsf 写道:[_,_,5,_,_,_,4,_,_]
[_,_,2,1,6,4,7,_,_]
[_,1,_,_,9,_,_,3,_]
[4,_,_,9,_,2,_,_,3]
[_,_,9,_,1,_,2,_,_]
[8,_,_,6,_,5,_,_,7]
[_,5,_,_,7,_,_,8,_]
[_,_,8,4,2,6,5,_,_]
[_,_,7,_,_,_,3,_,_]

这篇有解过数独:
http://prolog.longluntan.net/t38-topic


把这段代码里陈述题目的部分改成你这个题目就可以解出来,其他部分完全不需要改动:

代码:
shudu_rule(Scope, I, Total_cell) :-
  numlist(1, 9, J), repeat_list(9, I, I2, []), repeat_list(9, Total_cell, T, []),
  maplist(choose_number, Scope, T, I2, J).

choose_number(Deter, Total_cell, I, J) :- var(Deter) -> candidate(C, Total_cell, I, J), member(Deter, C) ; true.

candidate(Can, Total_cell, I, J) :-
  nth1(1, Total_cell, Line_set), nth1(2, Total_cell, Column_set), nth1(3, Total_cell, BigCell_set),
  nth1(I, Line_set, Line), nth1(J, Column_set, Column),
  JJ is floor((J - 1) / 3) + 1, II is floor((I - 1) / 3) + 1, K is (II - 1) * 3 + JJ, nth1(K, BigCell_set, BigCell),
  copy_term(Line, SmallLine), list_to_set(SmallLine, Out), numlist(1, 9, A), subtract(A, Out, C),
  copy_term(Column, SmallColumn), list_to_set(SmallColumn, Out2), subtract(C, Out2, Ca),
  copy_term(BigCell, SmallBigCell), list_to_set(SmallBigCell, Out3), subtract(Ca, Out3, Can).

repeat_list(N, Ele, F, A) :- N > 0 -> append(A, [Ele], A1), N1 is N - 1, repeat_list(N1, Ele, F, A1); F = A.


shudu :-

Line1 = [S11,S12,5,S14,S15,S16,4,S18,S19],
Line2 = [S21,S22,2,1,6,4,7,S28,S29],
Line3 = [S31,1,S33,S34,9,S36,S37,3,S39],
Line4 = [4,S42,S43,9,S45,2,S47,S48,3],
Line5 = [S51,S52,9,S54,1,S56,2,S58,S59],
Line6 = [8,S62,S63,6,S65,5,S67,S68,7],
Line7 = [S71,5,S73,S74,7,S76,S77,8,S79],
Line8 = [S81,S82,8,4,2,6,5,S88,S89],
Line9 = [S91,S92,7,S94,S95,S96,3,S98,S99],

Column1 = [S11,S21,S31,4,S51,8,S71,S81,S91],
Column2 = [S12,S22,1,S42,S52,S62,5,S82,S92],
Column3 = [5,2,S33,S43,9,S63,S73,8,7],
Column4 = [S14,1,S34,9,S54,6,S74,4,S94],
Column5 = [S15,6,9,S45,1,S65,7,2,S95],
Column6 = [S16,4,S36,2,S56,5,S76,6,S96],
Column7 = [4,7,S37,S47,2,S67,S77,5,3],
Column8 = [S18,S28,3,S48,S58,S68,8,S88,S98],
Column9 = [S19,S29,S39,3,S59,7,S79,S89,S99],

BigCell1 = [S11,S12,5,S21,S22,2,S31,1,S33],
BigCell2 = [S14,S15,S16,1,6,4,S34,9,S36],
BigCell3 = [4,S18,S19,7,S28,S29,S37,3,S39],
BigCell4 = [4,S42,S43,S51,S52,9,8,S62,S63],
BigCell5 = [9,S45,2,S54,1,S56,6,S65,5],
BigCell6 = [S47,S48,3,2,S58,S59,S67,S68,7],
BigCell7 = [S71,5,S73,S81,S82,8,S91,S92,7],
BigCell8 = [S74,7,S76,4,2,6,S94,S95,S96],
BigCell9 = [S77,8,S79,5,S88,S89,3,S98,S99],

Line_set = [Line1, Line2 ,Line3, Line4, Line5 ,Line6, Line7, Line8 ,Line9],
Column_set = [Column1, Column2 ,Column3, Column4, Column5 ,Column6, Column7, Column8 ,Column9],
BigCell_set = [BigCell1, BigCell2 , BigCell3, BigCell4, BigCell5 , BigCell6, BigCell7, BigCell8 , BigCell9],
Total_cell = [Line_set, Column_set, BigCell_set],

numlist(1, 9, I), repeat_list(9, Total_cell, T, []),
maplist(shudu_rule, Line_set, I, T),

write(Line1), nl, write(Line2), nl, write(Line3), nl,
write(Line4), nl, write(Line5), nl, write(Line6), nl,
write(Line7), nl, write(Line8), nl, write(Line9).


解得:

8 ?- shudu.
[7,9,5,2,3,8,4,6,1]
[3,8,2,1,6,4,7,9,5]
[6,1,4,5,9,7,8,3,2]
[4,7,1,9,8,2,6,5,3]
[5,6,9,7,1,3,2,4,8]
[8,2,3,6,4,5,9,1,7]
[2,5,6,3,7,9,1,8,4]
[1,3,8,4,2,6,5,7,9]
[9,4,7,8,5,1,3,2,6]
true .
谢谢
请问这是用GNU proplog做的吗

4用prolog解这个数独 Empty 回复: 用prolog解这个数独 周二 二月 26, 2013 5:44 am

Mercury Liao


Admin

wowsfsf 写道:
Mercury Liao 写道:
wowsfsf 写道:[_,_,5,_,_,_,4,_,_]
[_,_,2,1,6,4,7,_,_]
[_,1,_,_,9,_,_,3,_]
[4,_,_,9,_,2,_,_,3]
[_,_,9,_,1,_,2,_,_]
[8,_,_,6,_,5,_,_,7]
[_,5,_,_,7,_,_,8,_]
[_,_,8,4,2,6,5,_,_]
[_,_,7,_,_,_,3,_,_]

这篇有解过数独:
http://prolog.longluntan.net/t38-topic


把这段代码里陈述题目的部分改成你这个题目就可以解出来,其他部分完全不需要改动:

代码:
shudu_rule(Scope, I, Total_cell) :-
  numlist(1, 9, J), repeat_list(9, I, I2, []), repeat_list(9, Total_cell, T, []),
  maplist(choose_number, Scope, T, I2, J).

choose_number(Deter, Total_cell, I, J) :- var(Deter) -> candidate(C, Total_cell, I, J), member(Deter, C) ; true.

candidate(Can, Total_cell, I, J) :-
  nth1(1, Total_cell, Line_set), nth1(2, Total_cell, Column_set), nth1(3, Total_cell, BigCell_set),
  nth1(I, Line_set, Line), nth1(J, Column_set, Column),
  JJ is floor((J - 1) / 3) + 1, II is floor((I - 1) / 3) + 1, K is (II - 1) * 3 + JJ, nth1(K, BigCell_set, BigCell),
  copy_term(Line, SmallLine), list_to_set(SmallLine, Out), numlist(1, 9, A), subtract(A, Out, C),
  copy_term(Column, SmallColumn), list_to_set(SmallColumn, Out2), subtract(C, Out2, Ca),
  copy_term(BigCell, SmallBigCell), list_to_set(SmallBigCell, Out3), subtract(Ca, Out3, Can).

repeat_list(N, Ele, F, A) :- N > 0 -> append(A, [Ele], A1), N1 is N - 1, repeat_list(N1, Ele, F, A1); F = A.


shudu :-

Line1 = [S11,S12,5,S14,S15,S16,4,S18,S19],
Line2 = [S21,S22,2,1,6,4,7,S28,S29],
Line3 = [S31,1,S33,S34,9,S36,S37,3,S39],
Line4 = [4,S42,S43,9,S45,2,S47,S48,3],
Line5 = [S51,S52,9,S54,1,S56,2,S58,S59],
Line6 = [8,S62,S63,6,S65,5,S67,S68,7],
Line7 = [S71,5,S73,S74,7,S76,S77,8,S79],
Line8 = [S81,S82,8,4,2,6,5,S88,S89],
Line9 = [S91,S92,7,S94,S95,S96,3,S98,S99],

Column1 = [S11,S21,S31,4,S51,8,S71,S81,S91],
Column2 = [S12,S22,1,S42,S52,S62,5,S82,S92],
Column3 = [5,2,S33,S43,9,S63,S73,8,7],
Column4 = [S14,1,S34,9,S54,6,S74,4,S94],
Column5 = [S15,6,9,S45,1,S65,7,2,S95],
Column6 = [S16,4,S36,2,S56,5,S76,6,S96],
Column7 = [4,7,S37,S47,2,S67,S77,5,3],
Column8 = [S18,S28,3,S48,S58,S68,8,S88,S98],
Column9 = [S19,S29,S39,3,S59,7,S79,S89,S99],

BigCell1 = [S11,S12,5,S21,S22,2,S31,1,S33],
BigCell2 = [S14,S15,S16,1,6,4,S34,9,S36],
BigCell3 = [4,S18,S19,7,S28,S29,S37,3,S39],
BigCell4 = [4,S42,S43,S51,S52,9,8,S62,S63],
BigCell5 = [9,S45,2,S54,1,S56,6,S65,5],
BigCell6 = [S47,S48,3,2,S58,S59,S67,S68,7],
BigCell7 = [S71,5,S73,S81,S82,8,S91,S92,7],
BigCell8 = [S74,7,S76,4,2,6,S94,S95,S96],
BigCell9 = [S77,8,S79,5,S88,S89,3,S98,S99],

Line_set = [Line1, Line2 ,Line3, Line4, Line5 ,Line6, Line7, Line8 ,Line9],
Column_set = [Column1, Column2 ,Column3, Column4, Column5 ,Column6, Column7, Column8 ,Column9],
BigCell_set = [BigCell1, BigCell2 , BigCell3, BigCell4, BigCell5 , BigCell6, BigCell7, BigCell8 , BigCell9],
Total_cell = [Line_set, Column_set, BigCell_set],

numlist(1, 9, I), repeat_list(9, Total_cell, T, []),
maplist(shudu_rule, Line_set, I, T),

write(Line1), nl, write(Line2), nl, write(Line3), nl,
write(Line4), nl, write(Line5), nl, write(Line6), nl,
write(Line7), nl, write(Line8), nl, write(Line9).


解得:

8 ?- shudu.
[7,9,5,2,3,8,4,6,1]
[3,8,2,1,6,4,7,9,5]
[6,1,4,5,9,7,8,3,2]
[4,7,1,9,8,2,6,5,3]
[5,6,9,7,1,3,2,4,8]
[8,2,3,6,4,5,9,1,7]
[2,5,6,3,7,9,1,8,4]
[1,3,8,4,2,6,5,7,9]
[9,4,7,8,5,1,3,2,6]
true .
谢谢
请问这是用GNU proplog做的吗

我用的是SWI-Prolog

http://prolog.longluntan.net

5用prolog解这个数独 Empty 回复: 用prolog解这个数独 周二 二月 26, 2013 5:47 am

wowsfsf



Mercury Liao 写道:
wowsfsf 写道:
Mercury Liao 写道:
wowsfsf 写道:[_,_,5,_,_,_,4,_,_]
[_,_,2,1,6,4,7,_,_]
[_,1,_,_,9,_,_,3,_]
[4,_,_,9,_,2,_,_,3]
[_,_,9,_,1,_,2,_,_]
[8,_,_,6,_,5,_,_,7]
[_,5,_,_,7,_,_,8,_]
[_,_,8,4,2,6,5,_,_]
[_,_,7,_,_,_,3,_,_]

这篇有解过数独:
http://prolog.longluntan.net/t38-topic


把这段代码里陈述题目的部分改成你这个题目就可以解出来,其他部分完全不需要改动:

代码:
shudu_rule(Scope, I, Total_cell) :-
  numlist(1, 9, J), repeat_list(9, I, I2, []), repeat_list(9, Total_cell, T, []),
  maplist(choose_number, Scope, T, I2, J).

choose_number(Deter, Total_cell, I, J) :- var(Deter) -> candidate(C, Total_cell, I, J), member(Deter, C) ; true.

candidate(Can, Total_cell, I, J) :-
  nth1(1, Total_cell, Line_set), nth1(2, Total_cell, Column_set), nth1(3, Total_cell, BigCell_set),
  nth1(I, Line_set, Line), nth1(J, Column_set, Column),
  JJ is floor((J - 1) / 3) + 1, II is floor((I - 1) / 3) + 1, K is (II - 1) * 3 + JJ, nth1(K, BigCell_set, BigCell),
  copy_term(Line, SmallLine), list_to_set(SmallLine, Out), numlist(1, 9, A), subtract(A, Out, C),
  copy_term(Column, SmallColumn), list_to_set(SmallColumn, Out2), subtract(C, Out2, Ca),
  copy_term(BigCell, SmallBigCell), list_to_set(SmallBigCell, Out3), subtract(Ca, Out3, Can).

repeat_list(N, Ele, F, A) :- N > 0 -> append(A, [Ele], A1), N1 is N - 1, repeat_list(N1, Ele, F, A1); F = A.


shudu :-

Line1 = [S11,S12,5,S14,S15,S16,4,S18,S19],
Line2 = [S21,S22,2,1,6,4,7,S28,S29],
Line3 = [S31,1,S33,S34,9,S36,S37,3,S39],
Line4 = [4,S42,S43,9,S45,2,S47,S48,3],
Line5 = [S51,S52,9,S54,1,S56,2,S58,S59],
Line6 = [8,S62,S63,6,S65,5,S67,S68,7],
Line7 = [S71,5,S73,S74,7,S76,S77,8,S79],
Line8 = [S81,S82,8,4,2,6,5,S88,S89],
Line9 = [S91,S92,7,S94,S95,S96,3,S98,S99],

Column1 = [S11,S21,S31,4,S51,8,S71,S81,S91],
Column2 = [S12,S22,1,S42,S52,S62,5,S82,S92],
Column3 = [5,2,S33,S43,9,S63,S73,8,7],
Column4 = [S14,1,S34,9,S54,6,S74,4,S94],
Column5 = [S15,6,9,S45,1,S65,7,2,S95],
Column6 = [S16,4,S36,2,S56,5,S76,6,S96],
Column7 = [4,7,S37,S47,2,S67,S77,5,3],
Column8 = [S18,S28,3,S48,S58,S68,8,S88,S98],
Column9 = [S19,S29,S39,3,S59,7,S79,S89,S99],

BigCell1 = [S11,S12,5,S21,S22,2,S31,1,S33],
BigCell2 = [S14,S15,S16,1,6,4,S34,9,S36],
BigCell3 = [4,S18,S19,7,S28,S29,S37,3,S39],
BigCell4 = [4,S42,S43,S51,S52,9,8,S62,S63],
BigCell5 = [9,S45,2,S54,1,S56,6,S65,5],
BigCell6 = [S47,S48,3,2,S58,S59,S67,S68,7],
BigCell7 = [S71,5,S73,S81,S82,8,S91,S92,7],
BigCell8 = [S74,7,S76,4,2,6,S94,S95,S96],
BigCell9 = [S77,8,S79,5,S88,S89,3,S98,S99],

Line_set = [Line1, Line2 ,Line3, Line4, Line5 ,Line6, Line7, Line8 ,Line9],
Column_set = [Column1, Column2 ,Column3, Column4, Column5 ,Column6, Column7, Column8 ,Column9],
BigCell_set = [BigCell1, BigCell2 , BigCell3, BigCell4, BigCell5 , BigCell6, BigCell7, BigCell8 , BigCell9],
Total_cell = [Line_set, Column_set, BigCell_set],

numlist(1, 9, I), repeat_list(9, Total_cell, T, []),
maplist(shudu_rule, Line_set, I, T),

write(Line1), nl, write(Line2), nl, write(Line3), nl,
write(Line4), nl, write(Line5), nl, write(Line6), nl,
write(Line7), nl, write(Line8), nl, write(Line9).


解得:

8 ?- shudu.
[7,9,5,2,3,8,4,6,1]
[3,8,2,1,6,4,7,9,5]
[6,1,4,5,9,7,8,3,2]
[4,7,1,9,8,2,6,5,3]
[5,6,9,7,1,3,2,4,8]
[8,2,3,6,4,5,9,1,7]
[2,5,6,3,7,9,1,8,4]
[1,3,8,4,2,6,5,7,9]
[9,4,7,8,5,1,3,2,6]
true .
谢谢
请问这是用GNU proplog做的吗

我用的是SWI-Prolog
请问能转换成gnu prolog的吗

返回页首  留言 [第1页/共1页]

您在这个论坛的权限:
不能在这个论坛回复主题