Public Zone 公開區 > General Topics 綜合題目

Felix's puzzle

<< < (3/3)

kido:
I have seen original question posted on other forum.... for completeness, here is my LAST attempt... which should solve this problem completely.... However, this is just kind of fun, don't take it seriously (ain't I?)  ;) ;)


--- Quote ---parent_child(X,Y) :- father_child(X,Y).
parent_child(X,Y) :- mother_child(X,Y).

sibling(X, Y) :- parent_child(Z, X), parent_child(Z, Y), X\=Y.

mate(X,Y) :- father_child(X,Z), mother_child(Y,Z).

mate(X,Y) :- mother_child(X,Z), father_child(Y,Z).

son(X,Y) :-   parent_child(Y,X).

%%
%% Facts
%%

%% 2樓
mother_child(girlmom,girl).

%% 3樓
father_child(boy,girl).

%% 4樓
%%mother_child(girl, xxx).
%%father_child(boy, xxx).

%% 5樓
father_child(girldad,boy).
mother_child(boymom,boy).

%% 6樓
%% mate(boymom,girldad).

%% 7樓
sibling(boydad,girldad).

%% 8樓
%%mother_child(girlmom,boymom).
mother_child(boymom,girlmom).

%% 9樓
father_child(boydad,girlmom).

related(X,Y, 'mate') :- mate(X,Y).
related(X,Y, 'parent') :- parent_child(X,Y).
related(X,Y, 'sibling') :- sibling(X,Y).
related(X,Y, 'son') :- son(X,Y).

path(A,B,Path) :-
       travel(A,B,[A],Path).

travel(A,B,P,[B|P]) :-
       related(A,B,R),
      write(R), nl.
travel(A,B,Visited,Path) :-
      related(A,C,R),  
       C \= B,
       \+member(C,Visited),
       travel(C,B,[C|Visited],Path),
      write(R), nl.

--- End quote ---

Notes:

(1) Further thro' in relation 'son' (in fact son or daughter)

(2) Revamped relation 'related' again.

(3) Now the 'path' predicate should find ALL possible relation instead of just finding ONE solution as done previously.

Now run it again:


--- Quote ---1 ?- path(boymom,girldad,P).
mate
P = [girldad, boymom] ;

sibling
mate
P = [girldad, boydad, boymom] ;

son
mate
parent
mate
P = [girldad, boy, girlmom, boydad, boymom] ;

son
son
parent
parent
mate
P = [girldad, boy, girl, girlmom, boydad, boymom] ;

son
sibling
parent
mate
P = [girldad, boy, girlmom, boydad, boymom] ;

son
parent
P = [girldad, boy, boymom] ;

sibling
son
mate
parent
P = [girldad, boydad, girlmom, boy, boymom] ;

sibling
son
son
parent
parent
P = [girldad, boydad, girlmom, girl, boy, boymom] ;

sibling
son
sibling
parent
P = [girldad, boydad, girlmom, boy, boymom] ;

son
mate
parent
P = [girldad, boy, girlmom, boymom] ;

son
son
parent
parent
P = [girldad, boy, girl, girlmom, boymom] ;

son
sibling
parent
P = [girldad, boy, girlmom, boymom] ;

sibling
son
parent
P = [girldad, boydad, girlmom, boymom] ;

false.

--- End quote ---

The program shows that there are > 10 relations between them.... To name them.... I will leave to anyone who are interested.

chin:
And the answer to the last question?

kido:
Oh, yeah, I thought it was asking 男友他的媽見到女友的爸  instead of  男友他媽的媽見到女友他媽的爸, because "他媽的" is not part of the relation....

But anyway, now I throw in new relation :

And to makes things more complicated, now I put back the "xxx".


--- Quote ---mother_child(boymm,boymom).
father_child(girlmd,girlmom).

--- End quote ---

And run it:


--- Quote ---1 ?- path(boymm,girlmd,P).
mate
parent
P = [girlmd, boymom, boymm] ;

son
mate
parent
mate
parent
P = [girlmd, girlmom, boy, girldad, boymom, boymm] ;

son
son
mate
parent
mate
parent
P = [girlmd, girlmom, girl, boy, girldad, boymom, boymm] ;

son
son
parent
parent
mate
parent
P = [girlmd, girlmom, girl, boy, girldad, boymom, boymm] ;

son
son
sibling
parent
parent
mate
parent
P = [girlmd, girlmom, girl, xxx, boy, girldad, boymom, boymm] ;

son
son
son
parent
parent
mate
parent
P = [girlmd, girlmom, girl, xxx, boy, girldad, boymom, boymm] ;

son
sibling
parent
mate
parent
P = [girlmd, girlmom, boy, girldad, boymom, boymm] ;

son
parent
mate
parent
P = [girlmd, girlmom, boydad, boymom, boymm] ;

son
mate
parent
sibling
mate
parent
P = [girlmd, girlmom, boy, girldad, boydad, boymom, boymm] ;

son
son
mate
parent
sibling
mate
parent
P = [girlmd, girlmom, girl, boy, girldad, boydad, boymom, boymm] ;

son
son
parent
parent
sibling
mate
parent
P = [girlmd, girlmom, girl, boy, girldad, boydad, boymom, boymm] ;

son
son
sibling
parent
parent
sibling
mate
parent
P = [girlmd, girlmom, girl, xxx, boy, girldad, boydad, boymom, boymm] ;

son
son
son
parent
parent
sibling
mate
parent
P = [girlmd, girlmom, girl, xxx, boy, girldad, boydad, boymom, boymm] ;

son
sibling
parent
sibling
mate
parent
P = [girlmd, girlmom, boy, girldad, boydad, boymom, boymm] ;

son
mate
parent
parent
P = [girlmd, girlmom, boy, boymom, boymm] ;

son
son
mate
parent
parent
P = [girlmd, girlmom, girl, boy, boymom, boymm] ;

son
son
parent
parent
parent
P = [girlmd, girlmom, girl, boy, boymom, boymm] ;

son
son
sibling
parent
parent
parent
P = [girlmd, girlmom, girl, xxx, boy, boymom, boymm] ;

son
son
son
parent
parent
parent
P = [girlmd, girlmom, girl, xxx, boy, boymom, boymm] ;

son
sibling
parent
parent
P = [girlmd, girlmom, boy, boymom, boymm] ;

son
parent
parent
P = [girlmd, girlmom, boymom, boymm] ;

false.

--- End quote ---


kido:
I think it's just for illustration, perhaps the only human-understandable relation is the one below:


--- Quote ---1 ?- path(boymm,girlmd,P).
mate
parent
P = [girlmd, boymom, boymm] ;

--- End quote ---

Navigation

[0] Message Index

[*] Previous page

Go to full version