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

边栏推荐
猜你喜欢

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

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

Safe operation instructions for oscilloscope probe that must be read by engineers

pd.melt() vs reshape2::melt()

nodejs 简单例子程序之express

Application of current probe in ECU and electrical system current measurement

Software testing -- common testing tools

Flexible current probe selection guide

Interview shock: why does TCP need three handshakes?

乐观锁解析
随机推荐
11.1-cm24 nearest common ancestor
11.2-hj86 find the maximum number of continuous bits
Nc78 reverse linked list
Nc68 jumping steps
文件基础知识
pd.melt() vs reshape2::melt()
Jz32 print binary tree from top to bottom
Thales launches solutions to help SAP customers control cloud data
[haoi2015] tree operation
MySQL optimistic lock
MySQL index optimization introduction
Boomi won the "best CEO in diversity" and the "best company in career growth" and ranked among the top 50 in the large company category
Use of stm8s003f3 UART
GAN的详细介绍及其应用(全面且完整)
7. 依赖注入
Diagonalization, power of a
用GaussDB(for Redis)存画像,推荐业务轻松降本60%
There was an error while marking a file for deletion
MySQL lost the previous 0 after the decimal number type select
Nc15 sequence traversal of binary tree