我有一个问题:
如何为迷宫执行查找谓词。 给出一个实验室:
[[[down,left,up],[left,down,up],[right,up],[up],[,up],[right,left,up]],
[[left,down],[down,up],[down,up],[],[down],[right,down]],
[[left,up],[down,up],[down,up],[down],[down,up],[right,down,up]],
[[right,left],[left,up],[up],[up],[up],[right,up]],
[[left,down],[right,down],[left,down],[down],[down],[right,down]]]
我需要定义一种在Labryinth数据结构中查找单元格的方法(Andrew Rice sugestion)
lookup(Lab,(X,Y),Walls)
我已经有了第n个:
nth(1,[H|T],H).
nth(N,[_|T],R) :- M is N-1, nth(M,T,R).
现在我的查询:
lookup(Lab, (X, Y), Walls) :-
nth(Y, Lab, R),
nth(X, Lab, Y1),
这是对的吗?
EDIT !!!
我的代码:
nth(1,[H|_],H).
nth(N,[_|_],R) :-
M is N-1,
nth(M,_,R).
lookup(Lab, (X, Y), Walls) :-
Y1 is nth(Y, Lab, _),
Y == Y1,
X1 is nth(X, Lab, Y1),
X1 == Walls.
calc(up,(X,Y1),(X,Y2)) :-
Y2 is Y1-1.
calc(down,(X,Y1),(X,Y2)) :-
Y2 is Y1+1.
calc(left,(X1,Y),(X2,Y)) :-
X2 is X1-1.
calc(right,(X1,Y),(X2,Y)) :-
X2 is X1+1.
map_calc(_,[],[]).
map_calc((X,Y),[H|T],[(H,X1,Y1)|S]) :-
calc(H,(X,Y),(X1,Y1)),
map_calc((X,Y),T,S).
我的possible_moves:
movs_possiveis(Lab, (X,Y), Movs, Poss) :-
lookup(Lab, (X,Y), Walls),
\+memberchk(Movs),
map_calc((X,Y), Walls, Poss).
这是对的吗?
由于