| 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 that runs during backtracking For (var col = 0; col <order; col ++ ){ // 0 indicates the location where the queen has been detected and can be placed If (nQueens [row] [col] = 0 ){ Continue; } // In the Backtracking process, if the location can be placed in the Queen's position, it indicates that this location is not verified and needs to be re-processed. Else if (backTracking & nQueens [row] [col] = 1 ){ // You can find that the last row ends at the end of the row, and you need to continue backtracking. If (col = order-1 ){ ResetRow (nQueens, order, row ); Row = row-2; Continue rowLoop; } // The row to be traced has not reached the end of the row. Mark 0 and 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 row ). If (isQueenValid (nQueens, row, col )){ Continue rowLoop; } // There should be a queen in each row. No proper position has been found at the end of the column, which indicates that there is a problem with the placement of the Queen in front. 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 this line as undefined. ResetRow (nQueens, order, row ); // Subtract 2 because there is an auto-increment at the end of the loop, which is actually returned to the previous row. Row = row-2; // Return to the outer loop and continue Continue rowLoop; } Else { // If the row is not found, continue to detect the row that has not been detected NQueens [row] [col] = 0; Continue; }; } } Return nQueens; } // Clear the row before backtracking Function resetRow (nQueens, order, row ){ For (var col = 0; col <order; col ++ ){ NQueens [row] [col] = undefined; } } // Check whether the location can be placed as 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: Top left 45 degrees, top 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 ); |