オフラインどう書くE27参考問題解答

問題はこちら->http://nabetani.sakura.ne.jp/hena/orde27ligmir/

box/3で進行方向の変化を記述、d/3で実際の進行先を記述しています。
初期値として進行方向が決まっていますので、d/3で次の升を求め、nth1/3でそこの記号を決め、
box/3で次の進行方向を求め、Rに追加します。
進めない場合は同じ升をRに追加して、停止条件とします。（path/5の一行目）
Pに経路を記録し、循環したら停止します。（path/5の二行目）

```prolog
%swi-Prolog version 7.4.2
%start.
%:-initialization(start).   %ideone

box('0',u,l):-!.
box('0',r,d):-!.
box('1',u,r):-!.
box('1',l,d):-!.
box(S,X,Y):-(S=='0';S=='1'),box(S,Y,X),!.
box(_,X,X).

d(u,X,X1):-X>5,X1 is X-5.
d(d,X,X1):-X<21,X1 is X+5.
d(l,X,X1):-not(1 is X mod 5),X1 is X-1.
d(r,X,X1):-not(0 is X mod 5),X1 is X+1.
d(_,X,X).

path(_,_,_,R,R1):-R=[X,X|T],reverse([X|T],R1).
path(_,_,P,R,R):-msort(P,P1),not(sort(P1,P1)).
path(L,D1,P,R1,R):-R1=[X1|_],d(D1,X1,X),nth1(X,L,V),P1=[X1;X|P],
    (V==x->path(_,_,P,[X1|R1],R);(box(V,D1,D),R2=[X|R1],path(L,D,P1,R2,R))).

ch(X,Y):-X1 is X+96,atom_codes(Y,[X1]).

solve(L,R):-nth1(N,L,'Y'),path(L,u,[],[N],R1),maplist(ch,R1,R2),sort(R2,R),!.

start:-str(S),split_string(S,"\n,\s","\s",L0),pre(L0).

pre([]).
pre([_,B,C|T]):-atomics_to_string(L,"/",B),maplist(atom_chars,L,L1),flatten(L1,L2),
    solve(L2,R),disp(R,C),pre(T).

disp(R,Q):-atom_chars(S,R),atom_chars(Q1,Q),
    (S==Q1->Str=" pass ";Str=" fail "),write(Str),write(S),write("\n").
    
str("0       x...x/.1.0./..0../.Y.../0..x.   ghilnqs
1       ..Y../...../...../...../.....   c
2       ..x../..Y../...../...../.....   h
3       ..Y.x/..1x0/11.../....0/1..1.   c
4       ....1/....Y/...../...../.....   ej
5       .10../x.1../x.1x./.Y.1./...0.   bcghlq
6       0.x10/00..x/x0x.0/....0/...Y1   deinsx
7       1.01./01Y.1/..1.1/..10./0.0..   abcfgh
8       x..../x1x../0...0/....Y/.1..0   klmnot
9       ...../..10./.1Y1./.01../.....   hilmnqr
10      ...../..10./x.11./...../..Y..   hilmnrw
11      ...../x.10x/...../x.Y1x/.....   himnqrs
12      ..010/...Y1/..0../0.x../.....   defghij
13      1.0../...../.0x../Y.1x./..1..   abcfhkp
14      ...../101../0.0../..Y../.....   fgklmqrv
15      1.0../00.../.x..0/0.Y1./...10   abcfghmr
16      x101./1..../.Y.x./..01./.00.1   bcghlmrs
17      x11../x.x../.0.01/..x../...Y.   bcglmnsx
18      ..1.0/x0.x./0.0../x...Y/.10.1   cdehjmnot
19      ..x.0/.0.../1..0x/1..1./Y.00.   klmnpqrsu
20      0.1.0/.0.xY/0...0/01..1/x00.x   cdehjmrwx
21      ...0./.0.0./..101/...10/..01Y   mnpqrstwxy
22      10..0/.Y.0./0..1./....x/000..   abfghiklmn
23      10..1/...../.1010/110.1/x..Yx   lmnopqrstx
24      110../....1/x1..x/0.0.0/....Y   bcghlmrsty
25      x.101/1..../..001/010Yx/..1.1   cdehijmnos
26      x.111/x10../...0./00.1x/x.Y.1   ghklmnqrsw
27      11.../....0/11..1/1.1../.Y..1   fghijlmnoqv
28      ...x1/.1.0./11.1./.01../Y..x.   cghiklmnpqru
29      .0.../110x./11..0/01.x./..Y.x   ghklmnopqrtw
30      .01.0/.110x/0...0/.01Y./x.1x.   cdeghilmnqrs
31      .1100/..1.0/1.11Y/0..1./.0..0   hijklmnopqrs
32      1..00/..11./.100./1..Y1/.....   abcdfhikmnps
33      1.0../.11x0/.00.x/Y.10./.10x0   abcfghklmpqr
34      11110/11.../.x.../.0111/0.Y0.   deijnorstwxy
35      ...1./.1.0x/10..0/0Y.11/.0.x0   ghiklmnopqrst
36      ...10/x111./0x.11/.0.../0.0Y.   dehijmnorswxy
37      .1x../.x1.0/0x.x./x11.1/x0Y.1   hijmoqrstvwxy
38      x.x../x110./1.1.0/0.Y.1/0.00x   hiklmnopqrstx
39      ...0./11.00/10..x/..0.1/Y0.10   ghiklmnpqsuvwx
40      .110./....0/x..../.0001/11.Y.   cdfghijmnorstx
41      1.00./....1/.1.../0...0/0..1Y   abcfhkmpqrstwy
42      .1.01/..x../..100/..Y../...01   bcdgilmnoqrstvxy
43      1...0/Y..../...../...../0...1   abcdefjkoptuvwxy
44      x1..0/1..0./.Yx../0...1/.0.1.   bcdefghijklnopqrstvwx
45      1...0/.1.0./..1../..01./Y0..1   abcdefghijklmnopqrstuvwxy").
```