当前位置:网站首页>N皇后问题
N皇后问题
2022-06-25 16:36:00 【jie3606】
#include<iostream>
#include<cmath>
using namespace std;
int N;
int QueuePos[100];
void nQueue(int n) {
//在第1~n-1个皇后已经摆好的情况下,确定第n个皇后的位置
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++) {
//逐渐尝试第k个皇后的位置
int j = 0;
for (j = 0; j < n; j++) {
//已经摆好的皇后的
if (QueuePos[j] == i || (abs(QueuePos[j] - i) == abs(n - j))) {
break;
}
}
if (j == n) //for循环不是由于break退出,表明第n个皇后的位置和前j个皇后的位置不冲突
{
QueuePos[n] = i;
nQueue(n + 1);
}
}
}
int main() {
cin >> N;
nQueue(0);
return 0;
}
边栏推荐
- First knowledge of database
- 论文笔记:Generalized Random Forests
- Day_ fifteen
- Perfect shuffle problem
- 批量--07---断点重提
- [proficient in high concurrency] deeply understand the basics of assembly language
- The problem of missing precision of kettle table input components
- 这项最新的调查研究,揭开多云发展的两大秘密
- Creating a uniapp project using hbuilder x
- Are these old system codes written by pigs?
猜你喜欢

This latest research has revealed two secrets of cloudy development

The problem of missing precision of kettle table input components

Why does MySQL limit affect performance?

Redis Series - Overview day1 - 1

Problems encountered in using MySQL

Wireshark网卡无法找到或没有显示的问题

Vscode plug-in self use

使用hbuilder X创建uniapp项目

1-8file sharing in VMWare

微信公众号服务器配置
随机推荐
Notes: lbcf: a Large Scale budget Constrained causal Forest Algorithm
Optimization of lazyagg query rewriting in parsing data warehouse
WPF development essays Collection - ECG curve drawing
解析数仓lazyagg查询重写优化
論文筆記:LBCF: A Large-Scale Budget-Constrained Causal Forest Algorithm
【機器學習】基於多元時間序列對高考預測分析案例
Paper notes: generalized random forests
MySQL 用 limit 为什么会影响性能?
Reading mysql45 lecture - index continued
Internship: the annotation under swagger involves the provision of interfaces
vscode插件自用
About: encryption and decryption of rsa+aes data transmission [chapter], project practice (special summary)
计网 | 形象理解路由协议RIP、OSPF、BGP
Batch --07--- breakpoint lifting
Uncover ges super large scale graph computing engine hyg: Graph Segmentation
mac php多版本管理以及安装swoole扩展
[proficient in high concurrency] deeply understand the basics of assembly language
JVM内存结构
Simple dialogue system -- implement transformer by yourself
Uniapp to preview pictures (single / multiple)