×

Loading...

That's the recursive solution of the famous problem of eight queens.

Basic idea:

//
// n -- the number of non-attacked compoments already in the grid
// _ -- space
//
components(n)
{
__while (There is an empty cell (say C) in the grid where
_________the n+1th compoment may (or may not) be put
_________in a non-attacked manner) {
____Selects the cell C and tests if it is non-attacked.
____if (The cell C is non-attacked) {
______Puts the n+1th component into the cell C.
______if (n + 1 == 8 (the maximum number of components))
________Output the results (Gets one of the solutions).
______else
________Calls components(n + 1)
______Removes the n+1th compoment from the cell C
______(Tries the next empty cell).
____}
__}
__return
}
Report