Prolog Insult/Seating problem
This is the problem. "Mr. and Mrs. Astor, Mr. and Mrs. Blake, Mr. and Mrs. Crane, and Mrs. and Mrs. Davis were seated around a circular table. Mrs. Astor was insulted by Mr. Blake, who sat next to her on her left. Mr. Blake was insulted by Mrs. Crane, who sat opposite him accross the center of the table. Mrs. Crane was insulted by the hostess, Mrs. Davis. The hostess was the only person to sit between each of a married couple. The hostess was the only person to sit between each of a married couple. The hostess was insulted by the only person to sit between two men. Who insulted the hostess? Also print the seating order s开发者_开发百科tarting with the hostess."
So what I tried was
insult(A, B). /*A insults B*/
seats([seat(_,1),seat(_,2),seat(_,3),seat(_,4),
seat(_,5),seat(_,6),seat(_,7),seat(_,8)]). /*Clockwise*/
table(Seats,Who) :- seats(Seats),
member(seat(mrastor,MA),Seats),
member(seat(mrsastor,MRSA),Seats),
member(seat(mrblake,MB),Seats), insult(MB,MRSA), MB=:=MRSA+1,
member(seat(mrsblake,MRSB),Seats),
member(seat(mrcrane,MC),Seats), insult(MRSC,MB),
member(seat(mrscrane,MRSC),Seats), opposite(MRSC, MB),
member(seat(mrdavis,MD),Seats),
member(seat(mrsdavis,MRSD),Seats), insult(MRSD,MRSC),
(between_men(MA, Who, MB); between_men(MA, Who, MC);
between_men(MA, Who, MD); between_men(MB, Who, MC);
between_men(MB, Who, MD); between_men(MC, Who, MD)),
print_seat(Seats), print_who(Who, mrsdavis). /*insult(Who, mrsdavis).*/
print_seat([A | B]) :- write(A), nl, print_seat(B).
print_seat([]).
print_who(Who, what) :- insult(Who, mrsdavis), write(Who).
member(X, [X | _]).
member(X, [_ | Y]) :- member(X, Y).
adjacent(X, Y) :- X =:= Y+1.
adjacent(X, Y) :- X =:= Y-1.
opposite(X, Y) :- X =:= Y+4.
opposite(X, Y) :- X =:= Y-4.
between_men(X, Y, Z) :- X=Y+1, Y=Z+1.
between_men(X, Y, Z) :- X=Y-1, Y=Z-1.
I'm totally new in Prolog and this problem I'm not sure if I'm coding in the right way. Any suggestions?
I really don't understand what the phrase "The hostess was the only person to sit between each of a married couple." Is Mr. Davis also a Hostess? And the hostess other than the two couple that sit next to the hostess, all the other couples sit next to their partners? Kind of confusing...
You can find similar problems and solutions in the "examples" folder of the current Logtalk distribution. You can also browse the source code on-line here:
https://github.com/LogtalkDotOrg/logtalk3/tree/master/examples/puzzles
See, for example, the "houses.lgt" (a classical) or the "camp_swampy.lgt" source files.
精彩评论