当前位置:网站首页>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 :

边栏推荐
猜你喜欢

Oracle import error: imp-00038: unable to convert to environment character set handle

CircleIndicator组件,使指示器风格更加多样化

Related operations of figure

一周活动速递|深入浅出第8期;Meetup成都站报名进行中

Keil5 “Loading PDSC Debug Description Failed for STMicroelectronics STM32Hxxxxxxx”解决办法

Keil5 "loading PDSC debug description failed for STMicroelectronics stm32hxxxxxxx" solution

Practice of RTC performance automation tool in memory optimization scenario

Thales launches solutions to help SAP customers control cloud data

想要做好软件测试,可以先了解AST、SCA和渗透测试

MySQL 索引优化全攻略
随机推荐
【翻译】Logstash、Fluentd、Fluent Bit,还是Vector?如何选择合适的开源日志收集器...
Is it true that CITIC Securities' low commission account opening is free of 5? Is it safe
Use of stm8s003f3 UART
Analysis of regression problem, modeling and prediction
Application of current probe in ECU and electrical system current measurement
GAN的详细介绍及其应用(全面且完整)
电流探头在ECU、电气系统电流测量的应用
uniapp滚动条置顶效果、自定义页面滚动条的位置(整理)
Chapter 5 Basic Scripting: Shell Variables
NC68 跳台阶
Save the image with gaussdb (for redis), and the recommended business can easily reduce the cost by 60%
What are the methods of traversing arrays? What is the difference between the performance of the for loop foreach for/in for/of map and how to choose?
408第二章线性表
字符串函数和内存函数(二)
Number two 2010 real test site
[QNX Hypervisor 2.2用户手册]9.4 dryrun
Taishan Office Technology Lecture: conversion relations of inch, centimeter, pound, pika, Ti, line, word line and pixel
[haoi2015] tree operation
Could not stop Cortex-M device! please check the JTAG cable的解决办法
mysql的小数number类型select之后丢失了前面的0