Sudoku
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. 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 Source |
解题报告: Dancing Link题,改下9*9的数独代码就过了。代码如下:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int M=4096*5;
int l[M], r[M], d[M], u[M], col[M], row[M], h[4100], control[1030];
int dcnt = 0;
char str[16][17];
inline void addnode(int &x)
{
++x;
r[x]=l[x]=u[x]=d[x]=x;
}
inline void insert_row(int rowx, int x)
{
r[l[rowx]]=x;
l[x]=l[rowx];
r[x]=rowx;
l[rowx]=x;
}
inline void insert_col(int colx, int x)
{
d[u[colx]]=x;
u[x]=u[colx];
d[x]=colx;
u[colx]=x;
}
void dlx_init(int cols)
{
memset(h, -1, sizeof(h));
memset(control, 0, sizeof(control));
dcnt=-1;
addnode(dcnt);
for(int i=1;i<=cols;++i)
{
addnode(dcnt);
insert_row(0, dcnt);
}
}
inline 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];
control[col[j]]--;
}
}
inline void resume(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]]=j;
d[u[j]]=j;
control[col[j]]++;
}
l[r[c]]=c;
r[l[c]]=c;
}
bool DLX(int deep)
{
if(r[0]==0)
{
for(int i=0;i<16;i++)
puts(str[i]);
return true;
}
int min=M, tempc;
for(int i=r[0];i!=0;i=r[i]) if(control[i]<min)
{
min=control[i];
tempc=i;
}
remove(tempc);
for(int i=d[tempc];i!=tempc;i=d[i])
{
str[row[i]/256][row[i]/16%16]=row[i]%16+'A';
for(int j=r[i];j!=i;j=r[j]) remove(col[j]);
if(DLX(deep+1)) return true;
for(int j=l[i];j!=i;j=l[j]) resume(col[j]);
}
resume(tempc);
return false;
}
inline void insert_node(int x, int y)
{
control[y]++;
addnode(dcnt);
row[dcnt]=x;
col[dcnt]=y;
insert_col(y, dcnt);
if(h[x]==-1)
h[x]=dcnt;
else
insert_row(h[x], dcnt);
}
int main()
{
#ifdef ACM
freopen("in.txt", "r", stdin);
#endif
while(~scanf("%s", str[0]))
{
for(int i=1;i<16;i++) scanf("%s", str[i]);
dlx_init(4*16*16);
for(int i=1;i<=16;i++) for(int j=1;j<=16;j++)
{
for(int k=1;k<=16;k++) if(str[i-1][j-1]=='-' || str[i-1][j-1]=='A'+(k-1))
{
int rr=(i-1)*256+(j-1)*16+(k-1);
insert_node(rr, 16*16*0+(i-1)*16+k);
insert_node(rr, 16*16*1+(j-1)*16+k);
insert_node(rr, 16*16*2+(i-1)/4+(j-1)/4*4+(k-1)*16+1);
insert_node(rr, 16*16*3+(i-1)*16+j);
}
}
DLX(0);
puts("");
}
}