Is this figure connected? You are given a list of (x,y) grid coordinates.  Your function should decide whether the figure obtained by coloring in the squares of the grid with the given coordinates is connected or not.

Squares adjacent diagonally are not considered connected.

Example input:

(0,0),(1,0),(2,0),(3,0),(3,1),(3,2),(2,2),
(1,2),(0,2),(0,3),(0,4),(1,4),(2,4),(3,4)


which looks like:

****
*
****
   *
****


Output:

YES


Example input:

(0,0),(1,0),(2,0),(0,1),(0,2),(2,2),(3,2),(4,2),(0,3),
(4,3),(0,4),(1,4),(2,4),(4,4),(4,5),(2,6),(3,6),(4,6)


which looks like:

  ***
    *
*** *
*   *
* ***
*
***


Output:

NO


Your function must take as input a list of pairs of integers (e.g. in C++, maybe vector&lt;pair&lt;int,int&gt;&gt;) and return a boolean.  You may not assume any particular ordering of the input points.  The coordinates will be between 0 and 99 inclusive.

Smallest code wins.
c99 -- 620ish neccessary characters

#include &lt;stdlib.h&gt;
#define S return
typedef struct n N;struct n{int x,y;N*n;};
N*I(int x,int y,N*l){if(!l)goto z;N*m=l;do{if((m-&gt;x==x)&amp;&amp;(m-&gt;y==y))
S m;m=m-&gt;n;}while(m!=l);z:S NULL;}N*A(N*n,N*l){if(!l)n-&gt;n=n,l=n;
else n-&gt;n=l-&gt;n,l-&gt;n=n;S l;}N*R(N*n,N*l){if(!l||(l==n&amp;&amp;n-&gt;n==n))goto z;
N*m=l;do{if(m-&gt;n==n){m-&gt;n=n-&gt;n;S m;}m=m-&gt;n;}while(m!=l);z:S NULL;}
int E(N*l){int i=0;if(!l)goto z;N*m=l;do{++i;m=m-&gt;n;}while(m!=l);z:S i;}
int C(N*l){N*c=NULL,*n=l,*m=l=R(n,l);c=A(n,c);int a=E(l)*E(l);do{
if(!l)S 1;n=m-&gt;n;if(I(m-&gt;x-1,m-&gt;y,c)||I(m-&gt;x+1,m-&gt;y,c)||I(m-&gt;x,m-&gt;y-1,c)
||I(m-&gt;x,m-&gt;y+1,c))l=R(m,l),c=A(m,c);m=n;}while(--a);S 0;}


More than half the bulk is taken up by a minimal implementation of a linked list---c is horribly deficient in that sense. 

The code here includes the definition of the nodes and all the list manipulation functions called by C, my connectedness checker.

The method is to maintain two lists (one of connected nodes initialized by the first node) and one of yet-to-be-connected nodes (starting with everything else). What follows is an exhaustive cyclic search to find a connection for each node in turn. There is an upper limit on the number of attempts conservatively set to length-of-the-list squared, which marks the end of attempts. Very inefficient, but functional.

It completes several simple cases of my own devising and both of the given test cases correctly.

Full program with test scaffold and some comments

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
typedef struct n N;
struct n{
  int x,y;
  N*n;
};
N*I(int x,int y,N*l){ /* Return pointer to node x,y in l or NULL */
  if(!l)goto z;
  N*m=l;
  do{
    if((m-&gt;x==x)&amp;&amp;(m-&gt;y==y)){
      return m;
    }
    m=m-&gt;n;
  }while(m!=l);
 z:return NULL;
}; 
N*A(N*n,N*l){ /* Add node n to list l. Return the new l */
  if(!l){
    n-&gt;n=n;
    l=n;
  }else{
    n-&gt;n=l-&gt;n;
    l-&gt;n=n;
  }
  return l;
};
N*R(N*n,N*l){ /* Remove node n from list l. Return the new l */
  if(!l||(l==n&amp;&amp;n-&gt;n==n))goto z;
  N*m=l;
  do{
    if(m-&gt;n==n){m-&gt;n=n-&gt;n; return m;}
    m=m-&gt;n;
  }while(m!=l);
 z:return NULL;
}
int E(N*l){ /* Return the Enumberation of nodes in list l */
  int i=0;
  if(!l)goto z;
  N*m=l;
  do{++i;m=m-&gt;n;}while(m!=l);
 z:return i;
}
int C(N*l){ /* return true if all nodes in list l are connected */
  N*c=NULL,*n=l,*m=l=R(n,l); /* put the first node in the connected
                            list, and mark the start of the
                            working list */
  c=A(n,c);
  int a=E(l)*E(l); /* maximum number of times to try to place nodes */
  do {
    if (!l) return 1; /* when l is empty: we win! */
    n=m-&gt;n; 
    if ( I(m-&gt;x-1,m-&gt;y  ,c) ||
     I(m-&gt;x+1,m-&gt;y  ,c) ||
     I(m-&gt;x  ,m-&gt;y-1,c) ||
     I(m-&gt;x  ,m-&gt;y+1,c) ) l=R(m,l), c=A(m,c);
    m=n;
  } while (--a);
  return 0;
}
void P(N*l){ /* Print the list */
  printf("List: [");
  if(!l)goto z;
  N*m=l;
  do{printf(" (%d, %d)",m-&gt;x,m-&gt;y);m=m-&gt;n;}while(m!=l);
 z:printf(" ]\n");
}
N*G(int x,int y){ /* generate a new node with (x, y) */
  N*n;
  if(n=malloc(sizeof(N)))n-&gt;x=x,n-&gt;y=y;
  return n;
}
int main(){
  N*n,*l=NULL;

  n=G(0,0); l=A(n,l);
  n=G(1,0); l=A(n,l);
  n=G(2,0); l=A(n,l);
  n=G(0,1); l=A(n,l);
  n=G(0,2); l=A(n,l);
  n=G(2,2); l=A(n,l);
  n=G(3,2); l=A(n,l);
  n=G(4,2); l=A(n,l);
  n=G(0,3); l=A(n,l);

  n=G(4,3); l=A(n,l);
  n=G(0,4); l=A(n,l);
  n=G(1,4); l=A(n,l);
  n=G(2,4); l=A(n,l);
  n=G(4,4); l=A(n,l);
  n=G(4,5); l=A(n,l);
  n=G(2,6); l=A(n,l);
  n=G(3,6); l=A(n,l);
  n=G(4,6); l=A(n,l);

  P(l);
  if (C(l)) {
    printf("Connected!\n");
  } else {
    printf("Not connected.\n");
  };
}