Velocity Reviews > Eight queen problem

# Eight queen problem

ravi
Guest
Posts: n/a

 07-21-2007
C/C++ program to find a possible solution of eight queen problem

Can any one help me to get the solution ?

santosh
Guest
Posts: n/a

 07-21-2007
On Jul 21, 11:27 am, ravi <(E-Mail Removed)> wrote:
> C/C++ program to find a possible solution of eight queen problem
>
> Can any one help me to get the solution ?

<http://en.wikipedia.org/wiki/Eight_queens_puzzle>
<http://mathworld.wolfram.com/QueensProblem.html>
<http://en.literateprograms.org/Eight_queens_puzzle_(C)>

this group, comp.programming, comp.algorithms or a web search should
reveal lots of ideas.

pete
Guest
Posts: n/a

 07-21-2007
ravi wrote:
>
> C/C++ program to find a possible solution of eight queen problem
>
> Can any one help me to get the solution ?

/* BEGIN Q8.c */

#include <stdio.h>
#include <assert.h>

#define SQUARES 8

void column(int *row, int c, int squares);
int ok(int *row, int c, int r);
void solution(int *row, int squares);

int main(void)
{
int row[SQUARES + 1];

assert(sizeof row / sizeof *row - 1 != 0);
row[0] = 0;
column(row, 1, sizeof row / sizeof *row - 1);
return 0;
}

void column(int *row, int c, int squares)
{
int r = squares;

do {
if (ok(row, c, r)) {
row[c] = r;
if (c == squares) {
solution(row, squares);
break;
}
column(row, c + 1, squares);
}
} while (--r != 0);
}

int ok(int *row, int c, int r)
{
const int p = c + r, m = c - r;

do {
--c;
} while (c && r != row[c] && p != c+row[c] && m != c-row[c]);
return c == 0;
}

void solution(int *row, int squares)
{
int c = 1;

++row[0];
printf("\n\n\nSOLUTION # %d\n\n", row[0]);
do {
printf("COLUMN %d, ROW %d\n", c, row[c]);
} while (c++ != squares);
}

/* END Q8.c */

--
pete

Charles Richmond
Guest
Posts: n/a

 07-22-2007
ravi wrote:
> C/C++ program to find a possible solution of eight queen problem
>
> Can any one help me to get the solution ?
>

Try drawing a "space filling" curve, or a Koch snowflake...
much more fun.

--
+----------------------------------------------------------------+
| Charles and Francis Richmond richmond at plano dot net |
+----------------------------------------------------------------+

user923005
Guest
Posts: n/a

 07-24-2007
On Jul 21, 6:56 am, pete <(E-Mail Removed)> wrote:
> ravi wrote:
>
> > C/C++ program to find a possible solution of eight queen problem

>
> > Can any one help me to get the solution ?

>
> /* BEGIN Q8.c */
>
> #include <stdio.h>
> #include <assert.h>
>
> #define SQUARES 8
>
> void column(int *row, int c, int squares);
> int ok(int *row, int c, int r);
> void solution(int *row, int squares);
>
> int main(void)
> {
> int row[SQUARES + 1];
>
> assert(sizeof row / sizeof *row - 1 != 0);
> row[0] = 0;
> column(row, 1, sizeof row / sizeof *row - 1);
> return 0;
>
> }
>
> void column(int *row, int c, int squares)
> {
> int r = squares;
>
> do {
> if (ok(row, c, r)) {
> row[c] = r;
> if (c == squares) {
> solution(row, squares);
> break;
> }
> column(row, c + 1, squares);
> }
> } while (--r != 0);
>
> }
>
> int ok(int *row, int c, int r)
> {
> const int p = c + r, m = c - r;
>
> do {
> --c;
> } while (c && r != row[c] && p != c+row[c] && m != c-row[c]);
> return c == 0;
>
> }
>
> void solution(int *row, int squares)
> {
> int c = 1;
>
> ++row[0];
> printf("\n\n\nSOLUTION # %d\n\n", row[0]);
> do {
> printf("COLUMN %d, ROW %d\n", c, row[c]);
> } while (c++ != squares);
>
> }
>
> /* END Q8.c */
>
> --
> pete

/* short version: */
#include<stdio.h>
static int v,i,j,k,l,s,a[99];int main(void){for(s=8;*a-s;v=a[j*=v]-
a[i],
k=i<s,j+=(v=j<s&&(!k&&!!printf(2+"\n\n%c"-(!l<<!j)," #Q"[l^v?
(l^j)&1:2])
&&++l||a[i]<s&&v&&v-i+j&&v+i-j))&&!(l%=s),v||(i==j?a[i+=k]=0:+
+a[i])>=s*
k&&++a[--i]);printf("\n\n");return 0;}