反转深度优先搜索算法的输出
Reverse the output of the Depth-First-Search Algorithm
我想反转代码的输出。
goal(hall).
move(gateA,gateB).
move(gateA,gateG).
move(gateB,gateC).
move(gateB,gateH).
move(gateG,gateL).
move(gateG,gateF).
move(gateL,gateS).
move(gateC,gateD).
move(gateH,gateO).
move(gateD,gateI).
move(gateD,gateJ).
move(gateI,gateP).
move(gateP,gateQ).
move(gateJ,gateR).
move(gateR,hall).
goSolveTheMaze( Start, Goal) :-
depthfirst( [], Start, Goal).
depthfirst( Path, Start, [Start | Path] ) :-
goal( Start).
depthfirst( Path,Start, Hall) :-
move( Start, Node1),
depthfirst( [Start | Path], Node1, Hall).
输出为:
?- goSolveTheMaze(gateA,Hall).
Hall = [hall, gateR, gateJ, gateD, gateC, gateB, gateA] .
I want it to be :
Hall =[gateA ,gateB ,gateC ,gateD ,gateJ ,gateR ,hall].
I tried to use the reverse function like this :
reverse([]) --> [].
reverse([Goal|Ls]) --> reverse(Ls), [Goal].
But it didn't work.
goSolveTheMaze(Start, Path) :-
depthfirst(Start, Path).
depthfirst(Goal, [Goal]) :-
goal(Goal).
depthfirst(Start, [Start|Path]) :-
move(Start, Node1),
depthfirst(Node1, Path).
结果swi-prolog:
?- goSolveTheMaze(gateA, Path).
Path = [gateA,gateB,gateC,gateD,gateJ,gateR,hall] ;
false.
我想反转代码的输出。
goal(hall).
move(gateA,gateB).
move(gateA,gateG).
move(gateB,gateC).
move(gateB,gateH).
move(gateG,gateL).
move(gateG,gateF).
move(gateL,gateS).
move(gateC,gateD).
move(gateH,gateO).
move(gateD,gateI).
move(gateD,gateJ).
move(gateI,gateP).
move(gateP,gateQ).
move(gateJ,gateR).
move(gateR,hall).
goSolveTheMaze( Start, Goal) :-
depthfirst( [], Start, Goal).
depthfirst( Path, Start, [Start | Path] ) :-
goal( Start).
depthfirst( Path,Start, Hall) :-
move( Start, Node1),
depthfirst( [Start | Path], Node1, Hall).
输出为:
?- goSolveTheMaze(gateA,Hall).
Hall = [hall, gateR, gateJ, gateD, gateC, gateB, gateA] .
I want it to be : Hall =[gateA ,gateB ,gateC ,gateD ,gateJ ,gateR ,hall].
I tried to use the reverse function like this :
reverse([]) --> [].
reverse([Goal|Ls]) --> reverse(Ls), [Goal].
But it didn't work.
goSolveTheMaze(Start, Path) :-
depthfirst(Start, Path).
depthfirst(Goal, [Goal]) :-
goal(Goal).
depthfirst(Start, [Start|Path]) :-
move(Start, Node1),
depthfirst(Node1, Path).
结果swi-prolog:
?- goSolveTheMaze(gateA, Path).
Path = [gateA,gateB,gateC,gateD,gateJ,gateR,hall] ;
false.