fork 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,[[Old5,Now5]|Res5]):-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. writeln(Data5),
  50. bfs(Data5,Res5),
  51. [[Now5|_]|_]=Res5,
  52. member([Old5,Now5],Res5),
  53. !.
  54.  
  55. main:-
  56. process,
  57.  
  58. process:-
  59. /* ウォーターソートパズルを解くコード、答えの出力は手抜き予定、書きかけ、堀江 */
  60. msort([[1,2,2,2],[2,1,1,1],[],[]],Test),
  61. bfs([[[],Test]],Ans),
  62. maplist(writeln,Ans),
  63.  
  64. :- main.
Success #stdin #stdout #stderr 0.04s 7284KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Warning: /home/vQPmRH/prog:31:
	Singleton variables: [Old1]
Warning: /home/vQPmRH/prog:37:
	Singleton variables: [Now1]
ERROR: /home/vQPmRH/prog:66:
	deduplication/3: Type error: `character' expected, found `[]' (an empty_list)
Warning: /home/vQPmRH/prog:66:
	Goal (directive) failed: user:main
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit