(简单) POJ 3074 Sudoku, DLX+精确覆盖。

时间:2023-03-09 08:55:49
(简单) POJ 3074 Sudoku, DLX+精确覆盖。

  Description

  In the game of Sudoku, you are given a large 9 × 9 grid divided into smaller 3 × 3 subgrids. For example,

. 2 7 3 8 . . 1 .
. 1 . . . 6 7 3 5
. . . . . . . 2 9
3 . 5 6 9 2 . 8 .
. . . . . . . . .
. 6 . 1 7 4 5 . 3
6 4 . . . . . . .
9 5 1 8 . . . 7 .
. 8 . . 6 5 3 4 .

  Given some of the numbers in the grid, your goal is to determine the remaining numbers such that the numbers 1 through 9 appear exactly once in (1) each of nine 3 × 3 subgrids, (2) each of the nine rows, and (3) each of the nine columns.

  经典的数独问题,DLX精确覆盖。。。。。。

  构造01矩阵的话,行是9*9*9行,列是4*9*9列;

  其中行的话代表的是对于9*9个格子,每一个都有9种可能。。。。。。

  然后列的话,4是代表9*9个格子每一个填一个,9*9的格子中的行,列,每个3*3的块,这四种都要保证正确。

然后代码如下:

#include<iostream>
#include<cstring> using namespace std; const int MaxN=;
const int MaxM=;
const int MaxNode=MaxN*MaxM; struct DLX
{
int U[MaxNode],D[MaxNode],L[MaxNode],R[MaxNode],col[MaxNode],row[MaxNode];
int size,n,m;
int H[MaxN],S[MaxM];
int ans[],ans1[]; void init(int _n,int _m)
{
n=_n;
m=_m; for(int i=;i<=m;++i)
{
U[i]=D[i]=i;
L[i]=i-;
R[i]=i+;
row[i]=; S[i]=;
}
L[]=m;
R[m]=; size=m; for(int i=;i<=n;++i)
H[i]=-;
} void Link(int r,int c)
{
col[++size]=c;
row[size]=r;
++S[c]; U[size]=U[c];
D[size]=c;
D[U[c]]=size;
U[c]=size; if(H[r]==-)
H[r]=L[size]=R[size]=size;
else
{
L[size]=L[H[r]];
R[size]=H[r];
R[L[H[r]]]=size;
L[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])
{
U[D[j]]=j;
D[U[j]]=j;
++S[col[j]];
} L[R[c]]=R[L[c]]=c;
} void showans(int d)
{
for(int i=;i<d;++i)
ans1[(ans[i]-)/+]=(ans[i]-)%+; for(int i=;i<=;++i)
cout<<ans1[i]; cout<<endl;
} bool Dance(int d)
{
if(R[]==)
{
showans(d);
return ;
} int c=R[]; for(int i=R[];i!=;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+))
return ; for(int j=L[i];j!=i;j=L[j])
resume(col[j]);
} resume(c); return ;
} void display()
{
for(int i=R[];i!=;i=R[i])
{
cout<<i<<' ';
for(int j=D[i];j!=i;j=D[j])
cout<<'('<<j<<','<<(row[j]-)%+<<')'<<' '; cout<<endl;
}
}
}; DLX dlx;
char s[]; void slove()
{
dlx.init(,); for(int i=;i<=;++i)
for(int j=;j<=;++j)
dlx.Link(j+(i-)*,i); for(int i=;i<=;++i)
for(int j=;j<=;++j)
dlx.Link(*(j-)+(i-)%++*((i-)/),i+); for(int i=;i<=;++i)
for(int j=;j<=;++j)
dlx.Link((j-)*+i,i+); for(int i=;i<=;++i)
for(int j=;j<=;++j)
for(int k=;k<=;++k)
for(int l=;l<=;++l)
for(int m=;m<=;++m)
dlx.Link((i-)*+(j-)*+k+(l-)*+(m-)*,(i-)*+(j-)*+k+); for(int i=;i<;++i)
if(s[i]!='.')
{
dlx.ans1[i+]=s[i]-''; dlx.remove(i+); for(int j=dlx.D[i+];j!=i+;j=dlx.D[j])
{
if((dlx.row[j]-)%+==s[i]-'')
{
for(int k=dlx.R[j];k!=j;k=dlx.R[k])
dlx.remove(dlx.col[k]); break;
}
}
} dlx.Dance();
} int main()
{
ios::sync_with_stdio(false); for(cin>>s;s[]!='e';cin>>s)
slove(); return ;
}