ex5/2.2: use sort instead of manual recursion
This commit is contained in:
@@ -49,15 +49,6 @@ plan(Cabin1, Cabin2, Visited, Path, TotalDistance) :-
|
||||
|
||||
bestplan(Cabin1, Cabin2, ShortestPath, ShortestDistance) :-
|
||||
findall([Path, Distance], (plan(Cabin1, Cabin2, Path, Distance)), Solutions),
|
||||
shortestpath(Solutions, [ShortestPath, ShortestDistance]).
|
||||
|
||||
% takes a list of [Path Distance] pairs
|
||||
shortestpath([[Path, Distance]|[]], Solution) :- Solution = [Path, Distance].
|
||||
shortestpath([[Path, Distance]|Tail], [ShortestPath, ShortestDistance]) :-
|
||||
shortestpath(Tail, [TailPath, TailDistance]),
|
||||
(Distance < TailDistance ->
|
||||
(ShortestPath = Path, ShortestDistance = Distance)
|
||||
;
|
||||
(ShortestPath = TailPath, ShortestDistance = TailDistance)
|
||||
).
|
||||
sort(2, @=<, Solutions, [ShortestSolution|_]),
|
||||
[ShortestPath, ShortestDistance] = ShortestSolution.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user