In fact Dance links is just a data structure, Dance links is an algorithm. Dacing links x is an efficient algorithm for solving this kind of problem, and this algorithm, based on the cross-cross-loop bidirectional
Linked list. The following are two-way cross-linked lists:
Here is a bare topic that uses this algorithm template:
Exact Cover
Description:
There is a n*m matrix with only 0s and 1s, (1 <= n,m <= 1000). An exact cover are a selection of rows such that every column have a 1 in exactly one of the selected rows. Try to find out the selected rows.
Sample Input:
6 7
3 1 4 7
2 1 4
3 4 5 7
3 3 5 6
4 2 3) 6 7
2 2 7
Sample Output:
3 2 4 6
Reference code:
1#include <stdio.h>2#include <iostream>3 using namespacestd;4 Const intMaxnode = +*Ten+Ten;5 Const intMAXN = ++Ten;6 Const intMAXM = ++Ten;7 8 structDLX {9 intN,m, size;//n rows, number of M columns, number of Szie nodesTen intU[maxnode], D[maxnode], L[maxnode], R[maxnode], Col[maxnode], Row[maxnode]; One intH[MAXN], S[MAXM];//H[i] First node of row I, s[j] number of nodes in column J A intANSD, ANS[MAXN];//ANSD The number of rows contained in the solution, ans[] solution - - voidInitint_n,int_m)//Initialize the list of cross-links the { -n =_n; -m =_m; - for(inti =0; I <= m; i++) + { -Col[i] = i; Row[i] =0; +U[i] = D[i] =i; AL[i] = i +1; R[i] = i-1; atS[i] =0; - } -r[0] = m; L[M] =0; -Size =m; - for(inti =1; I <= N; i++) -H[i] =-1; in } - voidLinkintRintC//Insert a node in row r, column C to {//note, here is the direction downward +size++; -Col[size] =C; theRow[size] =R; *s[c]++; $D[size] =D[c];Panax NotoginsengU[D[C]] =size; -U[size] =C; theD[C] =size; + if(H[r] = =-1) AH[R] = r[size] = L[size] =size; the Else + { -R[size] =R[h[r]]; $L[r[h[r]] =size; $L[size] =H[r]; -R[h[r]] =size; - } the } - voidRemoveintC//remove rows from column C and the nodes on the columnWuyi { theL[R[C]] =L[c]; -R[L[C]] =R[c]; Wu for(inti = d[c]; I! = C; i =D[i]) - for(intj = L[i]; J! = i; j =L[j]) About { $D[U[J]] =D[j]; -U[D[J]] =U[j]; ---S[col[j]]; - } A } + voidResumeintC//restores the row in column C and the nodes on the column, just opposite to remove the { - for(inti = u[c];i! = C;i =U[i]) $ for(intj = R[i]; J! = i; j =R[j]) the { the++S[col[j]]; theD[U[J]] =J; theU[D[J]] =J; - } inL[R[C]] =C; theR[L[C]] =C; the } About BOOLDance (intD//d is the depth of the search the { the if(r[0] ==0) the { +ANSD =D; - return true; the }Bayi intc = r[0]; the for(inti = r[0]; I! =0; i =R[i]) the if(S[i] <S[c]) -c =i; - Remove (c); the for(inti = d[c]; I! = C; i = D[i])//try individually the { theANS[D] =Row[i]; the for(intj = R[i]; J! = i; j =R[j]) - Remove (col[j]); the if(Dance (d +1))return true; the for(intj = L[i]; J! = i; j =L[j]) the Resume (col[j]);94 } theResume (c);//this dispensable, written just for completeness the return false; the }98 }; About - DLX G;101 intMain ()102 {103 intN, M;104 the while(SCANF ("%d%d", &n,&m) = =2)106 {107 g.init (n, m);108 for(inti =1; I <= N; i++)109 { the intnum, J;111scanf"%d", &num); the while(num--)113 { thescanf"%d", &j); the G.link (i, j); the }117 }118 if(!g.dance (0))119printf"no\n"); - Else121 {122printf"%d", G.ANSD);123 for(inti =0; i < G.ANSD; i++)124printf"%d", G.ans[i]); theprintf"\ n");126 }127 } - return 0;129}
There is nothing wrong with the place, thank you for advice.
Dance links algorithm