当前位置:网站首页>n-queens problem
n-queens problem
2022-06-25 17:03:00 【jie3606】
#include<iostream>
#include<cmath>
using namespace std;
int N;
int QueuePos[100];
void nQueue(int n) {
// In the 1~n-1 When the two queens have been arranged , Make sure No n A queen's position
if (n == N) {
for (int i = 0; i < N; i++) {
cout << QueuePos[i] + 1 << " ";
}
cout << endl;
return;
}
//else
for (int i = 0; i < N; i++) {
// Try step by step k A queen's position
int j = 0;
for (j = 0; j < n; j++) {
// The Queen's has been arranged
if (QueuePos[j] == i || (abs(QueuePos[j] - i) == abs(n - j))) {
break;
}
}
if (j == n) //for The cycle is not due to break sign out , Show that No n The Queen's position and front j The positions of the two queens do not conflict
{
QueuePos[n] = i;
nQueue(n + 1);
}
}
}
int main() {
cin >> N;
nQueue(0);
return 0;
}
边栏推荐
猜你喜欢
随机推荐
3.条件概率与独立性
千万级购物车系统缓存架构方案
2021年5月云南省网络空间安全比赛赛题复盘
Tasklet API usage
六大专题全方位优化,阿里巴巴性能优化小册终开源,带你直抵性能极致
【微服务|Sentinel】流控规则概述|针对来源|流控模式详解<直接 关联 链路>
Ten thousand volumes - list of Dali wa
The problem of missing precision of kettle table input components
tensorflow 旧版本
PLSQL storage function SQL programming
论文笔记:LBCF: A Large-Scale Budget-Constrained Causal Forest Algorithm
使用hbuilder X创建uniapp项目
Wechat official account server configuration
mysql使用过程中遇到的问题
Knowing these interview skills will help you avoid detours in your test job search
Pytorch official document learning record
Notes: lbcf: a Large Scale budget Constrained causal Forest Algorithm
内卷?泡沫?变革?十个问题直击“元宇宙”核心困惑丨《问Ta-王雷元宇宙时间》精华实录...
Day_ 18 hash table, generic
Day_ seventeen









