当前位置:网站首页>DFS's complement to pruning
DFS's complement to pruning
2022-08-03 04:33:00 【Really fine duck】
I mentioned pruning optimization earlier, one is the optimal solution pruning, the current path is not as good as the optimal solution, so it can be pruned, one is the optimal solutionIt is feasible pruning, that is, the current scheme is illegal, so it can also be pruned.
So here is a simple use of pruning optimization through the eight queens problem
Classic Case - Eight Queens Problem
n−n−queens problem refers to placing nn queens on an n×nn×n chess board so that queens cannot attack each otherto, that is, no two queens can be on the same row, column, or slash.
Now, given an integer n, please output all the chess pieces that satisfy the conditions.
Input format
One line containing the integer n.
Output format
Each solution occupies n lines, and each line outputs a string of length n to represent the complete chessboard state.
Where
.indicates that the square state of a certain position is empty, andQindicates that a queen is placed on the square of a certain position.After the output of each scheme is completed, a blank line is output.
Note: No extra spaces at the end of the line.
The order of the output schemes is arbitrary, as long as there are no repetitions and no omissions.
Data Range
1≤n≤9
Sample input:
4Sample output:
.Q.....QQ.....Q...Q.Q......Q.Q..
Thoughts
Similar to searching for full arrangement, the search order can be the same as full arrangement, only one queen and only one queen can be placed in each row, so weYou can enumerate where each row of queens is placed like a full permutation.
Pay attention to pruning here. When we put the queen in a certain position, we need to judge whether there is a conflict. If there is a conflict, we don't need to go down.After the search is completed, the rest of the search does not need to be executed, and the process is pruned.In a nutshell, pruning means that it can be judged in advance that the current solution is illegal, so there is no need to search down.
How to judge whether there is a conflict? The title requires that each row, column and slash cannot be repeated, so use this conditionto prune.
Code implementation
#include using namespace std;int n;char p[20][20];bool lie[20],dg[20],udg[20];//lie indicates whether the column has been used, dg indicates a positive diagonal, and udg indicates an anti-diagonalvoid dfs(int u)//u represents the number of lines that have been drilled down at this time{if(u==n)//If u==n means that u has searched the last line, it can be output{for(int i=0;ib=y-x means subscript=u-i+n represents a positive diagonal, and because the subscript must be a positive number,So you can add a larger positive number, here we add n *///y=-x+b->b=y+x means subscript=u+i represents an antidiagonal{if(!lie[i]&&!dg[u+i]&&!udg[u-i+n])//lie indicates whether the column is used, dg[u+i] indicates whether a diagonal line is usedPass,//udg[u-i+n] indicates whether an anti-diagonal has been used{p[u][i]='Q';//If the queen can be filled at this time, let this point be Qlie[i]=dg[u+i]=udg[u-i+n]=1;dfs(u+1);lie[i]=dg[u+i]=udg[u-i+n]=0;//Restore the scenep[u][i]='.';}}}int main(){cin>>n;for(int i=0;i If there are any mistakes or omissions, please correct me.
边栏推荐
猜你喜欢

How many moments have you experienced the collapse of electronic engineers?

【软件工程之美 - 专栏笔记】35 | 版本发布:软件上线只是新的开始

【Harmony OS】【ARK UI】Date 基本操作

t conditional judgment statement and if loop

接口和协议

肖sir ——自动化讲解

社交电商:流量红利已尽,裂变营销是最低成本的获客之道

普乐蛙VR台风体验馆厂家VR防震减灾模拟VR沉浸式体验设备

软件开发的最大的区别是什么?

【Harmony OS】【ArkUI】ets开发 图形与动画绘制
随机推荐
2022 the first of the new league henan (4) : zhengzhou university of light industry G - maze
js的垃圾回收机制
社交电商:链动2+1模式,为什么能在电商行业生存那么久?
13.机器学习基础:数据预处理与特征工程
The flink sql task is changed, and after adding several fields to the sql, an error occurs when restoring from the previously saved savepoint.
【翻译】开发与生产中的Kubernetes修复成本对比
9.新闻分类:多分类问题
在线密码生成工具推荐
DC-6靶场下载及渗透实战详细过程(DC靶场系列)
Assembly answers
索引创建、删除与使用
Kotlin-Flow常用封装类:StateFlow的使用
「短视频+社交电商」营销模式爆发式发展,带来的好处有什么?
JS bottom handwriting
ScanNet数据集讲解与点云数据下载
1.一个神经网络示例
工程制图-齿轮
How many moments have you experienced the collapse of electronic engineers?
【Harmony OS】【ARK UI】轻量级数据存储
MCM箱模型建模方法及大气O3来源解析
