当前位置:网站首页>acwing 843. n-皇后问题
acwing 843. n-皇后问题
2022-07-06 22:04:00 【_刘小雨】
n−皇后问题是指将 n 个皇后放在 n×n 的国际象棋棋盘上,使得皇后不能相互攻击到,即任意两个皇后都不能处于同一行、同一列或同一斜线上。

现在给定整数 n,请你输出所有的满足条件的棋子摆法。
输入格式
共一行,包含整数 n。
输出格式
每个解决方案占 n 行,每行输出一个长度为 n 的字符串,用来表示完整的棋盘状态。
其中 . 表示某一个位置的方格状态为空,Q 表示某一个位置的方格上摆着皇后。
每个方案输出完成后,输出一个空行。
注意:行末不能有多余空格。
输出方案的顺序任意,只要不重复且没有遗漏即可。
数据范围
1≤n≤9
输入样例:
4
输出样例:
.Q…
…Q
Q…
…Q.
…Q.
Q…
…Q
.Q…
这题也是DFS, 可以根据全排列的
解法1:按行枚举 (也是跟上一题 全排列类似的)
可以作为模版进行记忆

#include <iostream>
using namespace std;
const int N = 20;
// bool数组用来判断搜索的下一个位置是否可行
// col列,dg对角线,udg反对角线
// g[N][N]用来存路径
int n;
char g[N][N];
bool col[N], dg[N], udg[N];
void dfs(int u) {
// u == n 表示已经搜了n行,故输出这条路径
if (u == n) {
for (int i = 0; i < n; i ++ ) puts(g[i]); // 等价于cout << g[i] << endl;
puts(""); // 换行
return;
}
// 枚举u这一行,搜索合法的列, // 这里可将行看成y, 列看成x
// 这里是枚举第u行, 第i 列, 是否放皇后, u= y, i = x 后 ===》 对角线就是 u + i、u - i + n
for (int i = 0; i < n; i ++ )
// 剪枝(对于不满足要求的点,不再继续往下搜索)
if (!col[i] && !dg[u + i] && !udg[n - i + u])
{
g[u][i] = 'Q';
col[i] = dg[u + i] = udg[n - i + u] = true;
dfs(u + 1);
col[i] = dg[u + i] = udg[n - i + u] = false;
g[u][i] = '.'; // 恢复现场
}
}
int main() {
cin >> n;
for (int i = 0; i < n; i ++ )
for (int j = 0; j < n; j ++ )
g[i][j] = '.';
dfs(0);
return 0;
}
方法2: 按照每个元素进行枚举
每个位置都有2种情况,放还是不放,总共就有n2
时间复杂度: O(2的n2次方), 打不出来了,见谅
原理图: 就类似这样画下去
思路: 按照每个位置放不放的步骤一点一点进行搜索。
code:
// 不同搜索顺序 时间复杂度不同 所以搜索顺序很重要!
#include <iostream>
using namespace std;
const int N = 20;
int n;
char g[N][N];
bool row[N], col[N], dg[N], udg[N]; // 因为是一个个搜索,所以加了row
// s表示已经放上去的皇后个数
void dfs(int x, int y, int s)
{
// 处理超出边界的情况, 这个是直接一行进行放还是不放, 一行走完了,跳到下一行开始
if (y == n) y = 0, x ++ ;
if (x == n) {
// x==n说明已经枚举完n^2个位置了
if (s == n) {
// s==n说明成功放上去了n个皇后
for (int i = 0; i < n; i ++ ) puts(g[i]);
puts("");
}
return;
}
// 分支1:放皇后
if (!row[x] && !col[y] && !dg[x + y] && !udg[x - y + n]) {
g[x][y] = 'Q';
row[x] = col[y] = dg[x + y] = udg[x - y + n] = true;
dfs(x, y + 1, s + 1);
row[x] = col[y] = dg[x + y] = udg[x - y + n] = false;
g[x][y] = '.';
}
// 分支2:不放皇后
dfs(x, y + 1, s);
}
int main() {
cin >> n;
for (int i = 0; i < n; i ++ )
for (int j = 0; j < n; j ++ )
g[i][j] = '.';
dfs(0, 0, 0);
return 0;
}
边栏推荐
- Win11 control panel shortcut key win11 multiple methods to open the control panel
- [leetcode]Spiral Matrix II
- buildroot的根文件系统提示“depmod:applt not found”
- 什么是Web3
- Video fusion cloud platform easycvr video Plaza left column list style optimization
- leetcode 53. Maximum subarray maximum subarray sum (medium)
- What if win11 pictures cannot be opened? Repair method of win11 unable to open pictures
- SQL where multiple field filtering
- 案例大赏:英特尔携众多合作伙伴推动多领域AI产业创新发展
- Hardware development notes (10): basic process of hardware development, making a USB to RS232 module (9): create ch340g/max232 package library sop-16 and associate principle primitive devices
猜你喜欢

【ArcGIS教程】专题图制作-人口密度分布图——人口密度分析

Intel David tuhy: the reason for the success of Intel aoten Technology

深耕开发者生态,加速AI产业创新发展 英特尔携众多合作伙伴共聚

How do test / development programmers get promoted? From nothing, from thin to thick

The request request is encapsulated in uni app, which is easy to understand

EasyCVR集群版本添加RTSP设备提示服务器ID错误,该如何解决?

Network Security Learning - Information Collection

Have you got the same "artifact" of cross architecture development praised by various industry leaders?

Common methods of list and map

mpf2_线性规划_CAPM_sharpe_Arbitrage Pricin_Inversion Gauss Jordan_Statsmodel_Pulp_pLU_Cholesky_QR_Jacobi
随机推荐
MySQL null value processing and value replacement
SSM+JSP实现企业管理系统(OA管理系统源码+数据库+文档+PPT)
Advertising attribution: how to measure the value of buying volume?
Win11截图键无法使用怎么办?Win11截图键无法使用的解决方法
[knife-4j quickly build swagger]
leetcode 53. Maximum subarray maximum subarray sum (medium)
一图看懂!为什么学校教了你Coding但还是不会的原因...
SQL where multiple field filtering
How to solve the problem of adding RTSP device to easycvr cluster version and prompting server ID error?
Complimentary tickets quick grab | industry bigwigs talk about the quality and efficiency of software qecon conference is coming
Formation continue en robotique (automatisation) - 2022 -
一度辍学的数学差生,获得今年菲尔兹奖
科兴与香港大学临床试验中心研究团队和香港港怡医院合作,在中国香港启动奥密克戎特异性灭活疫苗加强剂临床试验
[on automation experience] the growth path of automated testing
Nanopineo use development process record
Wechat can play the trumpet. Pinduoduo was found guilty of infringement. The shipment of byte VR equipment ranks second in the world. Today, more big news is here
The root file system of buildreoot prompts "depmod:applt not found"
SSM+jsp实现仓库管理系统,界面那叫一个优雅
The request request is encapsulated in uni app, which is easy to understand
Opencv third party Library