This is a creation in Article, where the information may have evolved or changed.
The problem of solving eight queens by backtracking (recursive and non-recursive, go language implementation)
Problem Recurrence:
Put eight queens on the 8x8 chess, so that they can not attack each other, that is, any two queens can not be in the same row, the same column or the same slash, ask how many kinds of pendulum? If the problem extends to n*n, how many kinds of pendulum are there?
Analysis:
Solving 8*8 problems by backtracking
Checkerboard Horizontal I, ordinate j,7>=i>=0 and 7>=j>=0, queen pendulum position (I,J)
Use Pos[i] to record the position of the first Queen
Analysis: 0 means no, 1 indicates already
Use A[i] to indicate whether the first row is placed Queen (a value in 0~7)
Use b[j] to indicate whether the column J is pendulum Queen (b value in 0~7)
Use C[j-i+7] Diagonal whether the Queen is placed (c value in 0~14)
With D[i+j] diagonal diagonal whether the Queen (d value in 0~14)
Optimization:
A[i] This condition of judgment can be omitted, because when the Queen is taken, the Queen cannot be placed on the same line, so it can be assumed that the Empress I is placed in column J, where I of the Empress I is the row.
The first queen in column J for (j=0;j<8;j++) {if (i>7) {count++ Printing the current solution set}if ((i,j) position corresponds to ABCD are represented as 0) {set corresponding ABCD for 1 recursive call next set corresponding ABCD is 0}}
Non-recursive version 1
Package Mainimport ("FMT") var pos, b [80]intvar c, D [150]int/* corresponding array for N{0|1}*/func putn (i, J, flag int) {B[J], c[j-i+7], d[ I+J] = flag, flag, flag}/**pos[i]=j indicates that the queen of line I in column J *b[i] Indicates whether the Queen is placed on line I, and that only one queen may appear in each line, and there must be only one queen in each row. *c[j-i+7] Indicates whether the main diagonal is placed Queen *d[i+j] indicates whether the second diagonal is placed Queen *0 means no, 1 means to place the Queen * check if the corresponding position can be placed Queen, conflict when returned 1 */func checkpos (i, j int) int {if b[j] = = 1 | | c[j -I+7] = = 1 | | D[I+J] = = 1 {return 1}return 0}/** backtracking non-recursive notation: Eight queens Problem author: Tian */func backtrackqueen (n int) int {//passed in n value is the original value (n-1) Count: = 0// Pendulum I: = 0//How many queens have been placed, that is, how many lines have been disposed conflict: = 0//conflict, default 0 no conflict for {if conflict = = 0 {//There is no conflict if i = = n {//Search to a solution count+ +//start backtracking, restart for pos[i] = = n {//special note 1:i=7,pos[7]=n, i=7 no need to release (see note 2), and as long as the i=7 will execute this sentence//if the upside-down position, will make the original position of I=6 is not released, therefore must first i--, Release I--/*if i = =-1 {//End condition return count}*///free space so that position can be put putn (I, Pos[i], 0)}pos[i]++} else {//else If I < n, because I is step up, so Els E if i<n can not//new mark, note not put//special point 2:i=7 did not execute this statement, because in i=7, as long as the right position to find, and then back to Putn (I, pos[i], 1)//into the next row No. 0 column Search i++ Pos[i] = 0//until i=7, known heuristics}} else {//Discovery conflict, and search from 0Depth to nfor pos[i] = = n {//Special Note 3: Here and above there is still a difference, because line I is still in conflict, so must first i--, then release, i--if i = = 1 {//real end exit, and is the inevitable return count}// Free up space so that the position can be placed putn (I, Pos[i], 0)}pos[i]++}conflict = Checkpos (i, pos[i])}//return Count}func Main () {FMT. Println (Backtrackqueen (8-1))}
Non-recursive version 2
Package Mainimport ("FMT") var col [9]int//col[j]=i, which indicates that row I of column J placed the Queen var row [9]int//row[i]=0 indicates that line I does not have queen Var b [17]int//0 indicates that the main pair The corner line does not put the Queen var C [17]int//0 indicates that the second diagonal does not put Queen/* in the specified position set new flag */func putn (num, n, flag int) {Row[col[num]] = Flagb[num+col[num]] = FLAGC [N+num-col[num]] = flag}/* Check position is available, can return 0*/func checkpos (num, n int) int {if row[col[num]] = = 1 | | b[num+col[num] [= 1 | | C[n+num-col[num] = = 1 {return 1}return 0}/** backtracking non-recursive notation: Eight queens Problem author: Tian */func backtrackqueen (n int) int {count: = 0//Statistical pendulum//Initial Num: = 1//The number of Queens already placed flag: = 0//0 indicates no conflict col[1] = 1//1th column selected 1th position col[0] = 0//No. 0 column data without for num > 0 {if flag = = 0 {//No conflict if num = = n {count++//Conflict, start fallback for col[num] = = n {//num minus 0 is also meant to end the status token before num--//purge, release position putn (num, n, 0)}col[num]++} E LSE {//New status Mark, indicating no re-putn (num, n, 1)//From the next column in the first place to continue searching num++col[num] = 1}} else {//This column conflicts and found N, start fallback for col[num] = = N {num- -PUTN (num, n, 0)}//continues to search this column for the next col[num]++}flag = Checkpos (num, n)}return Count}func main () {FMT. Println (Backtrackqueen (8))}
recursive version
Package Mainimport ("FMT")//initial value is 0, for ease of expansion, the size of the space multiplied by the 10//pos[i]=j for the first row of the Queen placed in J position//with the a[i] to indicate whether the first row of the Queen (a value in the 0~7)//with C[j-i+7 ] Whether the diagonal is placed Queen (c value in 0~14)//with d[i+j] diagonal diagonal whether the Queen (d value in 0~14) var pos, b [80]intvar c, D [150]int/* the corresponding array for N{0|1}*/func putn (i, J, n int) {pos[i], b[j], c[j-i+7], d[i+j] = j, N, N, n}/* check if the corresponding position can be placed Queen */func Checkpos (i, j int) int {if b[j] = = 1 | | c[j-i+7 ] = = 1 | | D[I+J] = = 1 {return 0}return 1}/* print the position of the Queen */func Printqueen (n int) {for i: = 0; i < n; i++ {for J: = 0; j < N; Pos[i] = = J {fmt. Print (1)} else {fmt. Print (0)}}fmt. Println ()}fmt. Println ("==================")}/** the Most concise backtracking method: The Eight Queens problem recursive core Author: */func Queen (i, n int, Count *int) {if I > 7 {//description 8 Huang After has been put well, that is to get understanding *count++printqueen (N)//Avoid useless search, should immediately return return}for J: = 0; J < N; J + + {if Checkpos (i, j) = = 1 {putn (i, J, 1) Queen (I+1, N, Count) putn (i, J, 0)}}}func main () {n, Count: = 8, 0queen (0, N, &am P;count) fmt. Println (Count)}