mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-25 09:42:16 +08:00
148 lines
3.8 KiB
Markdown
148 lines
3.8 KiB
Markdown
<p align="center">
|
||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||
<img src="https://code-thinking-1253855093.file.myqcloud.com/pics/20210924105952.png" width="1000"/>
|
||
</a>
|
||
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
|
||
|
||
|
||
|
||
# 52. N皇后II
|
||
|
||
题目链接:https://leetcode-cn.com/problems/n-queens-ii/
|
||
|
||
n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击。
|
||
|
||
上图为 8 皇后问题的一种解法。
|
||

|
||
|
||
给定一个整数 n,返回 n 皇后不同的解决方案的数量。
|
||
|
||
示例:
|
||
|
||
输入: 4
|
||
|
||
输出: 2
|
||
|
||
解释: 4 皇后问题存在如下两个不同的解法。
|
||
|
||
解法 1
|
||
|
||
[
|
||
[".Q..",
|
||
"...Q",
|
||
"Q...",
|
||
"..Q."],
|
||
|
||
解法 2
|
||
|
||
["..Q.",
|
||
"Q...",
|
||
"...Q",
|
||
".Q.."]
|
||
]
|
||
|
||
# 思路
|
||
|
||
|
||
想看:[51.N皇后](https://mp.weixin.qq.com/s/lU_QwCMj6g60nh8m98GAWg) ,基本没有区别
|
||
|
||
# C++代码
|
||
|
||
```CPP
|
||
class Solution {
|
||
private:
|
||
int count = 0;
|
||
void backtracking(int n, int row, vector<string>& chessboard) {
|
||
if (row == n) {
|
||
count++;
|
||
return;
|
||
}
|
||
for (int col = 0; col < n; col++) {
|
||
if (isValid(row, col, chessboard, n)) {
|
||
chessboard[row][col] = 'Q'; // 放置皇后
|
||
backtracking(n, row + 1, chessboard);
|
||
chessboard[row][col] = '.'; // 回溯
|
||
}
|
||
}
|
||
}
|
||
bool isValid(int row, int col, vector<string>& chessboard, int n) {
|
||
int count = 0;
|
||
// 检查列
|
||
for (int i = 0; i < row; i++) { // 这是一个剪枝
|
||
if (chessboard[i][col] == 'Q') {
|
||
return false;
|
||
}
|
||
}
|
||
// 检查 45度角是否有皇后
|
||
for (int i = row - 1, j = col - 1; i >=0 && j >= 0; i--, j--) {
|
||
if (chessboard[i][j] == 'Q') {
|
||
return false;
|
||
}
|
||
}
|
||
// 检查 135度角是否有皇后
|
||
for(int i = row - 1, j = col + 1; i >= 0 && j < n; i--, j++) {
|
||
if (chessboard[i][j] == 'Q') {
|
||
return false;
|
||
}
|
||
}
|
||
return true;
|
||
}
|
||
|
||
public:
|
||
int totalNQueens(int n) {
|
||
std::vector<std::string> chessboard(n, std::string(n, '.'));
|
||
backtracking(n, 0, chessboard);
|
||
return count;
|
||
|
||
}
|
||
};
|
||
```
|
||
|
||
# 其他语言补充
|
||
JavaScript
|
||
```javascript
|
||
var totalNQueens = function(n) {
|
||
let count = 0;
|
||
const backtracking = (n, row, chessboard) => {
|
||
if(row === n){
|
||
count++;
|
||
return;
|
||
}
|
||
for(let col = 0; col < n; col++){
|
||
if(isValid(row, col, chessboard, n)) { // 验证合法就可以放
|
||
chessboard[row][col] = 'Q'; // 放置皇后
|
||
backtracking(n, row + 1, chessboard);
|
||
chessboard[row][col] = '.'; // 回溯
|
||
}
|
||
}
|
||
}
|
||
|
||
const isValid = (row, col, chessboard, n) => {
|
||
// 检查列
|
||
for(let i = 0; i < row; i++){ // 这是一个剪枝
|
||
if(chessboard[i][col] === 'Q'){
|
||
return false;
|
||
}
|
||
}
|
||
// 检查 45度角是否有皇后
|
||
for(let i = row - 1, j = col - 1; i >= 0 && j >= 0; i--, j--){
|
||
if(chessboard[i][j] === 'Q'){
|
||
return false;
|
||
}
|
||
}
|
||
// 检查 135度角是否有皇后
|
||
for(let i = row - 1, j = col + 1; i >= 0 && j < n; i--, j++){
|
||
if(chessboard[i][j] === 'Q'){
|
||
return false;
|
||
}
|
||
}
|
||
return true;
|
||
}
|
||
const chessboard = new Array(n).fill([]).map(() => new Array(n).fill('.'));
|
||
backtracking(n, 0, chessboard);
|
||
return count;
|
||
};
|
||
```
|
||
-----------------------
|
||
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|