Analysis:
DLX is to optimize the data structure of the 01 matrix accurate coverage and repeated coverage from the point of view, it uses the cross-linked list to store only the non-0 of the Matrix, while the 01 matrix is more and more sparse in the DFS process, and every time the recovery is wasted, DLX happens to solve both problems. The key point is to transform the Sudoku problem into a 01 matrix exact overlay. The method of conversion of Sudoku to exact coverage problem or refer to Knuth's paper, if read to a grid is empty, then add 9 lines, respectively, that the lattice fill 1 to 9 of the 9 numbers, if the reading of the lattice is a number, then add a row is OK, then the column has 9*9*4 column, The first 81 columns indicate that this line indicates that the row is filled with the column J of Line I, the next 81 columns indicate that line i is filled with K, the next 81 columns indicate that column j is filled with K, and the last 81 columns indicate that the corresponding nine is filled with K.
POJ 3074//sep9#include <cstdio> #include <cstdlib> #define Int_max 2147483647using namespace Std;const INT max=1024;const int col_num=9*9*4;const int head=0;const int delta[]={1,82,163,244};int cnt[max],st[max];int left[MAX* Max],right[max*max],up[max*max],down[max*max];int row[max*max],col[max*max];int K,M;//k:node ' s idx M:row ' s Numberstruct ans{int r,c,k;} Ans[max*max];void init () {left[head]=col_num;right[head]=1;up[head]=down[head]=head;for (int i=1;i<=col_num;++i {left[i]=i-1;right[i]= (i+1)% (col_num+1); up[i]=down[i]=i;cnt[i]=0;col[i]=i;row[i]=0;} m=0; K=col_num;} int make_col_head (int c) {++k;++cnt[c];col[k]=c;row[k]=m;left[k]=right[k]=k;up[k]=c;down[k]=down[c];up[down[k]]=k ;d Own[up[k]]=k;return K;} void Addcol (int ids,int c) {++k;++cnt[c];col[k]=c;row[k]=m;left[k]=ids;right[k]=right[ids];left[right[k]]=k;right[ Left[k]]=k;up[k]=c;down[k]=down[c];up[down[k]]=k;down[up[k]]=k;} void addrow (int i,int j,int k) {++m;ans[m].r=i;ans[m].c=j;ans[m].k=k+1;int ids=make_col_head (9*i+j+delTa[0]); Addcol (ids,9*i+k+delta[1]); Addcol (ids,9*j+k+delta[2]); Addcol (ids,9* (I/3*3+J/3) +k+delta[3]);} void remove (int c) {left[right[c]]=left[c];right[left[c]]=right[c];for (int i=down[c];i!=c;i=down[i]) for (int j=right [I];j!=i;j=right[j]) {up[down[j]]=up[j];d own[up[j]]=down[j];--cnt[col[j];}} void Resume (int c) {for (Int. I=up[c];i!=c;i=up[i]) for (int j=left[i];j!=i;j=left[j]) {down[up[j]]=j;up[down[j]]=j;++ CNT[COL[J]];} Left[right[c]]=c;right[left[c]]=c;} bool Dfs (int k) {if (right[head]==head) {char s[128];for (int i=0;i<k;++i) S[ans[st[i]].r*9+ans[st[i]].c]=ans[st[i] . K + ' 0 '; s[81]= ';p UTS (s); return true;} int s=int_max,c=0;for (int i=right[head];i!=head;i=right[i]) {if (cnt[i]<s) {s=cnt[i];c=i;}} Remove (c); for (int i=down[c];i!=c;i=down[i]) {st[k]=row[i];for (int j=right[i];j!=i;j=right[j]) remove (col[j]); DFS (k+1)) return true;for (int j=left[i];j!=i;j=left[j]) resume (col[j]);} Resume (c); return false;} int main () {char s[128];while (scanf ("%s", s) ==1&&s[0]!= ' E ') {init (); for (int. i=0;i<9;++i) for (int J=0;J<9;++J) if (s[i*9+j]== '. ') {for (int k=0;k<9;++k) addrow (i,j,k);} Elseaddrow (i,j,s[i*9+j]-' 1 ');d FS (0);} return 0;}
POJ 3074 Sudoku DLX coefficient alone