当前位置:网站首页>Eight queens n Queens
Eight queens n Queens
2022-07-27 00:29:00 【Deep dream, high ambition, floating life like a dream】

Refer to the correspondents playing the Blue Bridge Cup If you invade or delete
All codes are similar to those of the original blogger I feel that the array is set too much, and I want to change the code
Wrote a set At first, the function simply thought Set each array element to 0 1 that will do I found something wrong after running Later, it was found that the array elements can be set +=x As long as it is in the same row or diagonal line +1 If multiple conditions are met, it will increase Only when The array element is 0 by 0 It will be considered as the place where the queen can be placed
#include <iostream>
using namespace std;
int map[11][11];
int a[10]; // a[] Store solution
int n; // Chessboard size
int ans;
bool check(int i, int j) // Check (i,j) Is it legal
{
if (!map[i][j])
return true; // b[j],c[i-j+n],d[i+j] by 0 This indicates that this point can be used to place chess pieces .
return false;
}
void set(int i, int j, int x)
{
for (int z = 1; z <= n; z++)
{
map[z][j] += x;
map[i][z] += x;
}
for (int z = 1; z <= n; z++)
{
if (i + z <= n && j + z <= n)
{
map[i + z][j + z] += x;
}
if (i + z <= n && j - z >= 1)
{
map[i + z][j - z] += x;
}
if (i - z >= 1 && j + z <= n)
{
map[i - z][j + z] += x;
}
if (i - z >= 1 && j - z >= 1)
{
map[i - z][j - z] += x;
}
}
}
void dfs(int i) // dfs The first i Row chess pieces
{
// The boundary conditions
if (i > n) // dfs Finish all the pieces
{
;
ans++;
if (ans <= 3) // Just the first three solutions
{
for (int i = 1; i <= n; i++) // Output solution
{
cout << a[i] << ' ';
}
cout << endl;
}
return;
}
for (int j = 1; j <= n; j++) // Enumerate all positions of a row
{
//(i,j) Points meet the conditions for putting chess pieces , Let's put the pieces on (i,j) spot
if (check(i, j))
{
a[i] = j; // The column number satisfying the solution exists a[i] in
set(i, j, 1);
dfs(i + 1); // Handle the pieces in the next line
set(i, j, -1);
}
}
}
int main()
{
cin >> n;
dfs(1);
cout << ans << endl;
return 0;
}
边栏推荐
- 画冲击函数
- The crawler parses the object of the web page. Element name method
- AlexNet(Pytorch实现)
- [PCB open source sharing] stc32g12k128/stc8h8k64u development board
- 13_集成学习和随机森林(Ensemble Learning and Random Forests)
- 1、 Kubernetes basic concept + environment installation (build cross server public network environment)
- C and pointer Chapter 18 runtime environment 18.7 problems
- Xshell连接服务器时报“Could not load host key”错误
- 4. Talk about the famous Zhang Zhengyou calibration method
- Sliding window problem summary
猜你喜欢
随机推荐
20220720 toss deeplobcut2
画冲击函数
放图仓库-3(功能图像)
【4.6 中国剩余定理详解】
UNET notes
Friend友元函数以及单例模式
CDs simulation of minimum dominating set based on MATLAB
ResNet论文解读及代码实现(pytorch)
torch.相关函数
Matlab based medical imaging technology filtering backprojection simulation, including direct backprojection, S-L filtering, R-L filtering, LeWitt filtering
[Qt]元对象系统
RecBole使用1
CSDN article syntax rules
Database: MySQL foundation +crud basic operation
【4.10 博弈论详解】
Leetcode - hash table
我的第一篇博客-迷茫的大三人
Huffman encoding and decoding
"Could not load host key" error when xshell connects to the server
Matlab simulation of inverted pendulum control system based on qlearning reinforcement learning





![[Qt]属性](/img/ca/5f9d8f33e38b0ac5cbb0768a7b3ffd.png)


