:- prompt(_, '').
:- use_module(library(readutil)).
goal_check([[],_]).
goal_check([[X,X,X,X],_]).
move_check([],_,_):-!,false.
move_check([X,X,X,Y],[X,X,X],[Y]):-X\=Y.
move_check([X,X,Y,Y2],[X,X],[Y,Y2]):-X\=Y.
move_check([X,Y,Y2,Y3],[X],[Y,Y2,Y3]):-X\=Y.
move_check([X,X,X],[X,X,X],[]).
move_check([X,X,Y],[X,X],[Y]):-X\=Y.
move_check([X,Y,Y2],[X],[Y,Y2]):-X\=Y.
move_check([X,X],[X,X],[]).
move_check([X,Y],[X],[Y]):-X\=Y.
move_check([X],[X],[]).
move_ok(_,[]).
move_ok([X|_],[X|_]).
head([Xs,_],Xs).
get2([_,Xs],Xs).
swap([X,Y],[Y,X]).
cleaning_route
(Data
,[Next1
,Now2
]):-bagof(Now1
,member
([Next1
,Now1
],Data
),Nows
),[Now2
|_
]=Nows
.
move(Xs1,Res):-select(X1,Xs1,Xs2),
[E1,No1]=X1,
move_check(E1,E1Move,E1Next),
select(X2,Xs2,Xs),
[E2,No2]=X2,
move_ok(E1Move,E2),
append(E1Move,E2,E2Next),
length(E2Next,Len),
Len<5,
msort([[E1Next,No1],[E2Next,No2]|Xs],Res).
search_all_move(Data1,[Next,Now1]):-member([Now1,_],Data1),move(Now1,Next).
my_equal(E1,E2):-maplist(head,E1,E1A),
maplist(head,E2,E2A),
sort([E1A,E2A],[E1A]).
my_more(E1,E2):-maplist(head,E1,E1A),
maplist(head,E2,E2A),
sort([E1A,E2A],[E1A,E2A]).
deduplication([],Data,Data):-!.
deduplication(_,[],[]):-!.
deduplication([Commit1|Data1],[[Next2,_]|Data2],Res):-my_equal(Commit1,Next2),!,deduplication([Commit1|Data1],Data2,Res).
deduplication([Commit1|Data1],[[Next2,Now2]|Data2],Res):-my_more(Commit1,Next2),!,
deduplication(Data1,[[Next2,Now2]|Data2],Res).
deduplication([E1|Data1],[E2|Data2],[E2|Res]):-!,
deduplication([E1|Data1],Data2,Res).
format_ans(Data):-member([Now1,_],Data),
maplist(swap,Now1,Now2),
msort(Now2,Now3),
maplist(get2,Now3,Now4),
maplist
(writeln
,Now4
),nl,false
.format_ans(_).
get_next(Data,Res):-member([Res,_],Data).
bfs(64,AllData,Data,_):-!.
bfs(_,_,Data,[[Ans,Old1]]):-member([Ans,Old1],Data),maplist(goal_check,Ans),!.
bfs
(N
,AllData1
,Data1
,[[NowRes
,OldRes
]|Res
]):-N2
is N
+1, setof(E1
,search_all_move
(Data1
,E1
),Data2
), findall(E3
,cleaning_route
(Data2
,E3
),Data3
), sort(Data3,Data4),
length(Data4,Len4),
deduplication(AllData1,Data4,Data5),
findall(E5
,get_next
(Data5
,E5
),DataNext
), append(AllData1,DataNext,AllData2),
sort(AllData2,AllData3),
!,
bfs(N2,AllData3,Data5,Res),
[[NextRes,NowRes]|_]=Res,
member([NowRes,OldRes],Data1),
!.
main:-
process:-
/*
深さ4、ビーカー数可変のウォーターソートパズルを解くコード、答えの出力は手抜き状態、堀江 伸一
テキストファイルに出力するコードに変えないとな。
処理が重たくメモリも食うので、ビーカー数の少ない問題しか解けません。
一応手元で問題を1問正しく解いたのは確認済みです。
ただいま高速化の模索中
deduplicationがきちんと動いてない様子
*/
msort([[[1,2,1,2],1],[[3,4,1,3],2],[[2,4,4,3],3],[[4,1,2,3],4],[[],5],[[],6]],Test2),
bfs(0,[],[[Test2,[]]],Ans),
format_ans(Ans),
:-main.
Oi0gc2V0X3Byb2xvZ19mbGFnKHZlcmJvc2Usc2lsZW50KS4KOi0gcHJvbXB0KF8sICcnKS4KOi0gdXNlX21vZHVsZShsaWJyYXJ5KHJlYWR1dGlsKSkuCgpnb2FsX2NoZWNrKFtbXSxfXSkuCmdvYWxfY2hlY2soW1tYLFgsWCxYXSxfXSkuCgptb3ZlX2NoZWNrKFtdLF8sXyk6LSEsZmFsc2UuCm1vdmVfY2hlY2soW1gsWCxYLFldLFtYLFgsWF0sW1ldKTotWFw9WS4KbW92ZV9jaGVjayhbWCxYLFksWTJdLFtYLF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