当前位置:网站首页>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, andQ
indicates 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:
4
Sample 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.
边栏推荐
- 表的创建、修改与删除
- 社交电商:流量红利已尽,裂变营销是最低成本的获客之道
- flink sql任务变更,在sql里面增加几个字段后,从以前保存的savepoint恢复启动出错。
- 荧光标记多肽FITC/AMC/FAM/Rhodamine/TAMRA/Cy3/Cy5/Cy7-Peptide
- Dialog manager in the fourth chapter: the dialog message loop
- 链动2+1模式简单,奖励结构丰厚,自主裂变?
- 【精讲】利用原生js实现todolist
- 12.机器学习基础:评估机器学习模型
- 记录一些遇见的bug——mapstruct和lombok同时使用时,转换实体类时数据丢失问题
- 肖sir ——自动化讲解
猜你喜欢
随机推荐
How many moments have you experienced the collapse of electronic engineers?
StarRocks July Community Update
移动流量的爆发式增长,社交电商如何选择商业模式
4.深度学习的几何解释与梯度的优化
path development介绍
8.电影评论分类:二分类问题
mysql bool blind
数值类型转换02
肖sir_测试点
安装ambari
Kotlin-Flow常用封装类:StateFlow的使用
【Harmony OS】【ARK UI】ets使用startAbility或startAbilityForResult方式调起Ability
社交电商:流量红利已尽,裂变营销是最低成本的获客之道
工程水文学知识点
DC-5靶场下载及渗透实战详细过程(DC靶场系列)
BIOTIN ALKYNE CAS:773888-45-2价格,供应商
传统企业如何转型社交电商,泰山众筹的玩法有哪些?
接口测试如何准备测试数据
接口测试框架实战(三)| JSON 请求与响应断言
【软件工程之美 - 专栏笔记】35 | 版本发布:软件上线只是新的开始