[kuangbin]专题三 Dancing Links Sudoku ZOJ - 3122【精确覆盖】

【题目描述】
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 P such 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.
Sudoku网格是由16x16个单元格组成的网格,由16个4x4方格组成。单元格中填满了A到P(英文字母表前16个大写字母)的字母,如图1a所示。游戏是用A到P的字母填充所有空的网格单元格,这样来自网格的每个字母只在它所占用的行、列和4x4网格中出现一次。网格的初始内容满足上述约束条件,并保证了唯一的解决方案。
在这里插入图片描述
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.
编写一个数独播放程序,从文本文件中读取数据集。每个数据集编码一个网格,并包含16条连续行上的16个字符串,如图2所示。第i个字符串代表网格的第i行,长度为16个字符,从行的第一个位置开始。字符串字符来自集合{A,B,.,P,-},其中-(减号)指定空网格单元格。数据集由单个空行分隔,并以文件结尾结束。该程序以与输入相同的格式和顺序打印输入编码网格的解决方案。

【样例输入】
–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–OE–
F-M–D--L-K-A
-C--------O-I-LH-
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-
上面题面给的样例是错的
正确的样例是这个
–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-

【样例输出】
FPAHMJECNLBDKOGI
OJMIANBDPKCGFLHE
LNDKGFOIJEAHMBPC
BGCELKHPOFIMAJDN
MFHBELPOACKJGNID
CILNKDGAHBMOPEFJ
DOGPIHJMFNLECAKB
JEKAFCNBGIDPLHOM
EBOFPMIJDGHLNKCA
NCJDHBAEKMOFIGLP
HMPLCGKFIAENBDJO
AKIGNODLBPJCEFMH
KDEMJIFNCHGAOPBL
GLBCDPMHEONKJIAF
PHNOBALKMJFIDCEG
IAFJOECGLDPBHMNK

题目链接:https://cn.vjudge.net/problem/ZOJ-3122

POJ1084的升级版,但是其实没什么区别
注意输出格式就好,最后结尾没有空行

代码如下:

#include <iostream>
using namespace std;
static const int MAXN=16*16*16+10;
static const int MAXM=16*16*4+10;
static const int MAXNODE=MAXN*4+MAXM+10;
char g[MAXN];
struct DLX{
    int n,m,size;
    int U[MAXNODE],D[MAXNODE],L[MAXNODE],R[MAXNODE],Row[MAXNODE],Col[MAXNODE];
    int H[MAXN],S[MAXM];
    int ansd,ans[MAXN];
    void init(int _n,int _m)
    {
        n=_n;
        m=_m;
        for(int i=0;i<=m;i++)
        {
            S[i]=0;
            U[i]=D[i]=i;
            L[i]=i-1;
            R[i]=i+1;
        }
        R[m]=0;L[0]=m;
        size=m;
        for(int i=1;i<=n;i++)
            H[i]=-1;
    }
    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 resume(int c)
    {
        for(int i=U[c];i!=c;i=U[i])
            for(int j=L[i];j!=i;j=L[j])
                ++S[Col[U[D[j]]=D[U[j]]=j]];
        L[R[c]]=R[L[c]]=c;
    }
    bool dance(int d)
    {
        if(R[0]==0)
        {
            for(int i=0;i<d;i++)
                g[(ans[i]-1)/16]=(ans[i]-1)%16+'A';
            for(int i=0;i<16;i++)
            {
                for(int j=0;j<16;j++)
                    cout<<g[i*16+j];
                cout<<endl;
            }
            return true;
        }
        int c=R[0];
        for(int i=R[0];i!=0;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]);
            if(dance(d+1))
                return true;
            for(int j=L[i];j!=i;j=L[j])
                resume(Col[j]);
        }
        resume(c);
        return false;
    }
};
int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(0),cout.tie(0);
    int kase=0;
    while(cin>>g)
    {
        if(kase)
            cout<<endl;
        kase++;
        for(int i=1;i<=15;i++)
            cin>>g+i*16;
        DLX dlx;
        dlx.init(16*16*16,16*16*4);
        for(int i=0;i<16;i++)
            for(int j=0;j<16;j++)
                for(int k=1;k<=16;k++)
                    if(g[i*16+j]=='-' || g[i*16+j]=='A'+k-1)
                    {
                        dlx.Link((i*16+j)*16+k,i*16+j+1);
                        dlx.Link((i*16+j)*16+k,16*16+i*16+k);
                        dlx.Link((i*16+j)*16+k,16*16*2+j*16+k);
                        dlx.Link((i*16+j)*16+k,16*16*3+((i/4)*4+(j/4))*16+k);
                    }
        dlx.dance(0);
    }
    return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值