数独(二进制优化)

在这里插入图片描述

思路:暴力数独就是判断行列和3X3的方格内没有重复的数就填一个,这样暴力搜,我们可以发现可用通过一个9位的二进制串表示行列或者3x3的方格内的数有没有用过,比如row[0]=111111111,就表示第1行中1,2,3…,9都没用过,列也相同,3x3的方格表示的特殊一点,原来是9X9的方格,现在我们只需要用个2进制串就能表示一个3X3的方格中的数有没有用过那么我们就可以吧原来的缩成9X9的缩小成3X3的用下标进行映射对应块,所以初始化我们吧这个3个的值都置为全1(1<<9)-1,我们发现在原图中每个点所在3X3的格子和行列中需要填的空越少的越好填,那么就加一个剪枝,我们先找容易填的地方,这里我们就需要记录行列和小方块&操作之后1的个数,我们用lowbit求一下就行,因为填数时,需要改名row,col,cell的值需要lowbit操作顺便就用了

看不懂也没关系毕竟我比较菜

//#pragma GCC optimize(2)
#include<bits/stdc++.h>
 
using namespace std;
typedef long long ll;
#define SIS std::ios::sync_with_stdio(false)
#define space putchar(' ')
#define enter putchar('\n')
#define lson root<<1
#define rson root<<1|1
typedef pair<int,int> PII;
typedef pair<int,PII> PIII;
const int mod=1e9+7;
const int N=2e5+5;
const int inf=0x7f7f7f7f;

int gcd(int a,int b)
{
    
    
    return b==0?a:gcd(b,a%b);
}
 
ll lcm(ll a,ll b)
{
    
    
    return a*(b/gcd(a,b));
}
 
template <class T>
void read(T &x)
{
    
    
    char c;
    bool op = 0;
    while(c = getchar(), c < '0' || c > '9')
        if(c == '-')
            op = 1;
    x = c - '0';
    while(c = getchar(), c >= '0' && c <= '9')
        x = x * 10 + c - '0';
    if(op)
        x = -x;
}
template <class T>
void write(T x)
{
    
    
    if(x < 0)
        x = -x, putchar('-');
    if(x >= 10)
         write(x / 10);
    putchar('0' + x % 10);
}
ll qsm(int a,int b,int p)
{
    
    
    ll res=1%p;
    while(b)
    {
    
    
        if(b&1)
            res=res*a%p;
        a=1ll*a*a%p;
        b>>=1;
    }
    return res;
}
const int M=9;
int cnt[1<<M],row[M],col[M],cell[3][3];
int mp[1<<M];
char str[100] ;
inline int lowbit(int x)
{
    
    
    return x & -x;
}
void init()
{
    
    
    for(int i=0;i<M;i++) col[i]=row[i]=(1<<M)-1;
    for(int i=0;i<3;i++)
     for(int j=0;j<3;j++)
       cell[i][j]=(1<<M)-1;
}
int get(int x,int y)
{
    
    
    return row[x]&col[y]&cell[x/3][y/3];
}
bool dfs(int num)
{
    
    
    if(!num)return true;
    int minv=10;
    int x,y;
    for(int i=0;i<M;i++)
    {
    
    
        for(int j=0;j<M;j++)
        {
    
    
            if(str[i*9+j]=='.'){
    
    
                 int t=cnt[get(i,j)];
            if(t<minv){
    
    
                minv=t;
                x=i;
                y=j;
            }
            }
           
        }
    }
    for(int i=get(x,y);i;i-=lowbit(i))
    {
    
    
        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(num-1))return true;
        row[x]+=1<<t;
        col[y]+=1<<t;
        cell[x/3][y/3]+=1<<t;
        str[x*9+y]='.';

    }
    return false;
}
int main()
{
    
    
  
    for(int i=0;i<M;i++)mp[1<<i]=i;
    for(int i=0;i< 1<<M;i++)
    {
    
    
        int s=0;
        for(int j=i;j;j-=lowbit(j))s++;
        cnt[i]=s;

    }
   
 
    while(cin>>str,str[0]!='e')
    {
    
    
        int num=0;
        init();
        for(int k=0,i=0;i<M;i++)
         for(int j=0;j<M;j++,k++)
         {
    
    
             if(str[k]!='.'){
    
    
                int t=str[k]-'1';
                row[i]-=1<<t;
                col[j]-=1<<t;
                cell[i/3][j/3]-=1<<t;
             }
             else num++;
         }
         dfs(num);
         cout<<str<<endl;

    }
    
       
    

    
       

   
      
   return 0;

}


猜你喜欢

转载自blog.csdn.net/qq_43619680/article/details/111597976