topic
* n The queen problem studies how to place n queens on an n × n chessboard and make the queens unable to attack each other.
Give you an integer n, and return the number of different solutions to the n queen problem. *
Queen's rules
The Queen's way of walking is: you can walk straight and diagonally, and there is no limit to the number of grids. Therefore, requiring queens not to attack each other is equivalent to requiring no two queens to be in the same row, column, and diagonal line.
Example
Example 1:
输入:n = 4
输出:2
Explanation: As shown in the figure above, there are two different solutions to the 4-queen problem.
Example 2:
输入:n = 1
输出:1
hint:
1 <= n <= 9
Ideas
- Define the test function for judging the current position. The constraint conditions include: cannot be in the same row, cannot be in the same column, and cannot be in the same diagonal (45 degrees and 135 degrees)
- Define the chessboard; standard backtracking processing;
The specific method of using backtracking is to place a queen in each row in turn. Each time the newly placed queen cannot attack the placed queen, that is, the newly placed queen cannot be in the same column as any placed queen. The same diagonal line. When NNN queens are placed, a possible solution is found, and the number of possible solutions is added to 111.
Problem-solving code
var totalNQueens = function (n) {
let count = 0; //皇后可放置的总数
let isValid = (row, col, board, n) => {
//所在行不用判断,每次都会下移一行
//判断同一列的数据是否包含
for (let i = 0; i < row; i++) {
if (board[i][col] === 'Q') {
return false
}
}
//判断45度对角线是否包含
for (let i = row - 1, j = col + 1; i >= 0 && j < n; i--, j++) {
if (board[i][j] === 'Q') {
return false
}
}
//判断135度对角线是否包含
for (let i = row - 1, j = col - 1; i >= 0 && j >= 0; j--, i--) {
if (board[i][j] === 'Q') {
return false
}
}
return true
}
let backTracing = (row, board) => {
//走到最后一行,统计次数
if (row === n) {
count++;
return
}
for (let x = 0; x < n; x++) {
//判断该位置是否可以放置 皇后
if (isValid(row, x, board, n)) {
board[row][x] = 'Q'; //放置皇后
backTracing(row + 1, board); //递归
board[row][x] = '.'; //回溯,撤销处理结果
}
}
}
backTracing(0, board)
let board = [...Array(n)].map(v => v = ([...Array(n)]).fill('.')) //棋盘
return count
};
Summarize
The backtracking algorithm is mainly used; and solving a backtracking problem is actually a traversal process of a decision tree.
let backtracking=(路径,选择列表) =>{
if (满足结束条件)) {
存放路径;
return;
}
for (选择:路径,选择列表) {
做出选择;
backtracking(路径,选择列表); // 递归
回溯,撤销处理结果
}
}
which is:
- 1. Path: The choice that has been made.
- 2. Selection list: that is, the choices you can make currently.
- 3. End condition: that is, the condition that reaches the bottom of the decision tree and can no longer make a choice.
Pruning function
- 1. Prune the subtrees of unobtainable feasible solutions with constraints
- 2. Use the objective function to cut the subtree of the optimal solution that cannot be obtained
The general steps of the backtracking method:
- 1. Set up the initialization scheme (assign initial values to variables, read in known data, etc.)
- 2. Change the way to test, if all the tests are finished turning sideways (7)
- 3. Judge whether this method is successful (through the constraint function), if it is unsuccessful, go to (2)
- 4. If the temptation is successful, go one step further and try again
- 5. If the correct solution is still not found, then go to (2)
- 6. To find a solution, record and print
- 7. Go back one step (backtracking), if not back to the end, go to (2)
- 8. If it has been retracted to the end, it will end or print without solution
Work hard every day, keep on working hard!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。