当前位置:网站首页>n-queens problem
n-queens problem
2022-07-25 18:28:00 【Seven inclinations】
subject :
Eight queens question :
Put eight queens on a chess board , Make no two queens attack each other , Find out all
Cloth chess method of . Run the machine and check the results .
reflection : Extend this question to N The Queen's situation , The test is in N In large cases , For example N=16 When the
Hou , Can your program get the result quickly , If not , Think about how to optimize the algorithm .
#include <iostream>
using namespace std;
#define max 50
int x[max + 1];// The first i The queen of the line is on the x[i] On the column
int n;// The width of the chessboard and the number of queens
int sum = 0;// Calculate the number of solutions
// The coordinates of the queen to be placed are (row,col), Judge whether it can be placed
bool Place(int row, int col) {
for (int i = 1; i < row; i++) // Before the comparison row -1 Line has been placed queen aax
{
if (col == x[i] || abs(row - i) == abs(col - x[i])) // Judge whether the queen to be released and the existing queens are in the same column or the same slash
return false;
}
return true;
}
// Recursive backtracking function , If the conditions are met, recursion down , If the conditions are not met , Then go back
void Backtrack(int t, int n) {
if (t == n + 1) // Successfully traverse all chessboards once , Formed a solution
sum++;
else {
// If this line traverses to n Still can't place the queen , Then go back
for (int i = 1; i <= n; i++) {
x[t] = i;
if (Place(t, x[t]))// Judge whether the queen can be placed
Backtrack(t + 1, n);// If the queen can be placed in this line , Then recurse to the next line
}
}
}
int main()
{
cout << " Please enter the number of queens : ";
cin >> n;
Backtrack(1, n);
cout << " The number of solutions is : " << sum << endl;
return 0;
}Test chart :

边栏推荐
- 11.1-CM24 最近公共祖先
- 泛域名配置方法
- vim基本操作命令
- Insufficient space on Disk C mklink solves vscode expansion migration to other disks
- PHP memory management mechanism and garbage collection mechanism
- Keil5 “Loading PDSC Debug Description Failed for STMicroelectronics STM32Hxxxxxxx”解决办法
- 【网页性能优化】SPA(单页面应用)首屏加载速度慢怎么办?
- 如何创建一个有效的帮助文档?
- 可视化模型网络连接
- Leetcode 101. symmetric binary tree & 100. same tree & 572. Subtree of another tree
猜你喜欢

泛域名配置方法

RTC 性能自动化工具在内存优化场景下的实践

如何创建一个有效的帮助文档?

One week activity express | in simple terms, issue 8; Meetup Chengdu station registration in progress

Today's sleep quality record is 84 points

【华为机试真题】字符串匹配

使用sqldeveloper连接mysql

Stm8s003f3 internal flash debugging
![[web page performance optimization] what about the slow loading speed of the first screen of SPA (single page application)?](/img/e2/9b62dd9bd0f2bc8dcbb6d9c851254d.png)
[web page performance optimization] what about the slow loading speed of the first screen of SPA (single page application)?

电流探头在ECU、电气系统电流测量的应用
随机推荐
Uniapp scroll bar topping effect, customized page scroll bar position (sorting)
Could not stop Cortex-M device! Please check the JTAG cable solution
Stm8s003f3 internal flash debugging
网易严选库存中心设计实践
11.2-hj86 find the maximum number of continuous bits
JZ32 从上往下打印二叉树
How to create an effective help document?
List转换问题
List conversion problem
JZ71 跳台阶扩展问题
电流探头在ECU、电气系统电流测量的应用
泛域名配置方法
软件测试——常用的测试工具
Leetcode 101. symmetric binary tree & 100. same tree & 572. Subtree of another tree
Recognized by international authorities! Oceanbase was selected into the Forrester translational data platform report
GAN的详细介绍及其应用(全面且完整)
7. Dependency injection
【网页性能优化】SPA(单页面应用)首屏加载速度慢怎么办?
ServletConfig class and ServletContext class
Oracle import error: imp-00038: unable to convert to environment character set handle