Friday, July 07, 2006

8 Queens Problem

/**********************************************************************************
A program to generate all possible positions of keeping maximum queens on a n*n chess board with neither of them attacking each other.
***********************************************************************************/
void generateCases(int row)
{
int col = 0;
BOOLEAN retVal = FALSE;
if(row greaterThanEqualTo SIZE)
{
return;
}
for(col = 0 ; col lessThan SIZE; col++)
{
/**************************************
This function will check if the new
queen(row,col) can be placed in the
board. Returns true if we can.
***************************************/
retVal = validatePosition(row,col,QUEEN);
if(retVal == TRUE)
{
piecePositions[size_g].row = row;
piecePositions[size_g].col = col;
size_g++;
if(row == SIZE-1)
{
printBoard();/*Prints the board*/
count_g++;
}
generateCases(row+1);
size_g--;
}
}
}

main()
{
int r = 0;
INIT_GLOBALS();
size_g=0;
memset(piecePositions,0,sizeof(piecePositions));
generateCases(0);
}

No comments: