The eight Queens question, the 19th century famous mathematician Gauss in 1850 proposed: in the 8x8 lattice chess plate placed 8 queens, so that they can not attack each other, that any two queens can not be in the same row, the same column, the same slash, how many kinds of pendulum? Mr. Gauss gave the answer is "76" species, is actually 76 kinds?
The eight Queens problem is a typical application of backtracking algorithms, but this paper provides a recursive approach.
The core idea of recursion can be summed up as: to narrow a complex problem infinitely, the solution of each small problem is the same, and finally comes down to the prototype of solving each small problem. The idea of recursive programming can assume that all problems have been resolved, come to an end condition, which is very simple, and then call itself to solve the whole problem. Although the efficiency of recursion is relatively low, it can greatly reduce the degree of thinking.
The idea of recursion for the eight Queens question is to first place the first Queen; exclude the area where the first Queen can attack, place the second queen, exclude the area where the second Queen can attack, and place the third queen ... Until the eighth Queen is placed, the eighth Queen can place only one lattice in the area.
1 //Eight Queen's problem, recursive method of realization2#include <stdio.h>3 4 intCount =0;5 6 //Determine If row J column is safe, judging column, top left , upper right, bottom left, bottom right7 intSafe (intRowintJint(*chess) [8])8 {9 intI, K, flag1=0, flag2=0, flag3=0, flag4=0, flag5=0;Ten One //determine if the column direction is safe A for(i=0; i<8; i++) - { - if(* (* (chess+i) +j)! =0) the { -Flag1 =1;//Not Safe - Break; - } + } - + //determine if the upper left is safe A for(I=row, k=j; i>=0&& k>=0; I--, k--) at { - if(* (* (chess+i) +k)! =0) - { -Flag2 =1;//Not Safe - Break; - } in } - to //determine if the lower right is safe + for(I=row, k=j; i<8&& k<8; i++,k++) - { the if(* (* (chess+i) +k)! =0) * { $Flag3 =1;//Not SafePanax Notoginseng Break; - } the } + A //determine if the upper right is safe the for(I=row, k=j; i>=0&& k<8; I--, k++) + { - if(* (* (chess+i) +k)! =0) $ { $Flag4 =1;//Not Safe - Break; - } the } - Wuyi //determine if the lower left is safe the for(I=row, k=j; i<8&& k>=0; i++,k--) - { Wu if(* (* (chess+i) +k)! =0) - { AboutFlag5 =1;//Not Safe $ Break; - } - } - A if(Flag1 | | flag2 | | flag3 | | flag4 | |Flag5) + return 0; the Else - return 1; $ } the the //Row : Start line the //N: Number of columns the //(*chess) [8]: pointer to each row of the chessboard - voidEightqueen (intRowintNint(*chess) [8]) in { the intI, J, chesstemp[8][8];//used to store the current form of the board the About for(i=0; i<8; i++) the { the for(j=0; j<8; J + +) the { +CHESSTEMP[I][J] =Chess[i][j]; - } the }Bayi the if(8==row)//The recursive termination condition, which assumes that a checkerboard distribution has been found, prints it the { -printf"type%d chessboard: \ n", count+1); - for(i=0; i<8; i++) the { the for(j=0; j<8; J + +) the { theprintf"%d", * (* (chesstemp+i) +j)); - } theprintf"\ n"); the } theprintf"\ n");94count++; the } the Else //Enter recursion the {98 for(j=0; j<n; J + +)//scan columns on row row About { - if(Safe (Row, J, chess))//If the first row, column J Security101 {102 for(i=0; i<8; i++)103 {104* (* (chesstemp+row) +i) =0;//The first row is assigned 0 the }106* (* (chesstemp+row) +j) =1;//Row rows, column J, assigned 1107 108Eightqueen (row+1, N, chesstemp);109 } the }111 } the }113 the intMain () the { the intchess[8][8], I, J;117 118 for(i=0; i<8; i++)119 { - for(j=0; j<8; J + +)121 {122CHESS[I][J] =0;123 }124 } the 126Eightqueen (0,8, chess);//starting at line No. 0, there are 8 columns127printf"There are a total of%d methods:", count); - 129 return 0; the}
The eight Queens question, the recursive method realizes