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

边栏推荐
- Software testing -- common testing tools
- 11.2-HJ86 求最大连续bit数
- 【翻译】Logstash、Fluentd、Fluent Bit,还是Vector?如何选择合适的开源日志收集器...
- Detailed introduction and application of GaN (comprehensive and complete)
- 一次备库的坏块的修复过程
- Sequential storage structure, chain storage structure and implementation of stack
- 程序的编译
- Tensor to img & imge to tensor (tensor conversion of pytorch)
- Analysis of regression problem, modeling and prediction
- List转换问题
猜你喜欢

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

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

Could not stop Cortex-M device! Please check the JTAG cable solution

7. Dependency injection

MySQL index optimization introduction

Tang's little helper

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

软件测试进阶篇—测试分类

工程师必看的示波器探头安全使用说明书

One week activity express | in simple terms, issue 8; Meetup Chengdu station registration in progress
随机推荐
How to create an effective help document?
Is it true that CITIC Securities' low commission account opening is free of 5? Is it safe
MySQL index optimization introduction
If you want to do a good job in software testing, you can first understand ast, SCA and penetration testing
SQL那些事
Stm32f105rbt6 internal flash debugging
Taishan Office Technology Lecture: conversion relations of inch, centimeter, pound, pika, Ti, line, word line and pixel
Tkinter GUI address book management system
Leetcode 101. symmetric binary tree & 100. same tree & 572. Subtree of another tree
uniapp滚动条置顶效果、自定义页面滚动条的位置(整理)
Insufficient space on Disk C mklink solves vscode expansion migration to other disks
RTC 性能自动化工具在内存优化场景下的实践
Sequential storage structure, chain storage structure and implementation of stack
Express of nodejs simple example program
This is a quick look-up table of machine & deep learning code
Keil5 “Loading PDSC Debug Description Failed for STMicroelectronics STM32Hxxxxxxx”解决办法
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
[QNX hypervisor 2.2 user manual]9.4 dryrun
[QNX hypervisor 2.2 user manual]9.5 dump
泛域名配置方法