1

チェス盤上にあり、キングのように動くロボットがあるとします。

ボードの座標は [1,1] から [8,8] です。

開始位置は [1,1] で、最終位置は [8,8] です。[[1,4]、[2,5]、[5,6]] などの障害物の座標のリストを含むリスト X があります。問題は、ロボットが開始位置から最終位置まで移動できる方法があるかどうかです。

私はこの述語を作りました:

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).

しかし、間違いがあり、値を返しません。再帰が止まらない理由がわかりません。

4

1 に答える 1

2

1 つの述語で、有効なステップとは何かを定義します - すべての制限を含みます。この命名規則に従ってください: X0「最初の」値、X「最後の」値

step(Forbidden,[X0,Y0],[X,Y]) :-
   possibleMoves(X0,Y0,L), % rather: possibleMove([X0,Y0],L),
   member([X,Y],L),
   between(1,8,X),
   between(1,8,Y),
   non_member([X,Y],Forbidden).

これで、次のパスが作成されました。

 ..., closure0(step(Forbidden), S0,S), ...

closure0/3サイクルを処理します。

于 2014-11-20T20:43:20.823 に答える