help me with the program

S

sahu

N-Queens :
You are given a N x N chessboard & you are required to place N queens
on it so that they can't attack each-other. You just need to find out
the valid positions of the queens and the total number of such valid
combinations. A sample I/p and o/p is given below (where N will be
input by the user).

Sample I/p :
Enter the no. of queens: 4
Sample o/p:
1,3,0,2
2,0,3,1
Total no. of possible combinations = 2

Explanation: In the aforesaid example 4 is the value of N input by the
user(4 x 4 chessboard & 4 queens). The o/p prints the position no.s of
the four queens on the chessboard on screen (position no.s can have
value from 0 to N-1, refer the figure below).

0 1 2 3
- Q - -
- - - Q
Q - - -
- - Q -

The second line shows next such valid combination. Finally it prints
the total no. of such valid combinations on screen.
 
B

Barry Schwarz

N-Queens :
snip

Your third posting in five minutes with absolutely no lines of code
whatsoever. You must have confused this with the
do.my.homework.for.me group.


<<Remove the del for email>>
 
W

Wolfgang Riedel

sahu said:
N-Queens :
You are given a N x N chessboard & you are required to place N queens
on it so that they can't attack each-other. You just need to find out
the valid positions of the queens and the total number of such valid
combinations.
<snip>

start with the easy case n == 2
 
W

Wolfgang Riedel

Michael said:
The *really* easy case is n == 0, and the solution for that one also
works for any multiple of 0.

if n == 0 and n == 2 have the same number of solutions, it should be save to
assume
this holds for all (at least even) values of n.
Of course N-Janus is another story...
 
L

Lawrence Kirby

if n == 0 and n == 2 have the same number of solutions, it should be save to
assume
this holds for all (at least even) values of n.

Not safe and in fact wrong.

Lawrence
 
E

Eric Sosman

Wolfgang said:
if n == 0 and n == 2 have the same number of solutions, it should be save to
assume
this holds for all (at least even) values of n.
Of course N-Janus is another story...

It seems to me that the case n==0 has one solution,
n==1 also has one, and n==2 has none. Fitting these three
observations with a quadratic gives this general formula
for the number of solutions as a function of n:

S(n) = 1 + n/2 - n*n/2

This useful formula is a great time-saver. For example,
we need not write a computer program to discover that the
number of solutions on the standard n==8 chessboard is
minus twenty-seven.
 
W

Wolfgang Riedel

Eric said:
It seems to me that the case n==0 has one solution,
n==1 also has one, and n==2 has none. Fitting these three
observations with a quadratic gives this general formula
for the number of solutions as a function of n:

S(n) = 1 + n/2 - n*n/2

This useful formula is a great time-saver. For example,
we need not write a computer program to discover that the
number of solutions on the standard n==8 chessboard is
minus twenty-seven.

while I still think, you can place nothing on no chessboard, even no no queen -
for n == 3, there are as well 0 solutions.
So the formula should be:

S(n) = (n**4 - 13n**3 + 46n*2 -48n) / -14

and we see one of those 'off by one' features
(they just slip by and are so easy to miss,
not that I want to introduce TDD into this serious debate):

-27 is true for n == 9
for n == 8, there is simply no solution

Wolfgang
 

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
474,169
Messages
2,570,919
Members
47,459
Latest member
Vida00R129

Latest Threads

Top