Title Link: http://poj.org/problem?id=3740
Dancing Links Entry Questions
1#include <iostream>2#include <cstring>3#include <cstdio>4#include <string>5#include <iomanip>6 using namespacestd;7 intM, N;8 #defineMAXN 16*300+59 intR[MAXN], L[MAXN], U[MAXN], D[MAXN];Ten intH[MAXN], C[MAXN], ans[ -], colsum[ -+5]; One intmp[ -][310]; A intcnt, head; - voidAddNodeintRowintColintSumintPreintrowhead) { -cnt++; theH[CNT] =Row; -C[CNT] =Col; - - if(Pre = =-1){ +R[CNT] = cnt; L[CNT] =CNT; - } + Else if(Sum = =2){ AR[pre] = cnt; R[CNT] =Pre; atL[pre] = cnt; L[CNT] =Pre; - } - Else{ -R[CNT] = Rowhead; R[pre] =CNT; -L[CNT] = pre; L[rowhead] =CNT; - } in -D[u[col]] =CNT; toU[CNT] =U[col]; +D[CNT] =Col; -U[col] =CNT; the *colsum[col]++; $ }Panax Notoginseng voidinit () { -CNT =0; Head =0; thememset (Colsum,0,sizeof(Colsum)); +L[head] = N; R[head] =1; D[head] = head; U[head] =head; A for(inti =1; I <= N; i++){ thecnt++; +H[CNT] =0; C[CNT] =i; -U[CNT] = cnt; D[CNT] =CNT; $ if(i = =N) { $r[cnt-1] =CNT; -L[CNT] = cnt-1; -R[CNT] =head; the } - Else{Wuyir[cnt-1] =CNT; theL[CNT] = cnt-1; - } Wu } - inttemp; About for(inti =1; I <= M; i++){ $ intsum =0, Pre =-1, Rowhead; - for(intj =1; J <= N; J + +){ -scanf"%d", &temp); - if(temp = =1){ Asum++; + AddNode (i, J, Sum, Pre, rowhead); the if(Sum = =1) Rowhead =CNT; -Pre =CNT; $ } the } the } the } the voidRemoveintc) { -R[L[C]] =R[c]; inL[R[C]] =L[c]; the for(inti = d[c]; I! = C; i =D[i]) { the for(intj = R[i]; J! = i; j =R[j]) { AboutU[D[J]] =U[j]; theD[U[J]] =D[j]; thecolsum[c[j]]--; the } + } - } the voidResumeintc) {BayiR[L[C]] =C; theL[R[C]] =C; the for(inti = u[c]; I! = C; i =U[i]) { - for(intj = R[i]; J! = i; j =R[j]) { -U[D[J]] =J; theD[U[J]] =J; thecolsum[c[j]]++; the } the } - } the BOOLDanceintk) { the intc =R[head]; the if(c = =head) {94 return true; the } the intMin =999999; the for(inti = R[head]; I! = head; i =R[i]) {98 if(Colsum[i] <=min) { AboutMin =Colsum[i]; -c =i;101 }102 }103 Remove (c);104 for(inti = d[c]; I! = C; i =D[i]) { theANS[K] =H[i];106 for(intj = R[i]; J! = i; j =R[j]) remove (c[j]);107 if(Dance (k +1))return true;108 for(intj = R[i]; J! = i; j =R[j]) resume (c[j]);109 } the resume (c);111 return false; the }113 the intMain () { the while(~SCANF ("%d%d", &m, &N)) { the init ();117 if(Dance (0)) printf ("Yes, I found it\n");118 Elseprintf"It is impossible\n");119 } - 121 return 0;122}
POJ 3740 Easy Finding