This is a Eight Queen problem solver.
Well are u fond of playing chess,ever thought of placing eight queens on one board so that none is cross by other?
The program below gives you 92 solutions on a board of 64 squares,wanna try...
"/" is used instead of "<" and ">".
#include /iostream/
#include /stdlib.h/
using namespace std;
int *board;
int bsize;
bool valid( int row, int col ){
//Check the diagnols
// This checks all diagnols comming from the point the function was passed
int calcRow, calcCol;
calcRow = row;
calcCol = col;
while ( calcRow <= bsize && calcCol <= bsize ){ if( board[calcRow] == calcCol ) return false; //Found a queen
calcRow++;
calcCol++;
}
calcRow = row;
calcCol = col;
while ( calcRow >= 0 && calcCol >= 0 ){
if( board[calcRow] == calcCol ) return false; //Found a queen
calcRow--;
calcCol--;
}
calcRow = row;
calcCol = col;
while ( calcRow <= bsize && calcCol >= 0 ){
if( board[calcRow] == calcCol ) return false; //Found a queen
calcRow++;
calcCol--;
}
calcRow = row;
calcCol = col;
while ( calcRow >= 0 && calcCol <= bsize ){ if( board[calcRow] == calcCol ) return false; //Found a queen
calcRow--;
calcCol++;
}
//See if there is a queen in any of the horz or vert directions.
/*calcRow = row;
calcCol = 0;
while ( calcCol <= bsize ){ if( board[calcRow] == calcCol ) return false; //Found a queen
calcCol++;
}*/
calcRow = 0;
calcCol = col;
while ( calcRow <= bsize ){ if( board[calcRow] == calcCol ) return false; //Found a queen
calcRow++;
}
return true;
}
void print(){
// Keep track of the number of solutions
static int solcount = 1;
cout << "Solution #" << style="font-weight: bold;"> // Print Q for a queen or X for a blank spot
for( int j = 0; j <= bsize; j++)
{
for( int i = 0; i <= bsize; i++)
{
if( board[j] == i ) cout << " Q"; else cout << " X";
}
cout << 10 ="=" style="font-weight: bold;"> // Pause every 10 solutions
system("pause");
system("cls"); // Clear the console
}
}
bool move( int row )
//PRECONDITION: The board variable is competely initiazed to -1.
//POSTCONDITION: A board array that has a record of X amount of valid queen placements.
// Where X is equal to the board size.
{
// If the row is greater then bsize that means that it finished one solution
// So it will print the solution and return false to the previous call.
if( row > bsize )
{
print();
cout << style="font-weight: bold;"> // Go through each column on the board and check if a queen can be placed there.
for( int col = 0l; col <= bsize; col++ )
{
if( valid( row, col ) )
{ board[row] = col; if( move( row + 1 ) ) return true;
} } // If we could not find a valid spot, reset the board array to -1 and return false.
board[row] = -1;
return false;
}
int main(){
//Get the board size from the user
cout << "How big do you want the board to be? : "; cin >> bsize;
bsize--;
// Create new board with specified size
board = new int[bsize];
// Make sure a board was actually created and Windows didnt lie.
if( board == NULL ) {
cout << "ERROR: Memory allocation failed!"<< style="font-weight: bold;"> // Initialize entire board to -1
for( int i = 0; i <= bsize; i++ ) board[i] = -1; // Clear console
system("cls");
// Find all posible solutions for the board
move(0);
//Clean up board
if( board == NULL ) delete[] board;
return 0;
}
0 comments:
Post a Comment