#include using namespace std; static int gPrintConfiguration=0; static int CONFIGURATION=0; static char array[8][8]; const char QUEEN = '*'; const char EMPTY = '.'; void initBoard() { int i,j; for (i =0; i<8; i++) { for (j=0; j<8; j++) { array[i][j]=EMPTY; } } } void printBoard() { int i,j; for (i =7; i>-1; i--) { for (j=0; j<8; j++) { cout << array[i][j] << " "; } cout << endl; } } void MARK(int row,int col) { array[row][col]=QUEEN; } void UNMARK(int row,int col) { array[row][col]=EMPTY; } bool goodPosition(int row,int col) { // + check for (int i=0; i<8; i++) { if ((array[row][i]==QUEEN) || (array[i][col]==QUEEN)) return false; } // / check i=1; while (i) { if (((row-i)==-1) || ((col-i)==-1)) break; if (array[row-i][col-i]==QUEEN) return false; i++; } // \ check i=1; while (i) { if (((row-i)==-1) || ((col+i)==8)) break; if (array[row-i][col+i]==QUEEN) return false; i++; } return true; } void placeQueen(int row, int col) { /* Exit Conditions First */ if (col==8) return; else { /* Check this position is secure */ if (goodPosition(row,col)) { MARK(row,col); if (row==7) { CONFIGURATION++; if ( (gPrintConfiguration==CONFIGURATION) || gPrintConfiguration==0) { cout << endl; cout << "CONFIGURATION #" << CONFIGURATION << endl; printBoard(); cout<< endl; } } else { /* Go recursive */ for (int i=0; i<8; i++) placeQueen(row+1,i); } UNMARK(row,col); } } } void SolvePuzzle() { for (int i=0; i<8; i++) placeQueen(0,i); } void main() { for ( gPrintConfiguration = 1; gPrintConfiguration < 93; gPrintConfiguration++ ) { CONFIGURATION = 0; initBoard(); SolvePuzzle(); } }