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

old (HH)
Basic idea:

// n -- the number of non-attacked compoments already in the grid
// _ -- space
__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).
________Calls components(n + 1)
______Removes the n+1th compoment from the cell C
______(Tries the next empty cell).
2001-5-6 -05:00

回到话题: I want to write a method with the function of recursion, can anybody tell me how can I do it by useing Java or C++?

回到论坛: HOME枫下论坛枫下论坛主坛工作学习IT技术讨论