JavaScript recursive backtracking method for solving eight queens problem

Source: Internet
Author: User

JavaScript recursive backtracking method to solve eight queens problem:

On the Internet to see many articles about the eight Queens algorithm, rarely see the use of JavaScript to achieve, today we use JavaScript to solve this problem, the need for small partners can refer to.

The following is to share the retrospective method of the eight queens, with detailed annotation, here is not much nonsense.

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 5 86 87 88 89 90 91 92 93 94 95 96 97 98-99 100 function Nqueens {if (Order < 4) {Console.log (' N Queens problem apply to order bigger than 3! '); Return }   var nqueens = []; var backtracking = false; Rowloop:for (var row=0 row<order; row++) {//If row is less than 0, the problem is no solution if (Row < 0) {Console.log (' This N Queens problem ha s no solution! '); Break ///First detected a new line of if (nqueens[row] = = undefined) {Nqueens[row] = [];}//The block for (var col=0; col<order; col++) that runs when backtracking. {/ /0 for the position that has been detected and is able to place the Empress if (nqueens[row][col] = = 0) {Continue}///backtracking process, encounter the position where the empress can be placed, stating that this position in the subsequent validation did not pass, requires reprocessing else if (Backtrac King && Nqueens[row][col] = = 1 {//backtracking found that the previous line is also at the end of the line, need to continue backtracking if (col = = order-1) {Resetrow (Nqueens, order, row); row = Row-2; Continue Rowloop; //Backtracking line not yet to the end of the line, Mark 0, continue nqueens[row][col] = 0; backtracking = false; Continue //Place a queen nqueens[row][col] = 1; Find a place where you can place the Queen and jump to the next line (only one queen on a line). if (Isqueenvalid (Nqueens, Row, col)) {continue rowloop;}//Every line should have a queen, to the end of the column has not found a suitable location, indicating that the Queen placed in front of the problem, need to backtrack! else if (col = = order-1) {bAcktracking = true; 0 and 1 indicate that this position has been detected, so the bank should be cleared as undefined Resetrow (nqueens, order, row); Minus 2 is because the end of the loop has a self addition, in fact, is back to the row row = Row-2; Back to the outer loop, continue continue rowloop; else {//not to row, continue to detect nqueens[row][col] = 0; continue;}; } return nqueens; ///before backtracking, clears function Resetrow (nqueens, order, row) {for (var col=0; col<order; col++) {Nqueens[row][col] = Undefin Ed }//Detection location can place Queen 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 + +) {//column detection upper left 45 degree right 45 degree 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&lt ; 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 the entire content of this article, I hope you can enjoy.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.