简体   繁体   English

序言:解决难题

[英]Prolog: Solving a Puzzle

I am new to prolog and I am trying to solve the following question. 我是prolog的新手,正在尝试解决以下问题。 I am having difficulty trying to understand the logic to solve the problem. 我很难理解解决问题的逻辑。 I know its similar to zebra problem but, I am unsure how to approach. 我知道它类似于斑马问题,但是我不确定该如何处理。 Any help would be greatly appreciated. 任何帮助将不胜感激。

The answers submitted by five students to a T/F quiz are as follows. 由五名学生提交给T / F测验的答案如下。

Teresa: T T F T F
Tim:    F T T T F
Tania:  T F T T F
Tom:    F T T F T
Tony:   T F T F T
  1. Tania got more answers right than Teresa did. 塔妮娅比特蕾莎修女得到的答案更多。
  2. Tom got more right than Tim. 汤姆比蒂姆有权利。
  3. Tony did not get all the answers right, nor did he get them all wrong. 托尼没有正确地回答所有问题,也没有错误地回答所有问题。

Write a Prolog program quiz(Answer) that asserts Answer is the list of t and f constants that is the correct answer to the quiz.. 编写一个Prolog程序quiz(Answer) ,断言Answer是tf常数的列表,这是对测验的正确答案。

If you use SWI-Prolog, you can use library clpfd to solve the puzzle :, I get only one solution (f,f,t,f,t). 如果您使用SWI-Prolog,则可以使用库clpfd来解决这个难题:,我只有一个解决方案(f,f,t,f,t)。

You have a solution [A,B,C,D,E]. 您有解决方案[A,B,C,D,E]。 You initialize the possibles solutions with 您可以使用初始化可能的解决方案

[A,B,C,D,E] ins 0..1,

You reify all the answers for teresa for example 例如,您对特蕾莎修整的所有答案

teresea([1,1,0,1,0]).
A #= 1 #<==> TA
B #= 1 #<==> TB
.....

you compute the sum of Tis 您计算Tis的总和

sum([TA, TB, ...], #= , Steresa),

and later you will have for Tania got more answers right than Teresa did. 后来您将为Tania获得比Teresa正确的答案。

Stania #> Steresa

You get the solution with 您得到的解决方案

label([A,B,C,D,E]).

Hope this helps 希望这可以帮助

small puzzles like this can be solved by generate-and-test 这样的小难题可以通过生成和测试来解决

solve(L) :-
    % generator
    length(L, 5), maplist(tf, L),

    % Tania got more answers right than Teresa did.
    matches(L, tania, Tania),
    matches(L, teresa, Teresa), Tania > Teresa,
...

tf(t).
tf(f).

teresa(t, t, f, t, f).
tim(f, t, t, t, f).
...

Of course, matches(L, tania, Tania) counts correct Tania' answers. 当然, matches(L, tania, Tania)正确的Tania答案。

But, I don't find a solution. 但是,我找不到解决方案。 The only tuple that 'get thru' Tony, it's its exact result. 唯一通过“托尼”的元组,这就是它的确切结果。 So, this condition 因此,这种情况

Tony did not get all the answers right 托尼没有正确回答所有问题

cannot be solved... 无法解决...

edit I had a bug in matches/3. 编辑我在matchs / 3中有一个错误。 Of course there is a solution. 当然有解决方案。

edit well, the CLP(FD) version can be very compact, while being more general... 编辑得很好,CLP(FD)版本可以非常紧凑,同时更通用。

teresa(t, t, f, t, f).
...

matches(L, P, N) :-
    call(P, A,B,C,D,E),
    foldl(eqsum, [A,B,C,D,E], L, 0, N).
eqsum(t,Ls,Acc,N) :-    N #= Acc + (° #<==> Ls #= 1).
eqsum(f,Ls,Acc,N) :-    N #= Acc + (° #<==> Ls #= 0).

solve(L) :-
    length(°L, 5) ins 0..1,
    % Tania got more answers right than Teresa did.
    matches(L, tania, °) #> matches(L, teresa, °),
    % Tom got more right than Tim.
    matches(L, tom, °) #> matches(L, tim, °),
    % Tony did not get all the answers right, nor did he get them all wrong.
    matches(L, tony, °Tony) #> 0, Tony #< 5.

I used my lifter here. 我在这里用过我的升降器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM