我在一个艰难的Prolog项目/谜题工作,但我找不到解决方案。 我会感谢任何帮助。
实践是通过迷宫来模拟和解决逻辑编程。 它包括房间,门和钥匙。当两个房间连接时,它们通过一个门连接,该门由一个或多个锁关闭,这需要打开以从一个房间移动到另一个房间。键和锁是模糊的,IE任何键都打开任何锁。打开门之后它总是打开,但是钥匙卡在锁中并且无法恢复(永远丢失),因此打开每扇门时他们将需要许多钥匙和锁。每个房间都有未使用的钥匙,可以收集这些钥匙以进一步打开新门。最初我们没有钥匙。
解决方案程序必须定义谓词camino / 3(camino是西班牙语的道路),如果A
是F
,则X
,X
,F
)为真根据停留时间通往房间A
的道路,以便您可以随时使用相应的钥匙打开门。道路应按照交叉顺序列出房间名称(从F
开始到e / 2
结束)。
该程序将为每个会议室e (E , L )
定义E
表单的谓词L
,其中p / 3
是该会议室中包含的密钥数。您还为每个门定义了p (E1, E2 , C)
形式的谓词C
,其中E1
是具有连接E2
和%rooms+ number of keys
e(a,1).
e(b,2).
e(c,1).
e(d,0).
e(e,0).
e(f,0).
%Doors
p(a,b,1).
p(a,c,1).
p(b,d,1).
p(c,e,1).
p(d,f,1).
p(e,f,2).
个房间的门的锁的数量。该程序应保持房间的状态(如果他们有钥匙或没有)和门(如果已经打开或静止
在任何时候都被锁定。
一个例子是(Source):
?‐ camino(a,f,X).
X = [a,b,a,c,d,f] ?
yes
?‐ camino(a,f,X).
X = [a,c,d,f] ?
yes
结果应该是:
%CODE
% these are the rooms and the number of keys they contain
e(a,1).
e(b,2).
e(c,1).
e(d,0).
e(e,0).
e(f,0).
%these are the doors and the number of keys needed to open them
p(a,b,1).
p(a,c,1).
p(b,d,1).
p(c,e,1).
p(d,f,1).
p(e,f,2).
concatenate([],L,L).
concatenate([X|M],L,[X|Y]) :-
concatenate(M,L,Y).
%%%%%%%%%%%%%
camino(A,F,X):-
A==F, %check if we start at the destiny
concatenate([],[F],X).
%%%%%%%%%%%%%%%%%%%%%%%%%%
camino(A,F,X):-
A\==F, %check if we dont start at the destiny
concatenate([],[A],R),
findRoad(A,F,0,R,X).
%%%%%%%%%%%%%%%%%%
%TRUE if x is a road (list) that leads to room F starting from A
%
findRoad(A,F,K,R,X):- %k is key --- initial keys
addkey(A,K,L), %L new key--- number of keys after we add the keys of the room
pickkey(A), %we put the number of keys of the room to 0
passDoor(A,L,P,_), %P is position-- position of the new room
opendoor(A,P), %we put the number of keys needed to pass the door to 0
P == F, %we check if we have finished
concatenate(R,[P],X). %we concatenate the destiny and end
findRoad(A,F,K,R,X):- %k is key --- initial keys
addkey(A,K,L), %L new key--- number of keys after we add the keys of the room
pickkey(A), %we put the number of keys of the room to 0
passDoor(A,L,P,L2), %P is position-- position of the new room
opendoor(A,P), %we put the number of keys needed to pass the door to 0
P \== F, %we check we haven't finished
concatenate(R,[P],R2),%we concatenate the path we have for the moment
findRoad(P,F,L2,R2,X).
addkey(A,K,L):-
e(A,N),
L is K+N.
passDoor(A,L,P,L2):-
p(A,P,W),
L2 is L-W,
L2 >= 0.
passDoor(A,L,P,L2):-
p(P,A,W),
L2 is L-W,
L2 >= 0.
pickkey(A):-
e(A,_) = e(A,0).
opendoor(A,P):-
p(A,P,_) = p(A,P,0).
opendoor(A,P):-
p(P,A,_) = p(P,A,0).
这是我目前的代码。它找到了通往命运的道路,但这不正确。此外,我仍然没有应用剪辑,所以它只给我一个答案:
Notification.requestPermission(function (permission) {
// If the user accepts, let's create a notification
if (permission === "granted") {
//redirect using javascript here window.location
}
});
答案 0 :(得分:0)
这是解决它的另一种尝试,但它会陷入无限循环
%房间+钥匙数量 E(A,1)。
E(B,2)。
E(C,1)。
E(d,0)。
E(即,0)。
E(女,0)。
%门
P(A,B,1)。
P(A,C,1)。
P(B,d,2)。
P(C,E,1)。
P(d,F,1)。
P(E,F,2)。
%实习课
串连([],L,L)。
连接([X | M],L,[X | Y]): -
concatenate(M,L,Y).
路径(A,F,X): -
A ==楼
串连([],[F],X)。
路径(A,F,X): -
A \ ==楼
串连([],[A],R),
探路者(A,F,0,R,[],[] ,的,X)。
%如果x是从A
开始通向房间F的道路(列表),则为TRUE探路者(A,F,K,R,ROOM,DOOR,ROOM2,DOOR3,X): - %k是关键---初始键
addkey(A,K,L,ROOM,ROOM2), %L new key--- number of keys after we add the keys of the room
passDoor(A,L,P,_,DOOR,DOOR3), %P is position-- position of the new room
P == F, %we check if we have finished
concatenate([P],[R],X). %we concatenate the destiny and end
探路者(A,F,K,R,ROOM,DOOR,ROOM2,DOOR3,X): - %k是关键---初始键
addkey(A,K,L,ROOM,ROOM2), %L new key--- number of keys after we add the keys of the room
passDoor(A,L,P,L2,DOOR,DOOR3),%P=new room L2 = new Nº of keys
P \== F, %we check we havent finished
concatenate([R],[P],R2), %we add the room we are to the path
pathfinder(P,F,L2,R2,ROOM2,DOOR3,_,_,X). %
addkey(A,K,L,间,房间): -
member(A,ROOM), %checks if we hace visited the room
L is K. % If so [ROOM] and Nº of keys wont change
addkey(A,K,L,ROOM,ROOM2): -
not(member(A,ROOM)), %checks we havnt visited the room
e(A,N), %we save the number of keys of the room in N
L is K+N, %L is (Nº of previous keys)+ keys of the room
concatenate([ROOM],[A],ROOM2). %we add A to the list of visited rooms
passDoor(A,L,P,L2,DOOR,DOOR): -
member(p(A,P,_),DOOR), %checks if you have passed that door
L2 is L. %If you have allready passed it, the Nº of keys dont change
passDoor(A,L,P,L2,门,DOOR3): -
not(member(p(A,P,_),DOOR)), %checks that you havent passed that door
p(A,P,W), %checks the door exists
L2 is L-W, %we substract to our keys the Nº of keys needed
L2 >= 0, %checks the result is > 0
concatenate([DOOR],[p(A,P,_)], DOOR2), %we add the door to the list of used doors
concatenate([DOOR2],[p(P,A,_)], DOOR3). % we add both ways in case we need to go back
答案 1 :(得分:0)
一种方法,它不提供最佳路径(在房间之间的最小步骤数的意义上),但满足请求如下。主要规则是:
path( Orig, Dest, Res ) :-
e( Orig, Keys ),
nextDoor( Dest, [(Orig,Orig)], Keys, [_|OpenDoors] ), !,
joinDoors( Orig, OpenDoors, [Orig], Res ), !.
这意味着,在使用初始房间的键初始化键数后,算法将决定门必须打开的顺序(nextDoor),第二个将在前一个列表中找到一扇门到隔壁的路径。
理念是:在给定的时刻,我们有一组敞开的门,以及通过这些敞开的门连接的一组房间。穿过开放式门和访问室的区域是免费的,门已经打开,访问过的房间还没有钥匙。因此,我们只关心决定哪一个是我们必须打开的隔壁。决定此门打开顺序的规则是:
nextDoor( Dest, OpenDoors, _, Res ) :-
visitedRoom( Dest, OpenDoors ),
!,
reverse( OpenDoors, Res ).
nextDoor( Dest, OpenDoors, Keys, Res ) :-
/* choice a visited room */
visitedRoom( Room, OpenDoors ),
/* next door not yet open */
door( Room, NextRoom, DoorCost ),
\+ member( (Room,NextRoom), OpenDoors ),
\+ member( (NextRoom,Room), OpenDoors ),
/* we can open door ? */
DoorCost =< Keys,
/* do not open doors to rooms already visited */
\+ visitedRoom( NextRoom, OpenDoors ),
/* ok, cross door and next one */
e( NextRoom, RoomAward ),
Keys2 is Keys-DoorCost+RoomAward,
nextDoor( Dest, [(Room,NextRoom)|OpenDoors], Keys2, Res ).
使用两个实用程序,一个用于提供双向路径:
door(From,To,Cost) :-
( p(From,To,Cost); p(To,From,Cost) ).
另一个找到已经访问过的房间(通过敞开的门连接的房间)的房间:
visitedRoom( Room, OpenDoors ) :-
( member( (_,Room), OpenDoors ); member( (Room,_), OpenDoors ) ).
第二步是按照上一个订单从一扇门进入隔壁。
joinDoors( _, [], Path, Res ) :-
reverse( Path, Res ).
joinDoors( CurrentRoom, [ (RoomBeforeDoor, RoomAfterRoom ) | Q ], Path, Res ) :-
roomToRoom( CurrentRoom, RoomBeforeDoor, [], Path2 ),
append( Path2, Path, Path3 ),
joinDoors( RoomAfterRoom, Q, [RoomAfterRoom|Path3], Res ).
其中roomToRoom是一种用于查找路径的分类算法(TODO:optimize以查找最短路径):
roomToRoom( DestRoom, DestRoom, Path, Path ) :- !.
roomToRoom( CurrentRoom, DestRoom, Path, Res ) :-
door( CurrentRoom, NextRoom, _ ),
\+ member( NextRoom, Path ),
roomToRoom( DestRoom, NextRoom, [NextRoom|Path], Res ).
如果我们尝试使用示例中提供的数据:
e(a,1).
e(b,2).
e(c,1).
e(d,0).
e(e,0).
e(f,0).
p(a,b,1).
p(a,c,1).
p(b,d,2). /* corrected */
p(c,d,1). /* add */
p(c,e,1).
p(d,f,1).
p(e,f,2).
结果是:
?- path(a,f,Path).
Path = [a, b, a, c, d, f].
答案 2 :(得分:0)
为了确保获得最短路径,您可以使用BFS搜索:
:- use_module(library(lambda)).
%rooms + number of keys
e(a,1).
e(b,2).
e(c,1).
e(d,0).
e(e,0).
e(f,0).
%Doors
p(a,b,1).
p(a,c,1).
p(b,d,2).
p(c,d,1).
p(c,e,1).
p(d,f,1).
p(e,f,2).
% Doors are closed at the beginning of the puzzle
% state(CurrentRoom, NumberKeys, StateRooms, StateDoors)
init(state(a, 0, LstRooms, LstDoors)) :-
setof(e(X,Y), X^Y^e(X,Y), LstRooms),
setof(p(X,Y,Z, closed), X^Y^Z^p(X,Y,Z), LstDoors).
% final state
final(state(f, _, _, _)).
% skeleton of BFS search
:- dynamic(states/1).
puzzle :-
retractall(states(_)),
% set the initial state
init(State),
assert(states([[State]])),
repeat,
nextstates,
% if we get to the final state,
% eneded/1 succeeds with a path
ended(Path),
maplist(writeln, Path).
% test if we have finished the puzzle
% succeeds with a Path to the solution
% This BFS gives a reverse path to the solution
ended(Path) :-
final(State),
states(LstStates),
% may be there is no solution ?
( LstStates = []
-> Path = []
; include(=([State|_]), LstStates, Paths),
Paths = [RPath|_],
reverse(RPath, Path)).
nextstates :-
retract(states(LstStates)),
foldl(\States^Y^Z^(nextstates_one(States, NewStates),
append(Y, NewStates, Z)),
LstStates, [], LstNewStates),
assert(states(LstNewStates)).
% First we search the rooms near the current room
% Next we build the new paths
nextstates_one([Head | Tail], NewStates) :-
nextrooms(Head, LState),
foldl([Head, Tail] +\X^Y^Z^(member(X, Tail)
-> Z = Y
; append([[X, Head | Tail]], Y, Z)),
LState, [], NewStates),
% we must put a cut here,
% if **ended(Path)** fails, we must continue at **repeat**
!.
% fetch all the rooms near the current room
nextrooms(state(R, K, SR, SD), States) :-
% we fetch keys (even when there is no more keys left !)
select(e(R, Key), SR, TSR),
NK is K + Key,
sort([e(R, 0) | TSR], NSR),
% we test all the doors
foldl([R,NK,NSR,SD]+\X^Y^Z^(X = p(R1, R2, Keys, Open),
% can we go to the next door ?
( select(R, [R1,R2], [NR])
-> ( Open = opened
% the door is opened, we came in without changint anything
-> Z = [state(NR, NK, NSR, SD) | Y]
% the door is closed, have we enough keys ?
; ( Keys =< NK
-> NK1 is NK - Keys,
select(p(R1, R2, Keys, Open), SD, TSD),
sort([p(R1, R2, 0, opened) | TSD], NSD),
Z = [state(NR, NK1, NSR, NSD) | Y]
; Z = Y))
; Z = Y)),
SD, [], States).