CLPFDを用いて解いた。こういうのがCLPFDの得意分野だと思う。
作成時間1時間弱?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
:-use_module(library(clpfd)). main:- List = [A,B,C,D,E,F,G,H,I,J], List ins 1..10, all_different(List), A #= 10 #\/ B #= 10 #\/ C #= 10 #\/ D #= 10 #\/ E #= 10, maplist(#<(A),[B,C,D,E]), maplist( sum, [[A,F,G],[B,G,H],[C,H,I],[D,I,J],[E,J,F]], [#=,#=,#=,#=,#=], [Sum,Sum,Sum,Sum,Sum]), flatten([[A,F,G],[B,G,H],[C,H,I],[D,I,J],[E,J,F]],Flatten), setof(Flatten,label(List),Num15List), maplist(num_chr_recursive,Num15List,Num16List), write(Num15List),nl,nl, write(Num16List),nl,nl, msort(Num16List,Sorted), write(Sorted),nl,nl, last(Sorted,Last), write('answer:'),nl, write(Last). num_chr_recursive([],[]):-!. num_chr_recursive([FirstNum|RestNums],Ret):- !, num_chr_recursive(RestNums,RestChrs), atom_chars(FirstNum,Chrs), append(Chrs,RestChrs,Ret). |
実行結果
[10] 25 ?- main.
***********************(解答伏せます)**********************
true.