title: Prolog - neighbours in matrix
body: <p>I need a query, which help me resolve following problem:</p>
  
  <p>I have list of coordinates
  <code>[(1,1),(1,2),(1,3),(2,1),(2,2),(2,3)]</code></p>
  
  <blockquote>
    <p>(1,1) (1,2) (1,3)<br/> (3,1) (3,2) (3,3)</p>
  </blockquote>
  
  <p>I want get all possibilities of neighbours configurations in matrix</p>
  
  <blockquote>
    <p>?- [((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/>?- false.</p>
  </blockquote>
  
  <p>EDIT:</p>
  
  <p>My try:</p>
  
  <pre><code>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) =&lt; 1, 
          between(1, Columns, Y1), 
          abs(Y1 - Y) =&lt; 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).
  </code></pre>
  
  <p>My second try:</p>
  
  <pre><code>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).
  </code></pre>
  
  <p>EDIT 2:</p>
  
  <p>I have a data which might look like <code>[(1,1),(1,2),(1,3),(2,1),(2,2),(2,3)]</code> <strong>(*)</strong> (list of coordinates in board) or </p>
  
  <pre><code>[((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)])]
  </code></pre>
  
  <p>(list of pairs where the first element in the pair is a coordinate and the second is list of its neighbours) <strong>(*)</strong>.</p>
  
  <p>I try explain you what I really need (look at my second try in post) : )</p>
  
  <p><a href="http://i.stack.imgur.com/IapM2.jpg" rel="nofollow">http://i.stack.imgur.com/IapM2.jpg</a></p>
  
  <p>Representation of first table: <code>[((1,1), (2,1)), ((1,2), (2,2)), ((1,3), (2,3))]</code>.<br/>
  second: <code>[((1,1), (1,2)), ((2,1), (2,2)), ((1,3), (2,3))]</code>.<br/>
  third: <code>[((1,1), (2,1)), ((1,2), (1,3)), ((2,2), (2,3))]</code>.<br/>
  fourth: <code>[((1,1), (2,2)), ((1,2), (2,1)), ((1,3), (2,3))]</code>.<br/>
  fifth: <code>[((1,1), (2,1)), ((1,2), (2,3)), ((1,3), (2,2))]</code>.<br/>
  which I can get calls list_of_neighbours(X) where X is one of (*).</p>