开发者

Advice on solving zebra type puzzle

I need your help in solving the following problem:

There're 3 girls (Ann,Susan,Alice) who need to choose what color shoes and dress to wear. There're 3 possible colors for shoes and dresses: white, blue and green.

Main conditions:

  • Ann hates white.
  • Susan wears shoes and dress of same color.
  • Alice has white shoes.
  • Alice and Ann's shoes and dress have different colors.

My code satisfies only 2 conditions; I'm kinda having hard times meeting conditions on the same color for Susan and whereas other girls need to have different color garments.

Here's what I come up with:

PREDICATES
   girl(symbol)
   shoes(symbol,symbol)
   skirt(symbol,symbol)
   hates(symbol,symbol)
   will_wear(symbol, symbol, symbol)


CLAUSES
   will_wear(X,Y,Z):-
      girl(X),
      shoes(X,Y),
      skirt(X,Z),
      not(hates(X,Y)),
      not(hates(X,Z)).

   girl(ann).
   girl(susan).
   girl(alice).

   hates(ann,white).

   skirt(_,white).
   skirt(_,blue).
   skirt(_,green).

   shoes(alice,white).
   shoes(_,blue).
   shoes(_,green).

GOAL
   will_wear(Name,Shoes,Dress).

Code above works fine, but gives too many solutions. Plus, I couldn't co开发者_StackOverflow中文版me up with any logical solution for the condition for Susan to wear shoes and dress of same color.

Thanks.


If I understand the conditions correctly, they're not what Shurane answered.

This will make sure that a girl wears a dress and shoes with the same colour:

same_color(Girl) :-
    shoes(Girl, Color),
    dress(Girl, Color).

I'll leave the different colour one as an exercise, but hint that to say two things are not the same you say A \= B. Please leave a comment if you have a hard time with different_color - and tell me what you've tried.


Off the top of my head, I'm thinking something along these lines:

only_wears(Girl,Color):-
    shoes(Girl, Color),
    skirt(Girl, Color).

different_shoes(F, S):-
    shoes(F,F_color),
    shoes(S,S_color),
    not(equals(F_color,S_color)).

different_skirts(F, S):-
    skirt(F,F_color),
    skirt(S,S_color),
    not(equals(F_color,S_color)).

I do wonder if there's a way to pass clauses to other clauses, because different_shoes and different_skirts are identical in structure.

You would initialize it like so:

only_wears(ann, white).
different_shoes(alice, ann).
different_skirt(alice, ann).
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜