当前位置:网站首页>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
边栏推荐
- 省市区三级联动(简单又完美)
- 英伟达周锡健:设计到数字营销的最后一公里
- With frequent data leakage and deletion events, how should enterprises build a security defense line?
- AD常用快捷键
- 如视技术副总裁杨永林:当传统产业遇到“数字空间”
- Vs code的安装步骤及环境配置
- 预约中,2022京东云产业融合新品发布会线上开启
- C语言文件操作
- Alibaba cloud architect details nine trends in the game industry
- 携手数字人、数字空间、XR平台,阿里云与伙伴共同建设“新视界”
猜你喜欢

Complete ecological map of R & D Efficiency & selection of Devops tools

321,京东言犀×NLPCC 2022挑战赛开赛!

科班同学真的了解未来的职业规划吗?

During the appointment, the 2022 JD cloud industrial integration new product launch was launched online

力扣994:腐烂的橘子(BFS)

Redirection and files

C语言 一级指针

AiTalk创始人梁宇淇:镜像连接虚拟与现实的纽带

【C语言系列】—文件操作详解(上)

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 day challenge leetcode 1000 questions - day 041 two point search completion anniversary + nth magic number + online election
京东云金秋上云特惠进行中!扫码参与活动
串口通讯部分详解
Come on! See how Clickhouse, which has risen 16 places a year, can be implemented in jd.com
[event preview] cloud digital factory and digital transformation and innovation forum for small and medium-sized enterprises
With frequent data leakage and deletion events, how should enterprises build a security defense line?
[event preview] cloud development, efficient and intelligent - the second Alibaba cloud ECS cloudbuild developer competition is about to start
【C语言系列】— 打印100~200之间的素数
Day 1
【C语言系列】— 字符串+部分转义字符详解+注释小技巧
Preemptive appointment | Alibaba cloud shadowless cloud application online conference appointment opens
阿里云联合鼎捷软件发布云上数字工厂解决方案,实现云MES系统本地化部署
QML type: mousearea
Differences between texture2d and texture2dproj under webgl1.0
167. 两数之和 II - 输入有序数组
使用微信小程序扫码登录系统PC端web的功能
Teardown's method of lifting the time limit
365天挑战LeetCode1000题——Day 036 二叉树剪枝 + 子数组和排序后的区间和 + 删除最短的子数组使剩余数组有序
How to get command parameters in Visual Basic.Net
CMU15-213 Shell Lab实验记录