DLX(变形数独)hdu4069



F.A.Q
Hand In Hand
Online Acmers
Forum | Discuss
Statistical Charts
Problem Archive
Realtime Judge Status
Authors Ranklist
 
      C/C++/Java Exams     
ACM Steps
Go to Job
Contest LiveCast
ICPC@China
Best Coder beta
VIP | STD Contests
Virtual Contests 
    DIY | Web-DIY beta
Recent Contests
Author ID 
Password 
  Register new ID
BestCoder官方群:385386683 欢迎加入~
寻人启事:2014级新生看过来!

Squiggly Sudoku

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


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
      
      
3 144 18 112 208 80 25 54 144 48 135 38 147 80 121 128 97 130 32 137 32 160 144 114 167 208 0 32 192 100 160 160 208 96 183 192 101 209 80 39 192 86 48 136 80 114 152 48 226 144 112 160 160 149 48 128 0 112 166 215 96 160 128 41 128 39 153 32 209 80 101 136 35 192 96 200 67 80 112 208 68 96 144 48 144 81 81 16 53 144 48 128 96 224 144 48 128 103 128 38 163 208 80 0 37 224 209 0 32 135 48 176 192 64 112 176 192 104 192 101 128 89 80 82 32 150 48 149 48 224 208 16 48 224 192 33 128 0 114 176 135 0 80 112 169 137 32 148 32 192 96 176 144 32 192 96 193 64 80 80 96 192 96 144 88 48 217 16 16 80 112 176 224 176 129 48 128 40 208 16 37 145 32 128 96 196 96 176 136 32 192 32 227 176 144 80 96 192 32 176 192 80 98 160 145 80 48 224 128 48 144 80 96 224 183 128 48 128 36 224 144 51 144 32 128 105 131 64 112 136 32 192 36 224 176 224 208 80 64 64 116 192 83 96
 

Sample Output
      
      
Case 1: 521439678 763895124 984527361 346182795 157964832 812743956 235678419 479216583 698351247 Case 2: No solution Case 3: Multiple Solutions


思路:跟普通数独差不多,只需要先进行一遍dfs,对数独进行分块,然后就是普通的数独了

写的太恶心了

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=9*9*9+10;
const int maxm=9*9*4+10;
const int maxnode=maxn*4+maxm+10;
const int N=20;
const int SLOT=0;
const int ROW=1;
const int COL=2;
const int SUB=3;
int id[N][N],a[N][N],cnt;
char g[maxnode];

int encode(int a,int b,int c)
{
    return a*81+b*9+c;
}
void decode(int code,int &a,int &b,int &c)
{
    code--;
    c=code%9;code/=9;
    b=code%9;code/=9;
    a=code;
}
struct DLX
{
    int n,m,size,num;
    int U[maxnode],D[maxnode],L[maxnode],R[maxnode];
    int H[maxn],S[maxm];
    int row[maxnode],col[maxnode];
    int ansd,ans[maxnode];
    void init(int N,int M)
    {
        n=N,m=M;
        for(int i=0; i<=m; i++)
        {
            U[i]=D[i]=i;
            L[i]=i-1;
            R[i]=i+1;
        }
        L[0]=m;
        R[m]=0;
        size=m;
        memset(S,0,sizeof(S));
        memset(H,-1,sizeof(H));
    }
    void Link(int r,int c)
    {
        ++S[col[++size]=c];
        row[size]=r;
        D[size]=D[c];
        U[D[c]]=size;
        U[size]=c;
        D[c]=size;
        if(H[r]<0)H[r]=L[size]=R[size]=size;
        else
        {
            R[size]=R[H[r]];
            L[R[H[r]]]=size;
            L[size]=H[r];
            R[H[r]]=size;
        }
    }
    void remove(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[col[j]];
            }
        }
    }
    void restore(int c)
    {
        for(int i=U[c]; i!=c; i=U[i])
        {
            for(int j=L[i]; j!=i; j=L[j])
            {
                U[D[j]]=D[U[j]]=j;
                ++S[col[j]];
            }
        }
        L[R[c]]=R[L[c]]=c;
    }
    void Dance(int d)
    {
        if(cnt>1)return;
        if(R[0]==0)
        {
            ansd=d;
            for(int i=0; i<d; i++)
            {
                int r,c,v;
                decode(ans[i],r,c,v);
                a[r][c]=v+1;
            }
            cnt++;
            return;
        }
        int c=R[0];
        for(int i=R[0]; i; i=R[i])
            if(S[i]<S[c])c=i;
        remove(c);
        for(int i=D[c]; i!=c; i=D[i])
        {
            ans[d]=row[i];
            for(int j=R[i]; j!=i; j=R[j])remove(col[j]);
            Dance(d+1);
            if(cnt>1)return;
            for(int j=L[i]; j!=i; j=L[j])restore(col[j]);
        }
        restore(c);
    }
} dlx;
void bfs(int sx,int sy,int index)
{
    queue<pair<int,int> > q;
    q.push(make_pair(sx,sy));
    id[sx][sy]=index;
    while(!q.empty())
    {
        pair<int,int> tmp=q.front();q.pop();
        int x=tmp.first,y=tmp.second;
        if(a[x][y]<128&&y-1>=0&&id[x][y-1]==-1)
            q.push(make_pair(x,y-1)),id[x][y-1]=index;
        else if(a[x][y]>=128)a[x][y]-=128;

        if(a[x][y]<64&&x+1<9&&id[x+1][y]==-1)
            q.push(make_pair(x+1,y)),id[x+1][y]=index;
        else if(a[x][y]>=64)a[x][y]-=64;

        if(a[x][y]<32&&y+1<9&&id[x][y+1]==-1)
            q.push(make_pair(x,y+1)),id[x][y+1]=index;
        else if(a[x][y]>=32)a[x][y]-=32;

        if(a[x][y]<16&&x-1>=0&&id[x-1][y]==-1)
            q.push(make_pair(x-1,y)),id[x-1][y]=index;
        else if(a[x][y]>=16)a[x][y]-=16;
    }
}
int main()
{
    int T,cas=1;
    scanf("%d",&T);
    while(T--)
    {
        for(int i=0; i<9; i++)
            for(int j=0; j<9; j++)scanf("%d",&a[i][j]);
        memset(id,-1,sizeof(id));
        int tmp=0;
        for(int i=0; i<9; i++)
            for(int j=0; j<9; j++)
                if(id[i][j]==-1)
                    bfs(i,j,++tmp);
        dlx.init(9*9*9,9*9*4);
        for(int i=0; i<9; i++)
            for(int j=0; j<9; j++)
                for(int k=1; k<=9; k++)
                {
                    if(a[i][j]%16==0||a[i][j]%16==k)
                    {
                        int row=encode(i,j,k);
                        dlx.Link(row,encode(SLOT,i,j)+1);
                        dlx.Link(row,encode(ROW,i,k));
                        dlx.Link(row,encode(COL,j,k));
                        dlx.Link(row,encode(SUB,id[i][j]-1,k));
                    }
                }
        cnt=0;
        dlx.Dance(0);
        printf("Case %d:\n",cas++);
        if(cnt==0)printf("No solution\n");
        else
        {
            if(cnt>1)printf("Multiple Solutions\n");
            else
            {
                for(int i=0; i<9; i++)
                {
                    for(int j=0; j<9; j++)
                        printf("%d",a[i][j]);
                    printf("\n");
                }
            }
        }
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值