UVALive 3351 Easy and Not Easy Sudoku Puzzles 位運算~判斷簡單數獨

題意:給定一個9*9的數獨,要求判斷是否爲簡單數獨。
數獨:對於每一行每一列或者子方格內,只能填1~9這幾個數,並且每個數字只能出現一次,比如說:
a
如果一個9*9的數獨是簡單數獨的話,這個數獨的解是獨一無二的
也就是說,每次可以推導出一個點,這個點的值是確定的,直到數獨被填滿。
對於下圖,x只能填2
b
對於下圖,x只能填1
c
判斷數獨是否是簡單數獨。如果是輸出1,不是輸出0.

這道題坑爹,質疑標程寫錯,數據出錯。
其實仔細思考一番可以發現使用位運算來做,因爲只有9個數,那麼我們可以使用9位二進制來代表該數是否已經使用,若已經使用則爲1,否則爲0
那麼我們可以很容易得到一個01的狀態,它其實是個整數。
顯然,可以容易每個點的可以放置的狀態,第i位爲1說明該點可以放置i,否則不能放置,若該狀態只能放置1個點,也就是二進制中1的個數爲1,那麼就必然放置該點。
(這題訓練賽的時候沒有A,還被隊友嘲諷了一發,隊友敲了20分鐘就過了,我勒個擦!)
這個代碼其實早就出來了,因爲我兩種情況都考慮到了,詭異的一直wa
代碼1:

//author: CHC
//First Edit Time:  2015-09-24 21:03
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <algorithm>
#include <limits>
using namespace std;
typedef long long LL;
const int INF = numeric_limits<int>::max();
const LL LL_INF= numeric_limits<LL>::max();
int bs[10][10],h[10],l[10],tmp[10][10],xs[4][4];
int check(){
    for(int i=1;i<=9;i++)
        for(int j=1;j<=9;j++)
            if(!bs[i][j])return 0;
    return 1;
}
int count1(int x){
    int cnt=0;
    while(x>0){
        x-=x&-x;
        ++cnt;
    }
    return cnt;
}
void print(int x){
    for(int i=0;i<9;i++)
        if((x>>i)&1)printf("%d ",i+1);
    puts("");
}
const int X = (1<<9)-1;
int main()
{
    int T;
    scanf("%d",&T);
    while(T--){
        for(int i=1,x;i<=9;i++){
            for(int j=1;j<=9;j++){
                scanf("%d",&x);
                if(x)bs[i][j]=(1<<(x-1));
                else bs[i][j]=0;
            }
        }
        int flag=0;
        while(1){
            if(check())break;
            for(int i=1;i<=9;i++)h[i]=X,l[i]=X;

            for(int i=1;i<=3;i++)
                for(int j=1;j<=3;j++)xs[i][j]=X;

            for(int i=1;i<=9;i++)
                for(int j=1;j<=9;j++){
                    h[i]&=~bs[i][j];l[j]&=~bs[i][j];xs[(i-1)/3+1][(j-1)/3+1]&=~bs[i][j];
                }

            for(int i=1;i<=9;i++)
                for(int j=1;j<=9;j++){
                    if(bs[i][j])tmp[i][j]=X&bs[i][j];
                    else tmp[i][j]=X&h[i]&l[j]&xs[(i-1)/3+1][(j-1)/3+1];
                }

            int rcnt=0;
            for(int i=1;i<=9&&!rcnt;i++)
                for(int j=1;j<=9&&!rcnt;j++){
                    if(bs[i][j])continue;
                    int tx=tmp[i][j];
                    int ct=count1(tx);
                    if(ct==1){
                        bs[i][j]=tx;
                        ++rcnt;
                        break;
                    }
                    for(int ii=(i-1)/3*3+1;ii<=((i-1)/3+1)*3;ii++){
                        for(int jj=(j-1)/3*3+1;jj<=((j-1)/3+1)*3;jj++){
                            if(ii!=i&&jj!=j)tx&=~tmp[ii][jj];
                        }
                    }
                    int tt=count1(tx);
                    if(tt==1){
                        bs[i][j]=tx;
                        ++rcnt;
                        break;
                    }
                }
            if(rcnt==0){
                flag=1;break;
            }
        }
        printf("%d",!flag);
    }
    puts("");
    return 0;
}

這個代碼我無意中刪掉部分,就過了,發現其中只考慮了第一種情況。
代碼2:

//author: CHC
//First Edit Time:  2015-09-24 21:03
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <algorithm>
#include <limits>
using namespace std;
typedef long long LL;
const int INF = numeric_limits<int>::max();
const LL LL_INF= numeric_limits<LL>::max();
int bs[10][10],h[10],l[10],tmp[10][10],xs[4][4];
int check(){
    for(int i=1;i<=9;i++)
        for(int j=1;j<=9;j++)
            if(!bs[i][j])return 0;
    return 1;
}
int count1(int x){
    int cnt=0;
    while(x>0){
        x-=x&-x;
        ++cnt;
    }
    return cnt;
}
void print(int x){
    for(int i=0;i<9;i++)
        if((x>>i)&1)printf("%d ",i+1);
    puts("");
}
const int X = (1<<9)-1;
int main()
{
    int T;
    scanf("%d",&T);
    while(T--){
        for(int i=1,x;i<=9;i++){
            for(int j=1;j<=9;j++){
                scanf("%d",&x);
                if(x)bs[i][j]=(1<<(x-1));
                else bs[i][j]=0;
            }
        }
        int flag=0;
        while(1){
            if(check())break;
            for(int i=1;i<=9;i++)h[i]=X,l[i]=X;

            for(int i=1;i<=3;i++)
                for(int j=1;j<=3;j++)xs[i][j]=X;

            for(int i=1;i<=9;i++)
                for(int j=1;j<=9;j++){
                    h[i]&=~bs[i][j];l[j]&=~bs[i][j];xs[(i-1)/3+1][(j-1)/3+1]&=~bs[i][j];
                }

            for(int i=1;i<=9;i++)
                for(int j=1;j<=9;j++){
                    if(bs[i][j])tmp[i][j]=X&bs[i][j];
                    else tmp[i][j]=X&h[i]&l[j]&xs[(i-1)/3+1][(j-1)/3+1];
                }

            int rcnt=0;
            for(int i=1;i<=9&&!rcnt;i++)
                for(int j=1;j<=9&&!rcnt;j++){
                    if(bs[i][j])continue;
                    int tx=tmp[i][j];
                    int ct=count1(tx);
                    if(ct==1){
                        bs[i][j]=tx;
                        ++rcnt;
                        break;
                    }
                }
            if(rcnt==0){
                flag=1;break;
            }
        }
        printf("%d",!flag);
    }
    puts("");
    return 0;
}
   數據(需要能正確的推導出哪些點是一定的):
   0 8 0 3 0 0 0 0 9
   0 0 0 0 8 0 0 0 3
   3 0 0 5 0 0 0 8 2
   1 9 8 4 5 7 3 2 6
   5 2 3 9 6 1 8 4 7
   4 7 6 2 3 8 1 9 5
   9 3 4 8 2 5 7 6 1
   6 5 2 0 0 4 9 3 8
   8 1 7 6 9 3 2 5 4

   7 8 0 3 0 0 0 0 9
   0 0 0 0 8 0 0 0 3
   3 0 0 5 0 0 0 8 2
   1 9 8 4 5 7 3 2 6
   5 2 3 9 6 1 8 4 7
   4 7 6 2 3 8 1 9 5
   9 3 4 8 2 5 7 6 1
   6 5 2 0 0 4 9 3 8
   8 1 7 6 9 3 2 5 4

   1 6 0 0 0 0 7 0 0
   0 0 0 0 0 1 0 0 0
   0 0 0 4 0 9 0 0 0
   0 0 0 0 3 0 0 0 0
   0 0 0 0 0 0 0 0 0
   0 0 0 0 5 0 0 0 0
   0 0 0 0 0 0 0 0 0
   0 0 0 0 8 0 0 0 0
   0 0 0 0 0 0 0 0 0

   1 0 0 0 0 0 7 0 0
   0 0 0 0 0 1 0 0 0
   0 0 0 0 0 0 6 0 0
   0 0 0 0 0 0 0 0 1
   0 0 0 0 0 0 0 0 0
   0 0 0 0 0 0 0 0 0
   0 0 0 0 0 0 0 0 0
   0 0 0 0 0 0 0 0 0
   0 0 0 0 0 0 0 0 0

   1 2 3 0 0 0 0 0 0
   4 5 6 0 0 0 0 0 0
   7 8 0 0 0 0 0 0 0
   0 0 0 0 0 0 0 0 0
   0 0 0 0 0 0 0 0 0
   0 0 0 0 0 0 0 0 0
   0 0 0 0 0 0 0 0 0
   0 0 0 0 0 0 0 0 0
   0 0 0 0 0 0 0 0 0
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章