Baidu Test Department October 2015 of the face of the question--eight queen.
The eight Queens question is introduced here. The following is the realization of the eight Queen-n queen with recursive thought.
The code is as follows:
usingSystem;usingSystem.Collections.Generic;namespacequeenssolution{classProgram {Static intCount =0; Static voidMain (string[] args) { intn =Int32.Parse (Console.ReadLine ()); List<int> Queen =Newlist<int>(n); for(inti =1; I <= N; i++) {Queen. ADD (0); } putqueen (n, Queen,0); Console.WriteLine (count); Console.readkey (); } Private Static voidPutqueen (intN, list<int> Queen,introw) { for(Queen[row] =1; Queen[row] <= N; queen[row]++) { if(Checkqueens (Queen, Row)) { row ++; if(Row <N) {putqueen (n, queen, row); } Else{Count++; for(inti =0; I < n; i++) {Console.Write (Queen[i]. ToString ()+" "); } Console.WriteLine (); } Row --; } } } Private Static BOOLCheckqueens (list<int> Queen,introw) { for(inti =0; i < row; i++) { if(Math.Abs (Queen[i]-queen[row]) = = Math.Abs (i-row) | | queen[i] = =Queen[row]) { return false; } } return true; } }}
Explain:
1. To solve the n*n on the board on how many ways to place the Queen, the main use of two methods, put the Queen's Putqueen method, check the Queen's Checkqueens method.
2. Initializes the dynamic array in the main function, which is used to record the position of the Queen placed in each row of the N Queen (1 represents the first column in the row, and N is the nth column of the row).
3.row represents every row of the eight Queens chessboard.
4. The dynamic array is initialized in the main function, this step is necessary, otherwise the result of the operation error.
5. The variable count (number of solutions) is declared outside the main function and is static.
The 6.PutQueen method uses recursive thought-put the Queen (each column in the row to be placed), check the location of the Queen is reasonable, if reasonable to the next line, to determine whether the next line exists, if there--put the Queen (each column in the row to be placed), check the location of the Queen is reasonable, if reasonable ... ... Until the next line does not exist, each row has been placed in the Queen, then we will record the number of solutions (count++), and then print the solution.
7. After the end of the recursion, be sure to return to the previous line (row--) so that "for (queen[row] = 1; Queen[row] <= n; queen[row]++)" takes effect, and every column in each row is placed over the queen. Be sure to note that the position of the row--is placed behind the entire IF-ELSE statement block! Because the entire IF-ELSE statement block forms the judgment of the State in the recursive process, there are two states, the first state is the Queen is currently in the 2nd to N-1 line, this time if you want to return to the previous row, "row--" position can actually be written in the IF statement block "Putqueen (n, queen, row) ;" The second state is that the Queen is currently in the last row (that is, the nth row), and if you want to return to the previous line, the position of "row--" can actually be written in the Else statement block. Therefore, we moved the position of "row--" to the back of the entire IF-ELSE statement block.
Recursive solution to the eight Queens problem in C #--n Queen