我需要一个查询,帮助我解决以下问题:
我有坐标列表
[(1,1),(1,2),(1,3),(2,1),(2,2),(2,3)]
(p)(1,1)(1,2)(1,3)
(3,1)(3,2)(3,3)
我希望在矩阵中获得邻居配置的所有可能性
? - [((1,1),(2,1)),((1,2),(2,2)),((1,3),(2,3))]; < br - >? - [((1,1),(1,2)),((2,1),(2,2)),((1,3),(2,3))]; < br - >? - [((1,1),(2,2)),((1,3),(2,1)),((1,3),(2,3))]; < br - >? - [((1,1),(2,1)),((1,2),(1,3)),((2,2),(2,3))]; < br - >? - [((1,1),(2,1)),((1,2),(2,3)),((1,3),(2,2))]; < br />? - 假。
编辑:
我的尝试:
sublist([], _).
sublist([X|XS], [X|XSS]) :-
sublist(XS, XSS).
sublist([X|XS], [_|XSS]) :-
sublist([X|XS], XSS).
neighbour((X, Y), Rows, Columns, (X1, Y1)):-
between(1, Rows, X1),
abs(X1 - X) =< 1,
between(1, Columns, Y1),
abs(Y1 - Y) =< 1,
\+ (X, Y) = (X1, Y1).
get_configuration([], _, _, _, []).
get_configuration([Head|Tail], Used, Rows, Columns, [(Head, Neighbour)|Tail2]) :-
neighbour(Head, Rows, Columns, Neighbour),
\+ member(Neighbour, Used),
append(Used, [Neighbour], Used2),
get_configuration(Tail, Used2, Rows, Columns, Tail2).
gen(CountOfCoords, Coords, Rows, Columns, Result) :-
CountOfPairs is CountOfCoords / 2,
length(List, CountOfPairs),
sublist(List, Coords),
get_configuration(List, List, Rows, Columns, Result).
% ?- gen(6, [(1,1),(1,2),(1,3),(2,1),(2,2),(2,3)], 2, 3, X).
我的第二次尝试:
ngbs(X) :- X = [((1,1),[(1,2),(2,1),(2,2)]),
((1,2),[(1,1),(1,3),(2,1),(2,2),(2,3)]),
((1,3),[(1,2),(2,2),(2,3)]),
((2,2),[(1,1),(1,2),(1,3),(2,1),(2,3)]),
((2,1),[(1,1),(1,2),(2,2)]),
((2,3),[(1,2),(1,3),(2,2)])].
generate(_, [], _, []).
generate([(Coord, Ngbs)|Tail], [_, _|Tail2], Used, [(Coord, Ngb)|Tail3]) :-
\+ member(Coord, Used),
nth1(_, Ngbs, Ngb),
\+ member(Ngb, Used),
append(Used, [Coord, Ngb], Used2),
generate(Tail, Tail2, Used2, Tail3).
generate(List, Result) :-
generate(List, List, [], Result).
% ngbs(X),generate(X, Y).
编辑2:
我的数据可能看起来像[(1,1),(1,2),(1,3),(2,1),(2,2),(2,3)]
(*)(船上坐标列表)或
[((1,1),[(1,2),(2,1),(2,2)]),
((1,2),[(1,1),(1,3),(2,1),(2,2),(2,3)]),
((1,3),[(1,2),(2,2),(2,3)]),
((2,2),[(1,1),(1,2),(1,3),(2,1),(2,3)]),
((2,1),[(1,1),(1,2),(2,2)]),
((2,3),[(1,2),(1,3),(2,2)])]
(对中的第一个元素是坐标,第二个是其邻居列表的对的列表)(*)。
我尝试向你解释我真正需要的东西(看看我在帖子中的第二次尝试):)
http://i.stack.imgur.com/IapM2.jpg
第一张桌子的代表:[((1,1), (2,1)), ((1,2), (2,2)), ((1,3), (2,3))]
。
第二:[((1,1), (1,2)), ((2,1), (2,2)), ((1,3), (2,3))]
。
第三:[((1,1), (2,1)), ((1,2), (1,3)), ((2,2), (2,3))]
。
第四:[((1,1), (2,2)), ((1,2), (2,1)), ((1,3), (2,3))]
。
第五名:[((1,1), (2,1)), ((1,2), (2,3)), ((1,3), (2,2))]
。
我可以调用list_of_neighbours(X),其中X是(*)之一。
答案 0 :(得分:0)
我希望我理解这些要求。这个片段
neighbours(Cs, NBs) :-
findall((C,NBsC),
(select(C, Cs, Rs),
findall(NB,
(member(NB, Rs),
neighbour(C, NB)
), NBsC)
), NBs).
neighbour((A,B),(C,D)) :- once(
A =:= C + 1 ;
A =:= C - 1 ;
B =:= D + 1 ;
B =:= D - 1 ).
产量
?- neighbours([(1,1),(1,2),(1,3),(2,1),(2,2),(2,3)], NBs),maplist(writeln,NBs).
(1,1),[ (1,2), (2,1), (2,2), (2,3)]
(1,2),[ (1,1), (1,3), (2,1), (2,2), (2,3)]
(1,3),[ (1,2), (2,1), (2,2), (2,3)]
(2,1),[ (1,1), (1,2), (1,3), (2,2)]
(2,2),[ (1,1), (1,2), (1,3), (2,1), (2,3)]
(2,3),[ (1,1), (1,2), (1,3), (2,2)]
NBs = [ ((1, 1), [ (1, 2), (2, 1), (2, 2), (2, 3)]), ((1, 2), [ (1, 1), (1, 3), (2, 1), (2, 2), (2, 3)]), ((1, 3), [ (1, 2), (2, 1), (2, 2), (2, 3)]), ((2, 1), [ (1, 1), (1, 2), (1, 3), (..., ...)]), ((2, 2), [ (1, 1), (1, 2), (..., ...)|...]), ((2, 3), [ (1, 1), (..., ...)|...])].