Eight queen problem

R

ravi

C/C++ program to find a possible solution of eight queen problem

Can any one help me to get the solution ?
 
P

pete

ravi said:
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 */
 
C

Charles Richmond

ravi said:
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. ;-)
 
U

user923005

ravi said:
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 */
/* 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,
k=i<s,j+=(v=j<s&&(!k&&!!printf(2+"\n\n%c"-(!l<<!j)," #Q"[l^v?
(l^j)&1:2])
&&++l||a<s&&v&&v-i+j&&v+i-j))&&!(l%=s),v||(i==j?a[i+=k]=0:+
+a)>=s*
k&&++a[--i]);printf("\n\n");return 0;}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top