当前位置:网站首页>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;
}
边栏推荐
猜你喜欢
Involution? Foam? Change? Ten questions directly hit the core puzzle of "meta universe" – the essence of "ask ta- Wang Lei about the time of the universe"
ddia数据密集型应用系统设计 整理
从TiDB上线阿里云的背后,如何看待云数据库的变革趋势
Redis series - overview day1-1
论文笔记:LBCF: A Large-Scale Budget-Constrained Causal Forest Algorithm
How did I get a salary increase of 13k+ after one year of employment?
2022-06-17 网工进阶(九)IS-IS-原理、NSAP、NET、区域划分、网络类型、开销值
MySQL 用 limit 为什么会影响性能?
Do you know all the configurations of pychrm?
Knowing these interview skills will help you avoid detours in your test job search
随机推荐
SnakeYAML配置文件解析器
旧手机变废为宝,充当服务器使用
Redis series - overview day1-1
Day_ fifteen
How to talk about salary correctly in software testing interview
效应与定律
Home office earned me C | community essay
FreeRTOS内核时钟不对的问题解决
mysql使用过程中遇到的问题
千万级购物车系统缓存架构方案
记一次基于PHP学生管理系统的开发
项目经理在项目中起到的作用
[proficient in high concurrency] deeply understand the basis of C language and C language under assembly
Day_ 18 hash table, generic
深入浅出对话系统——自己实现Transformer
try with resource
XShell连接VMWare虚拟机
2022云的世界会更好吗
Simple dialogue system -- implement transformer by yourself
PLSQL 存储函数SQL编程