我如何将序言深度优先搜索转换为A *

时间:2019-12-17 11:00:35

标签: search prolog depth-first-search path-finding

我有序言深度优先搜索功能,但我需要将其转换为A *搜索迷宫寻路。我该怎么办?

move(  p( _, _ ), up    ).
move(  p( _, _ ), down  ).
move(  p( _, _ ), left  ).
move(  p( _, _ ), right ).

% UP
update(  p(X, Y), up, p(X_new, Y)  ) :-
    X_new is X - 1.

% DOWN
update(  p(X,Y), down, p(X_new, Y) ) :-
    X_new is X + 1.

% LEFT
update(  p(X,Y), left, p(X, Y_new)  ) :-
    Y_new is Y - 1.

% RIGHT
update(  p(X,Y), right, p(X, Y_new)  ) :-
    Y_new is Y + 1.



/***************************************************************************/
/* Implementation of the predicate that checks whether a state is legal    */
/* according to the constraints imposed by the problem's statement.        */
/***************************************************************************/
/*
 * The location cannot be out of the maze, so we define lower limits for both
 * coordinates
 *
 * Also we have to check whether the (X,Y) coordinates represents a
 * wall or an empty space. We only can do the move if the cell is empty.
 * As we just assert the wall cells, we use the negation as failure.
 */
legal(  p(X,Y) ) :-
    X >= 0,
    Y >= 0,
    \+ c(X,Y,wall).



/***************************************************************************/
/* A reusable depth-first problem solving framework.                       */
/***************************************************************************/

solve_dfs(Problem, State, _, []) :-
    final_state(Problem, State).

solve_dfs(Problem, State, History, [Move|Moves]) :-
    move(State, Move),
    update(State, Move, NewState),
    legal(NewState),
    \+ member(NewState, History),
    %print(NewState),
    solve_dfs(Problem, NewState, [NewState|History], Moves).

所以上面的resolve_dfs需要使用A *算法而不是DFS,您知道如何实现吗?

0 个答案:

没有答案