假设我们有一个机器人,它在棋盘上,可以像国王一样移动。
棋盘的坐标从 [1,1] 到 [8,8]。
起始位置是 [1,1],最终位置是 [8,8]。有一个列表 X 包含障碍物坐标列表,例如 [[1,4],[2,5],[5,6]]。问题是:有没有一种可能的方式让机器人从起始位置移动到最终位置。
我做了这个谓词:
path([A,B],_):- A is 8, B is 8.
path([A,B],X):-
possibleMoves(A,B,L), % possibleMoves returns a list of all the possible coords that
the robot can go to. (Example for [1,1] are [[0,1],[0,0],[2,1]...])
member([Ex,Ey],L), % member generates all the possible members from the list L
not(member([Ex,Ey],X)), % if this member is not member of the "forbidden ones"
Ex<9,Ex>0,Ey<9,Ey>0, % and its coords are on the board
path([Ex,Ey],X).
isTherePath(X):- possibleMoves(1,1,L),
member(E,L),
path(E,X).
但是有一个错误,它没有返回任何值。递归永远不会停止我找不到原因。