当前位置:网站首页>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;
}
边栏推荐
- 未婚夫捐5亿美元给女PI,让她不用申请项目,招150位科学家,安心做科研!
- [team learning] [phase 34] Baidu PaddlePaddle AI talent Creation Camp
- Kivy tutorial of setting the size and background of the form (tutorial includes source code)
- 一图看懂!为什么学校教了你Coding但还是不会的原因...
- mpf2_线性规划_CAPM_sharpe_Arbitrage Pricin_Inversion Gauss Jordan_Statsmodel_Pulp_pLU_Cholesky_QR_Jacobi
- [ArcGIS tutorial] thematic map production - population density distribution map - population density analysis
- 高薪程序员&面试题精讲系列120之Redis集群原理你熟悉吗?如何保证Redis的高可用(上)?
- SSM+jsp实现仓库管理系统,界面那叫一个优雅
- Unit test asp Net MVC 4 Application - unit testing asp Net MVC 4 apps thoroughly
- Complimentary tickets quick grab | industry bigwigs talk about the quality and efficiency of software qecon conference is coming
猜你喜欢
Analysis on urban transportation ideas of 2022 Zhongqing cup C
機器人(自動化)課程的持續學習-2022-
Mongo shell, the most complete mongodb in history
[ArcGIS tutorial] thematic map production - population density distribution map - population density analysis
深耕开发者生态,加速AI产业创新发展 英特尔携众多合作伙伴共聚
NFT meta universe chain diversified ecosystem development case
Digital chemical plants realize the coexistence of advantages of high quality, low cost and fast efficiency
Mathematical analysis_ Notes_ Chapter 10: integral with parameters
见到小叶栀子
See Gardenia minor
随机推荐
NanopiNEO使用开发过程记录
Why does WordPress open so slowly?
The worse the AI performance, the higher the bonus? Doctor of New York University offered a reward for the task of making the big model perform poorly
軟件測試之網站測試如何進行?測試小攻略走起!
广告归因:买量如何做价值衡量?
Zero knowledge private application platform aleo (1) what is aleo
Unit test asp Net MVC 4 Application - unit testing asp Net MVC 4 apps thoroughly
程序员上班摸鱼,这么玩才高端!
日常工作中程序员最讨厌哪些工作事项?
[written to the person who first published the paper] common problems in writing comprehensive scientific and Technological Papers
Win11控制面板快捷键 Win11打开控制面板的多种方法
2022 electrician cup a question high proportion wind power system energy storage operation and configuration analysis ideas
MySQL null value processing and value replacement
5年自动化测试,终于进字节跳动了,年薪30w其实也并非触不可及
SSM+jsp实现仓库管理系统,界面那叫一个优雅
ACL2022 | 分解的元学习小样本命名实体识别
【线段树实战】最近的请求次数 + 区域和检索 - 数组可修改+我的日程安排表Ⅰ/Ⅲ
EasyCVR平台接入RTMP协议,接口调用提示获取录像错误该如何解决?
How do test / development programmers get promoted? From nothing, from thin to thick
Win11远程桌面连接怎么打开?Win11远程桌面连接的五种方法