当前位置:网站首页>[30. N-queen problem]
[30. N-queen problem]
2022-07-23 16:57:00 【Little silly bird_ coding】
DFS Two cores
- to flash back
- prune : It can be judged in advance that the scheme is illegal , So there is no need to continue searching , Directly subtract the following and directly backtrack .
subject


Two ways :
- The first method : By enumerating , There is only one queen in each line , When enumerating , Just make sure there is only one in each line , So there was no row[N]. Depth traversal of rows .
- The second method : There are two situations in each position , Or put the queen , Or don't let the queen go
Method 1:
- For the first r OK, No i A place , Judge whether the queen can be placed at each point , If possible , Then release the queen , Then process r + 1 That's ok . until r = n, The program instruction line is completed .

#include <iostream>
using namespace std;
const int N = 20; // Positive and negative diagonal , So it is 20
// bool Array is used to determine whether the next location of the search is feasible
// col Column ,dg Diagonals ,udg Anti diagonal , The two slashes corresponding to the point and whether there is a queen on the column
// g[N][N] Used to save path , Storage chessboard
int n;
char g[N][N];
bool col[N], dg[N], udg[N]; // Indicates the status bit , Not occupied as false, Otherwise true
void dfs(int u )
{
if (u == n) // Full of chessboard , Output chessboard , Already searched n That's ok , So output this path
{
for (int i = 0; i < n; i ++) puts(g[i]);
puts(""); // Line break
return;
}
for (int i = 0; i < n; i ++) // The first u That's ok , The first i Column Whether to release the queen
{
if (!col[i] && !dg[u + i] && !udg[n - u + i]) // No conflict , Put the queen
{
g[u][i] = 'Q';
col[i] = dg[u + i] = udg[n - u + i] = true; // Corresponding Column , Oblique line State change
dfs(u + 1); // Process next line
col[i] = dg[u + i] = udg[n - u + i] = false; // Restore the scene
g[u][i] = '.';
}
}
return;
}
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;
}
You can also write like this
if (u == n) // Full of chessboard , Output chessboard , Already searched n That's ok , So output this path
{
for (int i = 0; i < n; i ++)
{
for (int j = 0; j < n; j ++)
cout << (g[i][j]);
puts(""); // Line break
}
cout << endl;
return;
}
The time complexity is O(n!)
Diagonalsdg[u + i], Anti diagonaludg[n - u + i]Subscript inu+i+i andn−u+iIt means intercept
- In the following analysis (x,y) Equivalent to (u,i)
Anti diagonaly=x+binterceptb=y−x, Because we're going to b As an array subscript , obviously b Can't be negative , So we add+n( actually+n+4,+2nWill do ), To ensure that the result is positive , namelyy - x + n- And diagonals
y=−x+bThe intercept isb=y+x=, The intercept here must be positive , So you don't need to add an offset
Method 2:
- From the first row to the first column , Start putting queens in turn
// Different search order Time complexity is different So the search order is very important !
#include <iostream>
using namespace std;
const int N = 20;
int n;
char g[N][N];
bool row[N], col[N], dg[N], udg[N]; // Because it's a search , So I added row
// s Indicates the number of queens that have been put on
void dfs(int x, int y, int s)
{
// Deal with situations beyond the boundary
if (y == n) y = 0, x ++ ;
if (x == n) {
// x==n Description has been enumerated n^2 It's a place
if (s == n) {
// s==n It means that it was successfully put on n A queen
for (int i = 0; i < n; i ++ ) puts(g[i]);
puts("");
}
return;
}
// Branch 1: Put the queen
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] = '.';
}
// Branch 2: Don't let the queen go
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;
}
边栏推荐
- 【Web漏洞探索】SQL注入漏洞
- 中国化NFT?NFR横空出世
- General paging function
- 泰山OFFICE技术讲座:段落边框的布局绘制分析
- C语言基础篇 —— 2-5 指针与函数知识点
- 解决data functions should return an object 并(Property “visible“ must be accessed with “$data.visible“)
- Detector: detect objects with recursive feature pyramid and switchable atolos convolution
- 网络协议与攻击模拟:wireshark使用、ARP协议
- 面试官:生成订单30分钟未支付,则自动取消,该怎么实现?
- Object.defineproperty method, data agent
猜你喜欢
随机推荐
COPU副主席刘澎:中国开源在局部领域已接近或达到世界先进水平
pinia(菠萝)
Nifi 1.16.3 cluster setup +kerberos+ user authentication
Visualization of gross domestic product (GDP) data
Heartless sword English Chinese bilingual poem 006. to my wife
2022-7-22 面经复习+简单题目整理
Browser homology policy
考过PMP对实际工作帮助大吗?
mysql如何查询不在数据库里的数据?
一道反序列化的CTF题分享
【Flutter -- 布局】弹性布局(Flex 和 Expanded)
Scale Match for Tiny Person Detection
JS if the decimal is 0, subtract it, not keep it
go run,go build,go install有什么不同
iphone 无法打开openv**文件的解决方案
Microcomputer principle and technical interface practice in class
Docker install redis
同花顺上选择券商,网上客户经理开户安全吗
YOLOV7
Tan Zhangxi, director of risc-v Foundation: risc-v has gradually expanded from the edge to the center








