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.


zoj 3122(数独 Dancing Links精确覆盖)_第1张图片

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;
}


你可能感兴趣的:(c,String,input,constraints,each)