473,385 Members | 1,445 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Eight queen problem

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

Can any one help me to get the solution ?

Jul 21 '07 #1
4 9316
On Jul 21, 11:27 am, ravi <dceravigu...@gmail.comwrote:
C/C++ program to find a possible solution of eight queen problem

Can any one help me to get the solution ?
Look up these links:

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

Your linked-list problems are general algorithmic ones. A search of
this group, comp.programming, comp.algorithms or a web search should
reveal lots of ideas.

Jul 21 '07 #2
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
Jul 21 '07 #3
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 |
+----------------------------------------------------------------+
Jul 22 '07 #4
On Jul 21, 6:56 am, pete <pfil...@mindspring.comwrote:
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;}

Jul 24 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

33
by: Geoff Berrow | last post by:
I may have mentioned that I run an Introduction to PHP course at a local college (very basic - I'm no PHP expert). Well, one of my students was doing really well so I set him some extension work. ...
3
by: cfanatic | last post by:
Hey all, I been reading through these forums for a long time but have never posted. Well, I got a dillema. I have a program of the Eight Queen's program and I have to make it work without...
5
by: cj | last post by:
Hello, I am also looking for information how to write code which finds solutions to 8-queen problem (position 8 queens on a chess board, or N queens on a N*N board, so that they do not hit each...
9
by: jblazi | last post by:
In the book "Algortims and Data Structures" by Wirth there is a program in Pascal to compute all 92 solutions of the above mentioned problem. I tried to translate his program into C++, the only...
5
by: Matt | last post by:
I will be grateful if someone explians this part colfree = FALSE; upfree = FALSE; downfree = FALSE; of the code below. I don't understand how this marks the downward and upward diagonals....
1
by: Mustafab | last post by:
helloo... can anyone help me by sending me z source code for solving the 8 queen program using c language... i have 2 find just one solution for the n queen problem... here is my email <removed per...
4
by: Idea | last post by:
hi guys m new out here and need help with an assignment due next week.The assignmnet is some what related to eight queens problem but not fully, you can say the basic is taken from that problem....
1
by: Mothra | last post by:
.... is that it does not return the length of the bit set. For example, if I create a new 8-bit set: BitSet eightBits = new BitSet(8); Running eightBits.length() returns zero, rather than...
3
by: vozzek | last post by:
Hi all, I'm a CSS rookie, but I'm stumped. Hopefully this is something simple that I'm overlooking. My master/detail page generates the following html code, and I've got it formatted with CSS...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.