数独16*16的题目和9*9一样 DLX 精确覆盖的题目。
Sudoku
Time Limit: 10000MS | Memory Limit: 65536K | |
Total Submissions: 3777 | Accepted: 1840 |
Description
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.
Write a Sudoku playing program that reads data sets from a text file.
Write a Sudoku playing program that reads data sets from a text file.
Input
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.
Output
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
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<algorithm>
using namespace std;
const int MAXN=1000000;
#define INF 0xFFFFFF
int size;
int head,sz;
int U[MAXN],D[MAXN],L[MAXN],R[MAXN];
int H[MAXN],ROW[MAXN],C[MAXN],S[16*16*4+20],O[16*16*4+20];
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[j]];
U[D[j]]=j;
D[U[j]]=j;
}
}
L[R[c]]=c;
R[L[c]]=c;
}
bool dfs(int k)
{
if(R[head]==head)
{
sort(O,O+16*16);
int p=0;
for(int i=0;i<16;i++)
{
for(int j=0;j<16;j++)
{
int num=O[p++];
num=num-(i*16+j)*16;
printf("%c",num+'A'-1);
}
printf("\n");
}
printf("\n");
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]=ROW[i];
for(int j=R[i];j!=i;j=R[j])
remove(C[j]);
if(dfs(k+1))
return true;
for(int j=L[i];j!=i;j=L[j])
resume(C[j]);
}
resume(c);
return false;
}
void initDL(int n)
{
head=0;
for(int i=0;i<=n;i++)
{
U[i]=i;D[i]=i;
L[i]=i-1;R[i]=i+1;
S[i]=0;
}
R[n]=0;L[0]=n;S[0]=INF+1;
sz=n+1;
memset(H,0,sizeof(H));
}
void insert(int i, int j)
{
if(H[i])
{
L[sz]=L[H[i]];
R[sz]=H[i];
L[R[sz]]=sz;
R[L[sz]]=sz;
}
else
{
L[sz]=sz;
R[sz]=sz;
H[i]=sz;
}
U[sz]=U[j];
D[sz]=j;
U[D[sz]]=sz;
D[U[sz]]=sz;
C[sz]=j;
ROW[sz]=i;
++S[j];
++sz;
}
char str[200][200];
void build()
{
initDL(16*16*4);
for(int i=0;i<16;i++)
for(int j=1;j<=16;j++)
{
int base=(i*16+j-1)*16;
if(str[i][j-1]=='-')
{
for(int k=1;k<=16;k++)
{
int r;
r=base+k;
//第i行有数字k
insert(r,i*16+k);
//第j列有数字k
insert(r,16*16+(j-1)*16+k);
//第k块有数字k
int block=(j-1)/4*4+i/4;
insert(r,16*16*2+block*16+k);
//第i行j列有一个数字(限制一个格子只填一个数)
insert(r,16*16*3+i*16+j);
}
}
else
{
int k=str[i][j-1]-'A'+1;
int r=base+k;
//第i行有数字k
insert(r,i*16+k);
//第j列有数字k
insert(r,16*16+(j-1)*16+k);
//第k块有数字k
int block=(j-1)/4*4+i/4;
insert(r,16*16*2+block*16+k);
//第i行j列有一个数字(限制一个格子只填一个数)
insert(r,16*16*3+i*16+j);
}
}
}
int main()
{
size=9; //9*9数独
while(~scanf("%s",str[0]))
{
gets(str[1]);
for(int i=1;i<16;i++) gets(str[i]);
build();
dfs(0);
}
return 0;
}