R
ravi
C/C++ program to find a possible solution of eight queen problem
Can any one help me to get the solution ?
Can any one help me to get the solution ?
C/C++ program to find a possible solution of eight queen problem
Can any one help me to get the solution ?
ravi said:C/C++ program to find a possible solution of eight queen problem
Can any one help me to get the solution ?
ravi said:C/C++ program to find a possible solution of eight queen problem
Can any one help me to get the solution ?
/* short version: */ravi said:C/C++ program to find a possible solution of eight queen problemCan 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 */
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.