Title Link: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3209
Dancing links each point as a column.
Each block acts as a row.
I don't know why when I plug a node, if I write a loop
for (int i = y1+1; I <= y2; i++) { //Line for (int j = x1+1; J <= X2; j + +) {//Column
} }
Then col = (y-1) *n+x will time out.
1#include <iostream>2#include <cstring>3#include <cstdio>4#include <string>5#include <iomanip>6 using namespacestd;7 intT, N, M, p, x1, y1, x2, y2;8 #defineMAXN 510*35*359 intR[MAXN], L[MAXN], U[MAXN], D[MAXN];Ten intH[MAXN], C[MAXN], colsum[999]; One inthead, cnt, ans; A voidAddNodeintIintJintHintRowhead,intSumintpre) { - intCol = j+ (i-1) *m;//cout<<col<< ""; -cnt++; the -H[CNT] = h; C[CNT] =Col; - if(Sum = =1){ -R[CNT] = cnt; L[CNT] =CNT; + } - Else if(Sum = =2){ +R[pre] = cnt; R[CNT] =Pre; AL[pre] = cnt; L[CNT] =Pre; at } - Else{ -R[CNT] = Rowhead; R[pre] =CNT; -L[CNT] = pre; L[rowhead] =CNT; - } - inD[u[col]] =CNT; -U[CNT] =U[col]; toD[CNT] =Col; +U[col] =CNT; - thecolsum[col]++; * $ }Panax Notoginseng voidinit () { -Head = CNT =0; theAns =510;//It 's at most 500 bucks. +R[head] =1; L[head] = n*m; U[head] = head; D[head] =head; AH[head] =0; C[head] =0; thememset (Colsum,0,sizeof(Colsum)); + for(inti =1; I <= n*m; i++){ -cnt++; $U[CNT] = d[cnt] =CNT; $H[CNT] =0; C[CNT] =i; - if(i = = nm) { -r[cnt-1] = CNT; L[CNT] = cnt-1; R[CNT] =head; the } - Else{Wuyir[cnt-1] = CNT; L[CNT] = cnt-1; the } - } Wu for(intCC =1; CC <= p; Cc++){ -scanf"%d%d%d%d", &x1, &y1, &X2, &y2); About intsum =0, Rowhead =-1, pre; $ for(inti = x1+1; I <= x2; i++) {//Line - for(intj = y1+1; J <= Y2; J + +) {//column -sum++; - AddNode (i, J, CC, Rowhead, sum, pre); A if(Rowhead = =-1) Rowhead =CNT; +Pre =CNT; the } - } $ } the } the voidRemoveintc) { theR[L[C]] = r[c]; L[R[C]] =L[c]; the for(inti = d[c]; I! = C; i =D[i]) { - for(intj = R[i]; J! = i; j =R[j]) { inU[D[J]] = u[j]; D[U[J]] =D[j]; thecolsum[c[j]]--; the } About } the } the voidResumeintc) { theR[L[C]] = c; L[R[C]] =C; + for(inti = d[c]; I! = C; i =D[i]) { - for(intj = R[i]; J! = i; j =R[j]) { theU[D[J]] = j; D[U[J]] =J;Bayicolsum[c[j]]++; the } the } - } - voidDanceintk) { the if(k >= ans)return; the intc =R[head]; the if(c = =head) { theAns =K; - return; the } the intMin =9999999; the for(inti = R[head]; I! = head; i =R[i]) {94 if(Colsum[i] <=min) { themin = Colsum[i]; c =i; the } the }98 Remove (c); About for(inti = d[c]; I! = C; i =D[i]) { - for(intj = R[i]; J! = i; j =R[j]) remove (c[j]);101Dance (k +1);102 for(intj = L[i]; J! = i; j = L[j]) resume (c[j]);//to be written in L103 }104 resume (c); the return ;106 }107 intMain () {108scanf"%d", &T);109 while(t--){ thescanf"%d%d%d", &n, &m, &p);//n is the column, and M is the row. 111 init (); theDance0);113 if(ans = =510) printf ("-1\n"); the Elseprintf"%d\n", ans); the } the 117 return 0;118}
ZOJ 3209 Treasure Map