LoginSignup
0
0

More than 5 years have passed since last update.

オフラインリアルタイムどう書くの過去問(続柄)

Posted at

2013.1.13の問題です。->http://nabetani.sakura.ne.jp/hena/ord6kinship/
F06と同様にツリーが使えそうですので解いてみました。

tree(N,[]):-N>40,!.
tree(N,List):-N2 is N*3,N1 is N2-1,N3 is N2+1,tree(N1,L),tree(N2,M),tree(N3,R),
                List=[N,L,M,R].

re(me,X,Y,_):-X=:=Y .
re(da,X,Y,L):-search(X,Y,L).
re(mo,X,Y,L):-re(da,Y,X,L).
re(si,X,Y,L):-re(mo,X,Z,L),re(da,Z,Y,L),X=\=Y.
re(au,X,Y,L):-re(mo,X,Z,L),re(si,Z,Y,L).
re(ni,X,Y,L):-re(si,X,Z,L),re(da,Z,Y,L).
re(co,X,Y,L):-re(au,X,Z,L),re(da,Z,Y,L).
re(-,_,_,_).

search(_,_,[]):-fail.
search(X,Y,[N,L,M,R]):-
       (X=N,(L=[Y,_,_,_];M=[Y,_,_,_];R=[Y,_,_,_]));
       (search(X,Y,L);search(X,Y,M);search(X,Y,R)).

solve(_,[]).
solve(L,[A,B,C|T]):-
       split_string(B,"-",">",[X,Y]),number_chars(X1,X),number_chars(Y1,Y),atom_chars(C1,C),
       re(R,X1,Y1,L),write(A),(C1=R->write(" yes  ");write(" no    ")),write(R),write("\n"),
       solve(L,T),!.

start:-str(S),tree(1,T),split_string(S,"\s,\n","\s",L),solve(T,L).
%start.

str("0       5->2    mo
1       28->10  au
2       1->1    me
3       40->40  me
4       27->27  me
5       7->2    mo
6       40->13  mo
7       9->3    mo
8       4->1    mo
9       1->3    da
10      12->35  da
11      3->8    da
12      6->19   da
13      38->40  si
14      9->8    si
15      4->2    si
16      15->16  si
17      40->12  au
18      10->4   au
19      21->5   au
20      8->2    au
21      3->5    ni
22      11->39  ni
23      2->13   ni
24      13->32  ni
25      14->22  co
26      40->34  co
27      5->8    co
28      12->10  co
29      1->27   -
30      8->1    -
31      12->22  -
32      2->40   -
33      32->31  -
34      13->14  -").

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0