fork(1) download
  1. :- set_prolog_flag(verbose,silent).
  2. :- prompt(_, '').
  3. :- use_module(library(readutil)).
  4.  
  5. goal_check([]).
  6. goal_check([[]|Xs]):-goal_check(Xs).
  7. goal_check([[X,X,X,X]|Xs]):-goal_check(Xs).
  8.  
  9. move_check([X,X,X,Y],[X,X,X],[Y]):-X\=Y.
  10. move_check([X,X,Y,Y2],[X,X],[Y,Y2]):-X\=Y.
  11. move_check([X,Y,Y2,Y3],[X],[Y,Y2,Y3]):-X\=Y.
  12. move_check([X,X,X],[X,X,X],[]).
  13. move_check([X,X,Y],[X,X],[Y]):-X\=Y.
  14. move_check([X,Y,Y2],[X],[Y,Y2]):-X\=Y.
  15. move_check([X,X],[X,X],[]).
  16. move_check([X,Y],[X],[Y]):-X\=Y.
  17. move_check([X],[X],[]).
  18.  
  19. move_ok(_,[]).
  20. move_ok([X|_],[X|_]).
  21.  
  22. move(Xs1,Res):-select(E1,Xs1,Xs2),
  23. move_check(E1,E1Move,E1Next),
  24. select(E2,Xs2,Xs),
  25. move_ok(E1Move,E2),
  26. append(E1Move,E2,E2Next),
  27. length(E2Next,Len),
  28. Len<5,
  29. msort([E1Next,E2Next|Xs],Res).
  30.  
  31. search_all_move(Data1,[Now1,Next]):-member([Old1,Now1],Data1),move(Now1,Next).
  32.  
  33. swap([Old1,Now1],[Now1,Old1]).
  34.  
  35. deduplication([],Data,Data):-!.
  36. deduplication(_,[],[]):-!.
  37. deduplication([[Now1,Old1]|Data1],[[_,Old1]|Data2],Res):-!,deduplication(Data1,Data2,Res).
  38. deduplication([Now1,_]|Data1],[[Now2,Next2]|Data2],Res):-Now1<Now2,!,
  39. deduplication(Data1,[[Now2,Next2]|Data2],Res).
  40. deduplication([E1|Data1],[E2|Data2],[E2|Res]):-!,
  41. deduplication([E1|Data1],Data2,Res).
  42. bfs(Data,[[Old1,Ans]]):-member([Old1|Ans],Data),maplist(goal_check,Ans),!.
  43. bfs(Data1,[[Old1,Now1]|Res1]):-setof(E1,search_all_move(Data1,E1),Data2),
  44. sort(Data2,Data3),
  45. maplist(swap,Data1,Data1Swap),
  46. sort(Data1Swap,Data1Swap2),
  47. deduplication(Data1Swap2,Data3,Data4),
  48. deduplication(Data1,Data4,Data5),
  49. bfs(Data5,Res1),
  50. [[Now1,_]|_]=Res1,
  51. member([Old1,Now1],Data1),
  52. !.
  53.  
  54. main:-
  55. process,
  56.  
  57. process:-
  58. /*ビーカーを並べ替えていいと言う条件での ウォーターソートパズルを解くコード、答えの出力は手抜き状態、堀江 伸一*/
  59. msort([[1,2,2,2],[2,1,1,1],[],[]],Test),
  60. bfs([[[],Test]],Ans),
  61. maplist(writeln,Ans),
  62.  
  63. :- main.
Success #stdin #stdout #stderr 0.07s 7120KB
stdin
Standard input is empty
stdout
[[],[[],[],[1,2,2,2],[2,1,1,1]]]
[[[],[],[1,2,2,2],[2,1,1,1]],[[],[1],[2,1,1,1],[2,2,2]]]
[[[],[1],[2,1,1,1],[2,2,2]],[[],[1],[1,1,1],[2,2,2,2]]]
[[[],[1],[1,1,1],[2,2,2,2]],[[[],[],[1,1,1,1],[2,2,2,2]]]]
stderr
Warning: /home/1ZpGJx/prog:31:
	Singleton variables: [Old1]
Warning: /home/1ZpGJx/prog:37:
	Singleton variables: [Now1]
ERROR: /home/1ZpGJx/prog:38:28: Syntax error: Illegal start of term