How do you put 8 queens on a chessboard so that they won't capture each other?

Printable View

- 04-20-2003FearOfTheDarkCheckmate!
How do you put 8 queens on a chessboard so that they won't capture each other?

- 04-20-2003Magos
Being on a programming forum and all, you could write a program that finds the answer for you...

Or look below: - 04-20-2003minesweeper
Won't the top and bottom-most queens capture each other?

- 04-20-2003golfinguy4
Put on 8 kings so they're busy?

Put 4 queens on opposite sides of the board and offset them 1 square to the side. At least using the already posted board, that works. - 04-20-2003BMJ
I don't think it's possible.

- 04-20-2003minesweeper
Golfinguy,

Do you mean having 4 queens on the same vertical line? Because that clearly won't work. - 04-20-2003XSquared
This one actually works. (There are 92 working solutions)

- 04-20-2003vasanth
You could create a program to find the solution using backtracking.... Similar to solving a maze problem.. I have to use something like this when i start coding my chess games single player part..

- 04-20-2003MagosQuote:

*Originally posted by minesweeper*

**Won't the top and bottom-most queens capture each other?**

And I thought I checked em all.... :( - 04-20-2003golfinguy4Quote:

*Originally posted by minesweeper*

**Golfinguy,**

Do you mean having 4 queens on the same vertical line? Because that clearly won't work.

- 04-20-2003XSquared
And if you want how I calculated it, here ya go.

**Warning**

This code is uncommented, and basically contest-style code.

Code:`#pragma warning( disable: 4530 )`

#include <fstream>

using namespace std;

#define MAXSIZE 13

bool col[ MAXSIZE ];

bool updiag[ MAXSIZE * 2 ];

bool downdiag[ MAXSIZE * 2 ];

ifstream in("checker.in");

ofstream out("checker.out");

int n = 0;

int numoutputed = 0;

void outputsoln( int *row )

{

for( int i = 0; i < n; i++ ) {

if( i != 0 ) out<<" ";

out<<row[i] + 1;

}

out<<endl;

}

int calcnum( int i, int *row )

{

int sum = 0;

if( i == n ) {

outputsoln( row );

return 1;

}

for( int j = 0; j < n; j++ ) {

if( !col[ j ] && !updiag[ i + j ] && !downdiag[ i - j + n ] ) {

col[ j ] = true;

updiag[ i + j ] = true;

downdiag[ i - j + n ] = true;

row[i] = j;

sum += calcnum( i + 1, row );

col[ j ] = false;

updiag[ i + j ] = false;

downdiag[ i - j + n ] = false;

}

}

return sum;

}

int main( void )

{

int rows[ MAXSIZE ];

in>>n;

out<<calcnum( 0, rows )<<endl;

return 0;

}