[HDU 4069]刷的水DLX

来源:互联网 发布:论文灌水知乎 编辑:程序博客网 时间:2024/06/07 09:53

终于过了。。。要记住 记解,解可能被覆盖

memO的故事。。。

题意:求  一行+一列+不规则图形 满足数独的解

原题:

Squiggly Sudoku

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 624    Accepted Submission(s): 244


Problem Description
Today we play a squiggly sudoku, The objective is to fill a 9*9 grid with digits so that each column, each row, and each of the nine Connecting-sub-grids that compose the grid contains all of the digits from 1 to 9.
Left figure is the puzzle and right figure is one solution.

Now, give you the information of the puzzle, please tell me is there no solution or multiple solution or one solution.
 

Input
The first line is a number T(1<=T<=2500), represents the number of case. The next T blocks follow each indicates a case.
Each case contains nine lines, Each line contains nine integers.
Each module number tells the information of the gird and is the sum of up to five integers:
0~9: '0' means this gird is empty, '1' - '9' means the gird is already filled in.
16: wall to the up
32: wall to the right
64: wall to the down
128: wall to the left
I promise there must be nine Connecting-sub-grids, and each contains nine girds.
 

Output
For each case, if there are Multiple Solutions or no solution just output "Multiple Solutions" or "No solution". Else output the exclusive solution.(as shown in the sample output)
 

Sample Input
3144 18 112 208 80 25 54 144 48135 38 147 80 121 128 97 130 32137 32 160 144 114 167 208 0 32192 100 160 160 208 96 183 192 101209 80 39 192 86 48 136 80 114152 48 226 144 112 160 160 149 48128 0 112 166 215 96 160 128 41128 39 153 32 209 80 101 136 35192 96 200 67 80 112 208 68 96 144 48 144 81 81 16 53 144 48128 96 224 144 48 128 103 128 38163 208 80 0 37 224 209 0 32135 48 176 192 64 112 176 192 104192 101 128 89 80 82 32 150 48149 48 224 208 16 48 224 192 33128 0 114 176 135 0 80 112 169137 32 148 32 192 96 176 144 32192 96 193 64 80 80 96 192 96144 88 48 217 16 16 80 112 176224 176 129 48 128 40 208 16 37145 32 128 96 196 96 176 136 32192 32 227 176 144 80 96 192 32176 192 80 98 160 145 80 48 224128 48 144 80 96 224 183 128 48128 36 224 144 51 144 32 128 105131 64 112 136 32 192 36 224 176224 208 80 64 64 116 192 83 96
 

Sample Output
Case 1:521439678763895124984527361346182795157964832812743956235678419479216583698351247Case 2:No solutionCase 3:Multiple Solutions
 

我的Code :7377B

#include<iostream>#include<cstdio>#include<cstring>using namespace std;const int maxN = 10 * 100, maxM = 5*10 * 10;const int max_size = maxN * maxM;const int inf = 0x3f3f3f3f;int L[max_size], R[max_size], U[max_size], D[max_size], CH[max_size], RH[max_size];int S[maxM], O[maxM],memO[maxM];int head, size;int node(int up, int down, int left, int right) {    U[size] = up, D[size] = down;    L[size] = left, R[size] = right;    D[up] = U[down] = R[left] = L[right] = size;    return size++;}int anscnt;bool mat[maxN][maxM];void init(int N, int M) {    size = 0;    head = node(0, 0, 0, 0);    for (int j = 1; j <= M; ++j) {        CH[j] = node(size, size, L[head], head), S[j] = 0;    }    for (int i = 1; i <= N; ++i) {        int row = -1, k;        for (int j = 1; j <= M; ++j) {            if (!mat[i][j]) continue;            if (row == -1) {                row = node(U[CH[j]], CH[j], size, size);                RH[row] = i, CH[row] = CH[j], ++S[j];            } else {                k = node(U[CH[j]], CH[j], L[row], row);                RH[k] = i, CH[k] = CH[j], ++S[j];            }        }    }}void remove(const int &c) {    L[R[c]] = L[c], R[L[c]] = R[c];    for (int i = D[c]; i != c; i = D[i]) {        for (int j = R[i]; j != i; j = R[j]) {            U[D[j]] = U[j], D[U[j]] = D[j];            --S[CH[j]];        }    }}void resume(const int &c) {    for (int i = U[c]; i != c; i = U[i]) {        for (int j = L[i]; j != i; j = L[j]) {            ++S[CH[j]];            U[D[j]] = D[U[j]] = j;        }    }    L[R[c]] = R[L[c]] = c;}int len;bool DLX(const int &k) {    if (R[head] == head) {        if (anscnt==0)        {           len=k-1;             anscnt=1;             for (int i=0;i<=len;++i) memO[i]=O[i];                   }        if (len==k-1)        {                     bool same=true;                     for (int i=0;i<=len;++i)                         if (memO[i]!=O[i])                         {                            same=false;                            break;                         }                     if (!same)++anscnt;        }/*        else if (len>k-1)        {             len=k-1;             anscnt=1;             for (int i=0;i<=len;++i) memO[i]=O[i];             //return true;        }*/        if (anscnt==2) return true;        return false;       // ++anscnt;        //len = k - 1;        //return true;    }    int s = inf, c;    for (int t = R[head]; t != head; t = R[t]) {        if (S[t] < s) s = S[t], c = t;    }    remove(c);    for (int i = D[c]; i != c; i = D[i]) {        O[k] = RH[i];        for (int j = R[i]; j != i; j = R[j]) {            remove(CH[j]);        }        if (DLX(k + 1)) {            return true;        }        for (int j = L[i]; j != i; j = L[j]) {            resume(CH[j]);        }    }    resume(c);    return false;}int color[20][20];//const int color2[20][10]={{0,0,0,0,0,0,0,0,0,0,},{1,0,0,0,1,1,1,2,2,2},{2,0,0,0,1,1,1,2,2,2},{3,0,0,0,1,1,1,2,2,2},{4,3,3,3,4,4,4,5,5,5},{5,3,3,3,4,4,4,5,5,5},{6,3,3,3,4,4,4,5,5,5},{7,6,6,6,7,7,7,8,8,8},{8,6,6,6,7,7,7,8,8,8},{9,6,6,6,7,7,7,8,8,8}};bool mp[20][20][4];int num[20][20];int lis[20][2];const int dir[4][2]={{0,-1},{1,0},{0,1},{-1,0}};int colornum;void bfs(int xx,int yy){     int tail=0;     memset(lis,0,sizeof(lis));     lis[0][0]=xx;     lis[0][1]=yy;     color[xx][yy]=colornum;     for (int i=0;i<=tail;++i)     {         int x=lis[i][0];         int y=lis[i][1];         for (int d=0;d<4;++d)         {             if (!mp[x][y][d] && color[x+dir[d][0]][y+dir[d][1]]==-1)             {                ++tail;                lis[tail][0]=x+dir[d][0];                lis[tail][1]=y+dir[d][1];                color[x+dir[d][0]][y+dir[d][1]]=colornum;             }         }     }     }int main(){    //freopen("hdu4069.txt","r",stdin);    //freopen("hdu4069out.txt","w",stdout);    int t;    scanf("%d",&t);    for (int casenum=1;casenum<=t;++casenum)    {                len=inf;        memset(mat,0,sizeof(mat));        memset(memO,0,sizeof(memO));        //memset(mp,0,sizeof(mp));        memset(color,-1,sizeof(color));        //memset(num,0,sizeof(num));        int c;        for (int i=1;i<=9;++i)            for (int j=1;j<=9;++j)            {                scanf("%d",&c);                if (c>=128)                {                   mp[i][j][0]=1;                   c-=128;                }                else mp[i][j][0]=0;                if (c>=64)                {                   mp[i][j][1]=1;                   c-=64;                }                else mp[i][j][1]=0;                if(c>=32 )                {                   mp[i][j][2]=1;                   c-=32;                }                else mp[i][j][2]=0;                if (c>=16)                {                   mp[i][j][3]=1;                   c-=16;                }                else mp[i][j][3]=0;                num[i][j]=c;            }        colornum=-1;        for (int i=1;i<=9;++i)            for (int j=1;j<=9;++j)            {                if (color[i][j]>=0) continue;                ++colornum;                bfs(i,j);            }        #define NN 81        int rname[NN*NN][3];        int rtot=0;        for (int i=1;i<=9;++i)            for (int j=1;j<=9;++j)            {                if(num[i][j]==0)                {                     for (int k=1;k<=9;++k)                     {                         ++rtot;                         rname[rtot][0]=i;                         rname[rtot][1]=j;                         rname[rtot][2]=k;                         mat[rtot][(i-1)*9+j]=1;                         mat[rtot][NN+(i-1)*9+k]=1;                         mat[rtot][NN*2+(j-1)*9+k]=1;                         mat[rtot][NN*3+color[i][j]*9+k]=1;                         //mat[rtot][NN*4+color2[i][j]*9+k]=1;                     }                          }                else                {                         int k=num[i][j];                         ++rtot;                         rname[rtot][0]=i;                         rname[rtot][1]=j;                         rname[rtot][2]=k;                         mat[rtot][(i-1)*9+j]=1;                         mat[rtot][NN+(i-1)*9+k]=1;                         mat[rtot][NN*2+(j-1)*9+k]=1;                         mat[rtot][NN*3+color[i][j]*9+k]=1;                         //mat[rtot][NN*4+color2[i][j]*9+k]=1;                }            }        init(rtot,NN*4);        printf("Case %d:\n",casenum);        anscnt=0;        DLX(0);        if (anscnt==0) printf("No solution\n");        else if (anscnt==2) printf("Multiple Solutions\n");        else        {            for (int i=0;i<=len;++i)                num[rname[memO[i]][0]][rname[memO[i]][1]]=rname[memO[i]][2];            for (int i=1;i<=9;++i)            {                for (int j=1;j<=9;++j) printf("%d",num[i][j]);                printf("\n");            }        }    }}



原创粉丝点击