Expand|Select|Wrap|Line Numbers
- int a[8];
- void display ();
- int TestLegal (int currentColumn, int currentRow)
- {
- for (int column = 0; column < currentColumn; ++column) //check row conflict
- if (a[column] == a[currentColumn])
- return (0); // return FALSE if both queens are on the same row
- for (int column = 0; column < currentColumn; ++column) //check diagonal conflict
- if (abs(a[column] - column) == (abs(a[currentColumn] - currentColumn)))
- return (0); // return FALSE if both queens are on the same diagonal.
- return currentRow;
- }
- int placeQueen (int column) {
- if (column == 8) //Check if all columns have a legal value
- {
- display();
- return (-1);
- }
- for (int row = 1; row <= 8; ++row) // Try all of the legal values for the column
- {
- if (TestLegal (column, row))
- {
- a[column] = row;
- if (placeQueen (column + 1) < 0)
- return -1;
- }
- }
- return (0);
- }