当前位置:网站首页>C language n queen problem
C language n queen problem
2022-07-29 05:29:00 【YiHeboy】
C Language N queens problem
Introduce : What is? N queens problem
- In chess , The queen is a very powerful unit , It can be centered on the position of the Queen's pawn , Horizontal and vertical and The slope is 1 and -1 Move in the direction of ,N The Queen's question refers to n * n On my chessboard n A queen , And make any two The queen is different , Different columns are not on the same slash .
I use the method of backtracking to solve this problem :
With 4x4 Take the chessboard of :
Let's take the position of the first column in the first row , This position does not conflict with the walking range of other queens , So we can put
| 1 | 2 | 3 | 4 | |
|---|---|---|---|---|
| 1 | Q | q | q | q |
| 2 | q | q | ||
| 3 | q | q | ||
| 4 | q | q |
The second row also starts from the first column , When you get to the first column , Conflict , Second column , Conflict , The third column does not conflict , Place the second queen
| 1 | 2 | 3 | 4 | |
|---|---|---|---|---|
| 1 | Q | q | q | q |
| 2 | q | q | Q | q |
| 3 | q | q | q | q |
| 4 | q | q | q |
The third line repeats the operation of the second line , But any column found will conflict , Therefore, you can't put , Let's go back to the second line , Place the queen in the next column
Then come to the third row. At this time, there is no conflict in the second column , You can put
| 1 | 2 | 3 | 4 | |
|---|---|---|---|---|
| 1 | Q | q | q | q |
| 2 | q | q | q | Q |
| 3 | q | Q | q | q |
| 4 | q | q | q | q |
We found that there was no alternative in the fourth line . The first attempt failed , Go back one level , No columns available , Go back to the next level , No columns available , Finally, return to the first line .
Second retry
Let's go back to the first step , This shows that we have no solution to choose the first column in the first row , So we shouldn't choose the first column in the first row , Let's try the second column , And so on
| 1 | 2 | 3 | 4 | |
|---|---|---|---|---|
| 1 | q | Q | q | q |
| 2 | q | q | ||
| 3 | q | q | ||
| 4 | q |
Code implementation, such as
#include <bits/stdc++.h>// Lazy use C++ Universal head of , Actually sum C No difference ,.C Change the file if it fails to compile C The head of the
using namespace std;
bool attack(int *chess, int col, int row)
{
for(int i = 0; i < row; ++i){
if(chess[i] == col){
// Vertical conflict
return true;
}
if(col - chess[i] == row - i || chess[i] - col == row - i){
// Slash conflict
return true;
}
}
return false;
}
int nqueen()
{
int queens = 0;
cin>>queens;
int col = 0, cnt = 0, row = 0;
int chess[9] = {
-1};
// The topic requirements are the most 9 A queen , Moreover, the compiler of the website does not allow the array length to be a variable .
while(!(col == queens && row == 0)){
if(!attack(chess,col,row)){
// Judge that there is no conflict
chess[row++] = col;
if(row == queens){
// All queens have been prevented
++cnt;
col = chess[--row];// Go back to the previous line to find a new solution
++col;
while(col == queens){
// If the previous line is the last one, go back up
if(queens == 1){
col = queens;
row = 0;
break;
}
chess[row--] = -1;
col = chess[row];
col++;
}
}
else{
col = 0;
}
}
else{
++col;// In case of conflict, the next column
while(col == queens && row != 0){
// If there are conflicts in the whole column, go back
chess[row--] = -1;
col = chess[row];
col++;
}
}
}
return cnt;
}
int main()
{
int cnt = 0;
cnt = nqueen();
cout<<cnt;
return 0;
}
above !
I hope I can give you some help .
thank
边栏推荐
- 小程序中的DOM对象元素块动态排序
- One dimensional array exercise
- Li Yan, CEO of parallel cloud: cloudxr, opens the channel to the metauniverse
- 365 day challenge leetcode1000 question - distance between bus stops on day 038 + time-based key value storage + array closest to the target value after transforming the array and + maximum value at t
- ANSI C类型限定符
- Day 2
- Why is Google's internal tools not suitable for you?
- 数据泄漏、删除事件频发,企业应如何构建安全防线?
- 为啥谷歌的内部工具不适合你?
- 365 day challenge leetcode 1000 questions - day 042 array sequence number conversion + relative ranking discretization processing
猜你喜欢

预约中,2022京东云产业融合新品发布会线上开启

研发效能生态完整图谱&DevOps工具选型必看

365 day challenge leetcode 1000 questions - day 042 array sequence number conversion + relative ranking discretization processing

365 day challenge leetcode 1000 questions - day 040 design jump table + avoid flooding + find the latest grouping with size M + color ball with reduced sales value

365天挑战LeetCode1000题——Day 035 每日一题 + 二分查找 13

平行云CEO 李岩:CloudXR ,开启通往元宇宙的通道

Come on! See how Clickhouse, which has risen 16 places a year, can be implemented in jd.com

In depth analysis of common cross end technology stacks of app

【赛事预告】云上开发,高效智能——第二届阿里云ECS CloudBuild开发者大赛即将启动

Alibaba cloud architect Liang Xu: MES on cloud box helps customers quickly build digital factories
随机推荐
Cryengine5 shader debugging
With cloud simulation platform, Shichuang technology supports the upgrading of "China smart manufacturing"
Bubble sort c language
D3d Shader Instruction
力扣994:腐烂的橘子(BFS)
时间复杂度和空间复杂度
Day 2
Redirection and files
Cmu15-213 malloc lab experiment record
How to get command parameters in Visual Basic.Net
数千个数据库、遍布全国的物理机,京东物流全量上云实录 | 卓越技术团队访谈录
During the appointment, the 2022 JD cloud industrial integration new product launch was launched online
【C语言系列】— 不创造第三个变量,实现两个数的交换
数据泄漏、删除事件频发,企业应如何构建安全防线?
【剑指offer】— 详解库函数atoi以及模拟实现atoi函数
C语言 N皇后问题
【C语言系列】—三种方法模拟实现strlen库函数的方法
Container security open source detection tool - veinmind (mirror backdoor, malicious samples, sensitive information, weak password, etc.)
C语言文件操作
QML type: state state