Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
225 views
in Technique[技术] by (71.8m points)

Puzzle in Prolog

Anybody solve this Puzzle :

Figure out the first name, wine, entree, and price for each person using the clues given. Below are all categories and options used in this puzzle.

First names: Lynda, Nick, Robin, Virginia Wines: bordeaux, chianti, merlot, shiraz Entrees: beef stir-fry, citrus chicken, filet mignon, red snapper Prices: $24.99, $25.99, $26.99, $27.99

Clues:

  1. The diner who ordered the red snapper didn't have the bordeaux.
  2. Lynda paid less than the one who had the bordeaux.
  3. Neither the one who had the bordeaux nor the one who had the chianti was the person who paid $26.99.
  4. The diner who ordered the beef stir-fry had the chianti.
  5. The diner who ordered the citrus chicken paid 1 dollar less than the one who had the chianti.
  6. The diner who ordered the filet mignon paid less than the one who had the shiraz.
  7. Virginia was either the diner who ordered the beef stir-fry or the diner who ordered the red snapper.
  8. The one who had the merlot paid 1 dollar less than Robin.

SOURCE:

logic-puzzles.org

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Figure out the first name, wine, entree, and price for each person

so we represent each person as 4-ary compound term, p(Name,Wine,Entree,Price). There seem to be four of them, too.

Then we just write down what we are told:

wine_and_dine(People):-
  length(People,4),
  Ordered1 = p(_,W1,red_snapper,_),   
             member(Ordered1, People),
             % W1 = bordeaux, but delay writing this down 
             %                 until it is defined some more
             % or use freeze/2 in SWI:
             freeze( W1, W1 = bordeaux),
  Lynda2 = p(lynda,_,_,PL2), 
             Had2 = p(_,bordeaux,_,PB2),
             member(Lynda2, People),
             member(Had2, People),
             % PL2 < PB2,     % check this only when they are known; or
             freeze(PL2, freeze(PB2, PL2 < PB2)),
  .... etc.

do consult the Q&A on the tag.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...