-1

这是我的作业线索:

四名司机:Norma、Andy、Edward 和 Olivia 被控四项交通违法行为:闯停车标志、超速、非法左转和闯红灯。使用线索找出每个嫌疑人违反了哪条法律。

线索

  1. 诺玛没有闯红灯或停车标志。
  2. 安迪从未因超速而被拦下。
  3. 爱德华非法左转。
  4. 奥利维亚总是小心翼翼地在停车标志前停下来。

我需要使用prolog来解决它。

到目前为止,我已经尝试了以下代码:

violatated(Violation):-
member(Violation,[speeding,leftturn,stop,red]).

solve(S):-
S=[[Name1,Violation1],
   [Name2,Violation2],
   [Name3,Violation3],
   [Name4,Violation4]],

    %five drivers: Norma, Andy, Edward, Olivia
Name1 = norma,
Name2 = andy,
Name3 = edward,
Name4 = olivia,

violated(Violation1), violated(Violation2), violated(Violation3), violated(Violation4),
Violation1 \== Violation2, Violation1 \== Violation3, Violation1 \== Violation4,
Violation2 \== Violation1, Violation2 \== Violation3, Violation2 \== Violation4,
Violation3 \== Violation1, Violation3 \== Violation2, Violation3 \== Violation4,
Violation4 \== Violation1, Violation4 \== Violation2, Violation4 \== Violation3,

%Edward made an illegal left turn
Violation3 == leftturn,

%Norma did not run either a red light or a stop sign
Violation1 \== red,
Violation1 \== stop,

%Andy was never stopped for speeding.
Violation2 \== speeding,

%Olivia was always careful to stop at a stop sign
Violation4 \== stop.

对不起,我是prolog的新手!

4

1 回答 1

0

我已经简化了你的代码:

solve(S):-
    S=[[norma,Violation1],
       [andy,Violation2],
       [edward,Violation3],
       [olivia,Violation4]],

    permutation([speeding,leftturn,stop,red], [Violation1,Violation2,Violation3,Violation4]),

    %Edward made an illegal left turn
    Violation3 = leftturn,

    %Norma did not run either a red light or a stop sign
    Violation1 \= red,
    Violation1 \= stop,

    %Andy was never stopped for speeding.
    Violation2 \= speeding,

    %Olivia was always careful to stop at a stop sign
    Violation4 \= stop.

接着就,随即:

?- solve(S).
S = [[norma, speeding], [andy, stop], [edward, leftturn], [olivia, red]] ;
false.

但是您的代码似乎还可以,只是要更正一个错字:

violated(Violation):-
    member(Violation,[speeding,leftturn,stop,red]).

代替violatated(Violation):-

于 2013-04-03T16:28:00.347 回答