gpt4 book ai didi

Prolog Logic Puzzle 不工作?

转载 作者:行者123 更新时间:2023-12-05 00:12:46 31 4
gpt4 key购买 nike

我一直在研究这个 Prolog 逻辑问题。它得到了不正确的结果,运行程序本身需要一段时间,这是我更关心的。逻辑问题如下:

Alex, Bret, Chris, Derek, Eddie, Fred, Greg, Harold, and John are nine students who live in a three story building, with three rooms on each floor. A room in the West wing, one in the centre, and one in the East wing. If you look directly at the building, the left side is West and the right side is East. Each student is assigned exactly one room. Can you find where each of their rooms is:


  • 哈罗德不住在底层。
  • Fred 就住在 John 的正上方,紧挨着 Bret(住在西翼)。
  • Eddie 住在东翼,比 Fred 高一层。
  • 德里克住在弗雷德的正上方。
  • 格雷格住在克里斯的正上方。

  • 这是我编写的代码,需要一段时间才能完成并给出不正确的结果:
    rooms([west,center,east]).
    floors([bottom,middle,top]).

    students([alex,bret,chris,derek,eddie,fred,greg,harold,john]).

    student(S) :-
    students(L), member(S,L).

    empty_building(building(floor(_,_,_),floor(_,_,_),floor(_,_,_))).

    location(P,1,1,building(floor(P,_,_),_,_)).
    location(P,1,2,building(floor(_,P,_),_,_)).
    location(P,1,3,building(floor(_,_,P),_,_)).
    location(P,2,1,building(_,floor(P,_,_),_)).
    location(P,2,2,building(_,floor(_,P,_),_)).
    location(P,2,3,building(_,floor(_,_,P),_)).
    location(P,3,1,building(_,_,floor(P,_,_))).
    location(P,3,2,building(_,_,floor(_,P,_))).
    location(P,3,3,building(_,_,floor(_,_,P))).

    puzzle_soln(BLDG) :-
    empty_building(BLDG),
    location(harold,HFN,_,BLDG),
    location(fred,FFN,FRN,BLDG),
    location(john,JFN,JRN,BLDG),
    location(bret,BFN,BRN,BLDG),
    location(eddie,EFN,ERN,BLDG),
    location(derek,DFN,DRN,BLDG),
    location(greg,GFN,GRN,BLDG),
    location(chris,CFN,CRN,BLDG),
    location(alex,_,_,BLDG),

    HFN \= 1,
    FFN is JFN + 1,
    FRN = JRN,
    1 =:= abs(FRN - BRN),
    FFN = BFN,
    BRN is 1,
    ERN is 3,
    EFN is FFN + 1,
    DFN is FFN + 1,
    DRN = FRN,
    GFN is CFN + 1,
    GRN = CRN.

    这是一个指向存在问题和解决方案的站点的链接: https://www.brainbashers.com/showpuzzles.asp?puzzle=ZQJZ

    任何帮助,将不胜感激。

    最佳答案

    你得到了正确的答案。

    ?- puzzle_soln(X).
    X = building(floor(alex, john, chris), floor(bret, fred, greg), floor(harold, derek, eddie))

    与此相同(从底部数楼层)
    West    Centre  East
    ==== ====== ====
    Harold Derek Eddie
    Bret Fred Greg
    Alex John Chris

    如果你想加快速度,你可以交错生成和测试:
    puzzle_soln(BLDG) :-
    empty_building(BLDG),
    BRN is 1,
    ERN is 3,
    location(harold,HFN,_,BLDG),
    HFN \= 1,
    location(fred,FFN,FRN,BLDG),
    location(john,JFN,JRN,BLDG),
    FFN is JFN + 1,
    EFN is FFN + 1,
    DFN is FFN + 1,
    FRN = JRN,
    location(bret,BFN,BRN,BLDG),
    1 =:= abs(FRN - BRN),
    FFN = BFN,
    location(eddie,EFN,ERN,BLDG),
    location(derek,DFN,DRN,BLDG),
    DRN = FRN,
    location(greg,GFN,GRN,BLDG),
    location(chris,CFN,CRN,BLDG),
    GFN is CFN + 1,
    GRN = CRN,
    location(alex,_,_,BLDG).

    旧版本:869,727 次推理,0.533 秒内 0.533 CPU

    新版本:310 次推理,0.000 秒内 0.000 CPU

    CLP(FD) 解决方案:61,356 次推理,0.011 秒内占用 0.011 个 CPU

    关于Prolog Logic Puzzle 不工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49870172/

    31 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com