当前位置:网站首页>Sudoku written for once and for all
Sudoku written for once and for all
2022-07-23 21:48:00 【Can ran】

Obviously, you need DFS, For each unfilled space , We should exclude the numbers that have appeared in the same column and the same nine palaces . If there is no accident without optimization, it will T fly ︿( ̄︶ ̄)︿.
Consider pruning :
- Optimize search order
- Eliminate redundant information
- Feasibility pruning ( It is impossible to follow the current branch )
- Optimal pruning ( According to the current branch, the optimal solution is worse than the current solution )
- Memory search
Here we mainly consider optimizing the search order . This is actually the same as the simple perception when we do Sudoku , Start with a few optional spaces . So every time we search, we will judge which grid has the smallest number of choices ( All binary numbers that may be used can be preprocessed 1 The number of , Time consuming direct call ).
Record the number of options for each grid , Can be compressed with binary state , Opening nine digits means nine numbers . For convenience , We put 1~9 mapping 0 ~8. Open three arrays r o w [ N ] , c o l [ N ] , c e l l [ N / 3 ] [ N / 3 ] row[N],col[N],cell[N/3][N/3] row[N],col[N],cell[N/3][N/3] Record each line separately 、 Each column 、 What numbers can't be filled in each Jiugong grid . For position at ( x , y ) (x,y) (x,y) Number of numbers , use row[x] & col[y] & cell[x/3][y/3] You can find out which numbers can be filled in this blank , Then search these numbers honestly 233, It can be used lowbit Get binary, the following is 1 Every one of them , Convert it into decimal numbers (1 The number of digits corresponding to the position of , It can be processed in advance ). Search for each number and change it row.col.cell The state of , Recover when you go back .
#include<bits/stdc++.h>
using namespace std;
const int N=9;
string str;
int mp[1<<N],nums[1<<N],row[N],col[N],cell[N][N];
inline int lowbit(int x)
{
return x&(-x);
}
int get(int x,int y)
{
return row[x]&col[y]&cell[x/3][y/3];// Find out what numbers may be filled in this position
}
void init()// initialization : All digits can be filled in
{
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;
}
bool dfs(int cnt)
{
if(cnt==0) return true;// All the figures to be filled in have been filled in
int minn=10,x,y;
for(int i=0;i<N;i++)
for(int j=0;j<N;j++)
{
if(str[i*9+j]=='.')
{
int temp=nums[get(i,j)];
if(temp<minn)
{
minn=temp;
x=i,y=j;
}
}
}// Find the smallest number set in all the remaining cells to be filled , It's a pruning process
for(int i=get(x,y);i;i-=lowbit(i))// Enumerate all possible numbers in this field
{
int t=mp[lowbit(i)];
row[x]-=(1<<t);
col[y]-=(1<<t);
cell[x/3][y/3]-=(1<<t);
str[x*9+y]='1'+t;
if(dfs(cnt-1)) return true;
row[x]+=(1<<t);
col[y]+=(1<<t);
cell[x/3][y/3]+=(1<<t);
str[x*9+y]='.';// Restore
}
return false;
}
int main()
{
for(int i=0;i<N;i++) mp[1<<i]=i;// Save binary in advance 1 Where it is
for(int i=1;i<(1<<N);i++)
for(int j=i;j;j-=lowbit(j)) nums[i]++;
// There are several binary numbers stored in advance that may be enumerated later 1
while(cin>>str&&str[0]!='e')
{
init();
int cnt=0;
for(int i=0,k=0;i<9;i++)
for(int j=0;j<9;j++,k++)
{
if(str[k]!='.')
{
int t=str[k]-'1';//1~9 Mapping to 0~8
row[i]-=(1<<t);
col[j]-=(1<<t);
cell[i/3][j/3]-=(1<<t);
}// This one digit number is given , Put it in the corresponding position
else cnt++;// Figures to be filled in +1
}
dfs(cnt);
cout<<str<<endl;
}
return 0;
}
边栏推荐
- Complete set of official openlayers instances
- & 9 nodemon automatic restart tool
- Kuberntes cloud native combat VI uses rook to build CEPH cluster
- Given an array composed of numbers, realize the name whose output ID is a number and sorted from small to large
- Principle and implementation of hash table, unordered set and mapping
- Jianzhi offer II 115. reconstruction sequence: topological sorting construction problem
- MySQL数据库索引
- 05_ UE4 advanced_ Material UV scaling
- Improving performance with explicit rendering
- Redis常用命令对应到Redisson对象操作
猜你喜欢

淘宝助理停用,用大淘营导入数据包上传宝贝提示“主图为必填项,不能为空”是什么原因?如何解决?

Cluster chat server: Design of database table

The total ranking of blogs is 918

集群聊天服务器:集群与分布式理论
![[complex overloaded operator]](/img/ff/aafaa9471a1bd6ef57f6a619449e80.png)
[complex overloaded operator]

Apprentissage Lambda (utilisation du comparateur après tri, regroupement après collecte avec collectors.groupingby)

Openlayers instance advanced view positioning advanced view positioning

Uniapp uses canvas to write a circular progress bar

阿里onedate分层思想

Deep learning - NLP classic papers, courses, papers and other resources sorting and sharing
随机推荐
【愚公系列】2022年06月 .NET架构班 084-微服务专题 Abp vNext微服务通信
Complete set of official openlayers instances
query中的customer exit客户出口变量
& 9 nodemon automatic restart tool
Practice data Lake iceberg lesson 37 kakfa write the enfour, not enfour test of iceberg's icberg table
Distributed transaction scheme: best effort notification scheme
集群聊天服务器:如何解决跨服务器通信问题 | redis发布-订阅
Golang invalid argument to INTN
Compare kernelshap and treeshap based on speed, complexity and other factors
Detailed explanation of cesium events (mouse events, camera events, keyboard events, scene trigger events)
Synchronized同步锁的基本原理
Programmer growth Article 26: how to hold a good daily morning meeting?
JS object array de duplication
Leaderboard design in game server
Day109. Shangyitong: integrate Nacos, hospital list, drop-down list query, hospital online function, hospital details query
Jedis 6 - Introduction and difference between redisson and jedis
Basic principle of synchronized lock
Given an array composed of numbers, realize the name whose output ID is a number and sorted from small to large
【数学建模暑期培训】配送中心选址问题
Euclidean clustering (API) and its single tree segmentation