fork(2) 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,Old1]|Data1],[[_,Now1]|Data2],Res):-!,deduplication(Data1,Data2,Res).
  39. deduplication([Now1,_]|Data1],[[Now2,Next2]|Data2],Res):-Now1<Now2,!,
  40. deduplication(Data1,[[Now2,Next2]|Data2],Res).
  41. deduplication([E1|Data1],[E2|Data2],[E2|Res]):-!,
  42. deduplication([E1|Data1],Data2,Res).
  43. bfs(Data,Ans):-member([_|Ans],Data),maplist(goal_check,Ans),!.
  44. bfs(Data1,Res):-setof(E1,search_all_move(Data1,E1),Data2),
  45. maplist(swap,Data1,Data1Swap),
  46. sort(Data1Swap,Data1Swap2),
  47. deduplication(Data1Swap2,Data2,Data3),
  48. sort(Data3,Data4),
  49. maplist(writeln,Data4),nl,
  50. bfs(Data4,Res1).
  51.  
  52. main:-
  53. process,
  54.  
  55. process:-
  56. /* ウォーターソートパズルを解くコード、答えの出力は手抜き予定、書きかけ、堀江 */
  57. msort([[1,2,2,2],[2,1,1,1],[],[]],Test),
  58. bfs([[[],Test]],_),
  59.  
  60. :- main.
Success #stdin #stdout #stderr 0.07s 7088KB
stdin
Standard input is empty
stdout
[[[],[],[1,2,2,2],[2,1,1,1]],[[],[1],[2,1,1,1],[2,2,2]]]
[[[],[],[1,2,2,2],[2,1,1,1]],[[],[1,1,1],[1,2,2,2],[2]]]

[[[],[1],[2,1,1,1],[2,2,2]],[[],[1],[1,1,1],[2,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]]]
[[[],[1,1,1],[1,2,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]]]
[[[],[1,1,1,1],[2],[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]]]
[[[1],[1,1,1],[2],[2,2,2]],[[],[1,1,1,1],[2],[2,2,2]]]

stderr
Warning: /home/I3tJRC/prog:31:
	Singleton variables: [Old1]
Warning: /home/I3tJRC/prog:37:
	Singleton variables: [Now1]
Warning: /home/I3tJRC/prog:38:
	Singleton variables: [Old1]
ERROR: /home/I3tJRC/prog:39:28: Syntax error: Illegal start of term
Warning: /home/I3tJRC/prog:44:
	Singleton variables: [Res,Res1]