数独(バイナリ最適化)

ここに画像の説明を挿入

アイデア:暴力的な数独は、ランクと3X3グリッドに繰り返し数があるかどうかを判断し、1つを埋めることです。このように、ブルートフォース検索では、ランクまたは3x3グリッドの数をaで表すことができるかどうかを確認できます。 9ビットのバイナリ文字列。たとえば、row [0] = 111111111は、最初の行の1、2、3 ...、9のいずれも使用されておらず、列も同じであることを意味します。3x3グリッドは特別なポイント。以前は9X9グリッドでした。現在は、バイナリ文字列を使用して3X3グリッドの数値が使用されているかどうかを示すだけで、元の9X9を3X3に縮小し、添え字を使用して対応するものをマッピングできます。ブロックなので、初期化します。これら3つの値をすべて1(1 << 9)に設定しましょう-1。元の画像の各ポイントの3X3グリッドと行では、必要なスペースが少ないことがわかりました。記入した方が良いので、1つ追加します。剪定のために、記入しやすい場所を見つけましょう。ここでは、ランクと小さな正方形の数、および操作後の1の数を記録する必要があります。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