zoj 3122(数独 Dancing Links精确覆盖)

Sudoku

Time Limit: 10 Seconds       Memory Limit: 32768 KB

A Sudoku grid is a 16x16 grid of cells grouped in sixteen 4x4 squares, where some cells are filled with letters from A to P (the first 16 capital letters of the English alphabet), as shown in figure 1a. The game is to fill all the empty grid cells with letters from A to Psuch that each letter from the grid occurs once only in the line, the column, and the 4x4 square it occupies. The initial content of the grid satisfies the constraints mentioned above and guarantees a unique solution.



Write a Sudoku playing program that reads data sets from a text file. Each data set encodes a grid and contains 16 strings on 16 consecutive lines as shown in figure 2. The i th string stands for the i th line of the grid, is 16 characters long, and starts from the first position of the line. String characters are from the set {A,B,...,P,-}, where - (minus) designates empty grid cells. The data sets are separated by single empty lines and terminate with an end of file. The program prints the solution of the input encoded grids in the same format and order as used for input.

Sample Input

--A----C-----O-I
-J--A-B-P-CGF-H-
--D--F-I-E----P-
-G-EL-H----M-J--
----E----C--G---
-I--K-GA-B---E-J
D-GP--J-F----A--
-E---C-B--DP--O-
E--F-M--D--L-K-A
-C--------O-I-L-
H-P-C--F-A--B---
---G-OD---J----H
K---J----H-A-P-L
--B--P--E--K--A-
-H--B--K--FI-C--
--F---C--D--H-N-

Sample Output

FPAHMJECNLBDKOGI
OJMIANBDPKCGFLHE
LNDKGFOIJEAHMBPC
BGCELKHPOFIMAJDN
MFHBELPOACKJGNID
CILNKDGAHBMOPEFJ
DOGPIHJMFNLECAKB
JEKAFCNBGIDPLHOM
EBOFPMIJDGHLNKCA
NCJDHBAEKMOFIGLP
HMPLCGKFIAENBDJO
AKIGNODLBPJCEFMH
KDEMJIFNCHGAOPBL
GLBCDPMHEONKJIAF
PHNOBALKMJFIDCEG
IAFJOECGLDPBHMNK

Source: Southeastern European Regional 2006

题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3038

分析:这题样例有问题,我改过来了,注意每次输出解时都要输出空行,最后一组不用空行。。。

代码:

#include<cstdio>
using namespace std;
const int N=16;
const int mm=N*N*N*4+N*N*4+N;
const int mn=N*N*N+N;
int U[mm],D[mm],L[mm],R[mm],C[mm],X[mm];
int H[mn],S[mn],Q[mn];
bool v[mn];
char map[mn];
int size;
void prepare(int r,int c)
{
    for(int i=0;i<=c;++i)
    {
        S[i]=0;
        U[i]=D[i]=i;
        L[i+1]=i;
        R[i]=i+1;
    }
    R[size=c]=0;
    while(r)H[r--]=-1;
}
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[C[j]];
}
void resume(int c)
{
    for(int i=U[c];i!=c;i=U[i])
        for(int j=L[i];j!=i;j=L[j])
            ++S[C[U[D[j]]=D[U[j]]=j]];
    L[R[c]]=R[L[c]]=c;
}
bool Dance(int k)
{
    if(!R[0])
    {
        for(int i=0;i<k;++i)map[(X[Q[i]]-1)/N+1]=(X[Q[i]]-1)%N+'A';
        for(int i=1;i<=N*N;++i)
        {
            putchar(map[i]);
            if((i%N)==0)puts("");
        }
        return 1;
    }
    int i,j,c,tmp=mm;
    for(i=R[0];i;i=R[i])
        if(S[i]<tmp)tmp=S[c=i];
    remove(c);
    for(i=D[c];i!=c;i=D[i])
    {
        Q[k]=i;
        for(j=R[i];j!=i;j=R[j])remove(C[j]);
        if(Dance(k+1))return 1;
        for(j=L[i];j!=i;j=L[j])resume(C[j]);
    }
    resume(c);
    return 0;
}
void Link(int r,int c)
{
    ++S[C[++size]=c];
    X[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 place(int &r,int &c1,int &c2,int &c3,int &c4,int i,int j,int k)
{
    r=(i*N+j)*N+k,c1=i*N+j+1,c2=N*N+i*N+k,c3=N*N*2+j*N+k,c4=N*N*3+((i/4)*4+(j/4))*N+k;
}
bool init()
{
    char c;
    while(((c=getchar())<'A'||c>'P')&&c!='-'&&c!=EOF);
    if(c==EOF)return 0;
    for(int i=0;i<N*N;++i)
    {
        if(i)while(((c=getchar())<'A'||c>'P')&&c!='-');
        map[i]=c;
    }
    return 1;
}
int main()
{
    int i,j,k,r,c1,c2,c3,c4,T=0;
    while(init())
    {
        if(T++)puts("");
        prepare(mn,N*N*4);
        for(i=1;i<=mn;++i)v[i]=0;
        for(k=i=0;i<N;++i)
            for(j=0;j<N;++j,++k)
            if(map[k]!='-')
            {
                place(r,c1,c2,c3,c4,i,j,map[k]-'A'+1);
                Link(r,c1),Link(r,c2),Link(r,c3),Link(r,c4);
                v[c2]=v[c3]=v[c4]=1;
            }
        for(i=0;i<N;++i)
            for(j=0;j<N;++j)
                for(k=1;k<=N;++k)
                {
                    place(r,c1,c2,c3,c4,i,j,k);
                    if(v[c2]||v[c3]||v[c4])continue;
                    Link(r,c1),Link(r,c2),Link(r,c3),Link(r,c4);
                }
        Dance(0);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值