/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
 * eight_queens.c
 * Original program by Michael Becker, packaged by DMGualtieri,
 * April, 2011 (<gualtieri**AT**ieee.org>)
 * 
 * Eight_queens is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * eight_queens is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 /* Note - Original program by Michael Becker, "The 8 Queens Puzzle," 
 April, 2003; see http://www.ijon.de/comp/programme/damen/damen_en.html */

#include <stdio.h>
#include <stdlib.h>

int x[8]; /* x[i] = x coordinate of queen in row i. */

/* prints field */
void print ()
{
  int i,j;
  printf ("+----------------+\n");
  for (i=0; i<8; i++) {
    printf ("|");
    for (j=0; j<8; j++)
      if (j==x[i]) printf ("<>");
      else printf ("  ");
    printf ("|\n");
  }
  printf ("+----------------+\n\n");
}

/* tests, whether (ix, iy) is beaten by queens 0...(iy-1) */
int is_free (int ix, int iy)
{
  int i;
  for (i=0; i<iy; i++)
    if ((x[i]==ix) || (abs(x[i]-ix)==abs(i-iy))) return 0;
  return 1;
}

/* tries to place queen n on row n */
void try (int n)
{
  int i;
  if (n==8) print();
  else
    for (i=0; i<8; i++)
      if (is_free(i,n)) {
        x[n]=i;
        try (n+1);
      }
}

int main ()
{
  try (0);
  return 0;
}