当前位置:网站首页>【acwing】166. 数独****(DFS)
【acwing】166. 数独****(DFS)
2022-04-21 17:53:00 【percation】

如何能正确的搜出所有的方案
1.顺序
2.剪枝
搜索:
1.优化搜索顺序
大部分情况下,应优选搜索分支较少的节点
2.排除等效冗余
3.可行性剪枝
4.最优性剪枝
5.记忆化搜索(DP)
在这题中
使用了位运算优化(9位的01二进制数)
求行、列与九宫格的交集状态(按位与运算)
循环或lowbit运算O(1),返回最后一个1
1.优化搜索顺序:√(有)
选择
2.排序等效冗余 :×(无重复性情况)
3.可行性剪枝:√(有)
4.最优化剪枝:×(无)
5.记忆化搜搜(DP):×(无)
还是没有很理解
#include <bits/stdc++.h>
using namespace std;
const int N = 9, M = 1 << N;//当N为10时,会段错误
int row[N], col[N], cell[3][3];
char str[100];
int ones[M];//快速的求出有多少个1,打表
int mp[M];//快速求出2的多少次方,指数的多少
void init(){
for(int i = 0; i <N ; i++) row[i] = col[i] = (1 << N) - 1;//每行每列每个格子都没有填
for(int i = 0; i < 3; i++){
//不太理解???
for(int j = 0; j < 3; j++){
cell[i][j] = (1 << N) - 1;
}
}
}
void draw(int x, int y, int t, bool is_set){
//在x,y这个位置填上数字t,bool值表示,当前x,y是填上一个数,还是删除一个数(恢复现场)
if(is_set) str[x * N + y] = '1' + t;//t为0到8
else str[x * N + y] = '.';
int v = 1 << t;
if(!is_set) v = -v;//清空操作,取反?
row[x] -= v;
col[y] -= v;
cell[x/3][y/3] -= v;
}
int lowbit(int x){
return x & -x;
}
int get(int x, int y){
//与运算
return row[x] & col[y] & cell[x/3][y/3];
}
bool dfs(int cnt){
if(!cnt) return true;//没有空格了,说明找到了一种方案
//否则,两个辅助函数,lowbit,get(能填那些数)
int miv = 10;
int x,y;//存这个格子的值
for(int i = 0; i < N; i++){
for(int j = 0; j < N; j++){
if(str[i * N + j] == '.'){
//如果当前位置是空格,才枚举
int state = get(i,j);// 这个格子能填的交集是啥
if(ones[state] < miv){
//如果
miv = ones[state];
x = i, y = j;
}
}
}
}
//x,y存的是分支数量最少的一个格子
//枚举能填那些数
int state = get(x,y);
for(int i = state; i; i -= lowbit(i)){
int t = mp[lowbit(i)];//log_2的值是多少 ,没理解
draw(x,y,t,true);
if(dfs(cnt - 1)) return true;//如果成功,去下一个。
draw(x,y,t,false); //否则,清空
}
return false;
}
int main(){
for(int i = 0; i < N; i++) mp[1 << i] = i;
for(int i = 0; i < M; i++){
for(int j = 0; j < N; j++){
ones[i] += i >> j & 1;
}
}
while(cin >> str, str[0] != 'e'){
init();
int cnt = 0;//cnt 表示有多少空位
for(int i = 0, k = 0; i < N; i++){
for(int j = 0; j < N; j++,k++){
if(str[k] != '.'){
//说明是数字
int t = str[k] - '1';
draw(i,j,t,true);//填上一个数,为true;
}
else{
//说明它是空格
cnt++;
}
}
}
dfs(cnt);
puts(str);
}
return 0;
}
版权声明
本文为[percation]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_45866781/article/details/124324905
边栏推荐
- 【最佳实践】巡检项:对象存储(COS)存储桶公有读写
- High expansion and high availability engineering practice of recommended resource bits related to short video app
- 上位机这样玩,才有意思!
- Why do infrastructure engineers prefer MySQL?
- 终于有人讲明白了!原来这才是全球低时延一张网技术
- 用户发送请求到执行controller方法的过程发生了什么
- 修改van-dropdown-menu默认高度
- [best practice] patrol item: object storage (COS) bucket public read / write
- Logstash ~ output of logstash
- Pytorch数据封装进入网络前的几种方式
猜你喜欢

Analysis on the adaptation layer of openharmony UI framework (I)

科技云报道:DPU市场火热,未来会任由几家大厂吃独食吗?
MySQL ODBC驱动简介

Deep cultivation of the industry for decades, interpretation from multiple perspectives! Digital IT operation from the perspective of thinking transformation

Mobile date plug-in (add your favorite)

单片机能做什么,你有什么有单片机或开源硬件做的有意思的作品吗

俄乌冲突引发顾虑 五眼网络安全部门建议盟友增强关键基础设施防护

Considering loose coupling of microservice architecture? Be careful of these traps

Game partner topic: breederdao establishes a partnership with crypto unicorns

谷歌投放手机端一直收集不到转化目标怎么回事?
随机推荐
High expansion and high availability engineering practice of recommended resource bits related to short video app
Logstash ~ configuration file of logstash
mysql8.0设置忽略大小写后无法启动
使用K3S创建本地开发集群
The conflict between Russia and Ukraine raised concerns. The five eye network security department suggested that allies strengthen the protection of key infrastructure
看完这篇教程,你将拥有自己的一个卫星(diy全程详解)
Chest X-ray images - dataset
将模型训练外包真的安全吗?新研究:外包商可能植入后门,控制银行放款
Addition, deletion, modification and query of MySQL advanced table
How to turn on the undisturbed time with win10 memo how to set the message of rest day to be undisturbed
干货|app自动化测试之Appium 源码分析
Logstash ~ filter of logstash
谷歌投放手机端一直收集不到转化目标怎么回事?
New ETL scheduling batch management tool taskctl 8.0 simplest installation
综述:CFD的未来之路
Shell编程学习(三)条件判断、流程控制
为什么基础设施工程师更喜欢MySQL?
ASE35P03-ASEMI场效应管35P03
小小的模糊查询,竟来来回回修改了3次代码?找个电子厂上班吧
LogStash~LogStash的多个输入输出