1

我需要一个查询,它可以帮助我解决以下问题:

我有坐标列表 [(1,1),(1,2),(1,3),(2,1),(2,2),(2,3)]

(1,1) (1,2) (1,3)
(3,1) (3,2) (3,3)

我想在矩阵中获得所有邻居配置的可能性

?- [((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,2)), ((1,3), (2,1)), ((1,3), (2,3))];
?- [((1,1), (2,1)), ((1,2), (1,3)), ((2,2), (2,3))];
?- [((1,1), (2,1)), ((1,2), (2,3)), ((1,3), (2,2))];
?- 错误的。

编辑:

我的尝试:

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 是 (*) 之一。

4

1 回答 1

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), (..., ...)|...])].
于 2013-05-04T07:03:58.043 回答