The Recursive Backtracking Method of javascript solves the eight queens problem, and the javascript Recursion
Next I will share with you the solution of the eight queens by the backtracing method, with detailed annotations. There is not much nonsense here.
Function NQueens (order) {if (order <4) {console. log ('n Queens problem apply for order bigger than 3! '); Return;} var nQueens = []; var backTracking = false; rowLoop: for (var row = 0; row <order; row ++) {// if row is smaller than 0, the problem is not solved. if (row <0) {console. log ('this N Queens problem has no solution! '); Break;} // The first time a new row is detected, if (nQueens [row] === undefined) {nQueens [row] = [];} // program block for (var col = 0; col <order; col ++) {// 0 indicates the location where the queen has been detected and placed. if (nQueens [row] [col] = 0) {continue;} // during backtracking, when the location where the queen can be placed is displayed, it indicates that this location has not passed the verification. You need to re-process else if (backTracking & nQueens [row] [col] = 1) {// when backtracking, it is found that the last row ends at the end of the row. You need to continue backtracking if (col = order-1) {resetRow (nQueens, order, row ); row = row-2; continue rowLoop;} // return The traced row has not reached the end of the row. Mark it as 0. continue nQueens [row] [col] = 0; backTracking = false; continue ;} // place a queen nQueens [row] [col] = 1; // locate a location where the queen can be placed and jump out to the next line (only one queen can be placed on one line ). If (isQueenValid (nQueens, row, col) {continue rowLoop;} // each row should have a queen. No proper position is found at the end of the column, it indicates that there is a problem with the placement of the queen. You need to trace back! Else if (col = order-1) {backTracking = true; // both 0 and 1 indicate that this position has been detected. Therefore, we need to clear the row as undefined resetRow (nQueens, order, row); // minus 2 because there is an auto-increment at the end of the loop, which is actually returning to the previous row = row-2; // returns to the outer loop and continues to continue rowLoop ;} else {// The nQueens [row] [col] = 0; continue ;}}return nQueens;} // before backtracking, clear the row function resetRow (nQueens, order, row) {for (var col = 0; col <order; col ++) {nQueens [row] [col] = undefined;} // check whether the position can be placed in the Queen's function isQueenValid (nQueens, row, col) {// row detection for (var I = 0; I <col; I ++) {if (nQueens [row] [I] = 1) {return false ;}} for (var j = 1; j <row + 1; j ++) {// check the upper left 45 degrees and upper right 45 degrees if (nQueens [row-j] [col] = 1 | (nQueens [row-j] [col-j] = = 1) | (nQueens [row-j] [col + j] = 1) {return false ;}} return true ;} function printQ (queens) {for (var row = 0; row <queens. length; row ++) {var rowText = ''; for (var col = 0; col <queens. length; col ++) {if (queens [row] [col] === undefined) {queens [row] [col] = 0 ;} rowText = rowText + queens [row] [col] + '';} console. log (rowText) ;}}var queens = NQueens (8); printQ (queens );
The above is all the content of this article. I hope you will like it.