Spoj 1771 & DLX precise coverage, repeated coverage

Source: Internet
Author: User

This is the answer to the DLX question.

This question is transformed into a precise coverage model. At the beginning, we simply need to cover the rows and slashes, WA.

Later, I realized that this is not the case. I only need to overwrite all rows or columns. However, some details cannot be taken into consideration.

Note that

for(int i=R[c];i;i=R[i]){ if(i>ne) break; if(S[i] < S[c]) c = i;}

The minimum value can only be found before Ne. Why? Because we need to completely overwrite the rows. Is it feasible? Feasible. Just pay attention to the template of DLX. After selecting a column, it will enumerate the row values of the column,

That is to say, every row of this column (representing a row on the board) will be taken into account, so you don't have to worry about the solution.

The DLX algorithm is clever. In fact, it is just an efficient pruning algorithm. It's amazing. After doing this, you can really understand this algorithm.

# Include <cstdio> # include <iostream> # include <cstring> # include <algorithm> # include <vector> using namespace STD; const int maxn = 500; const int maxnode = 500*2500; int ne; int anst [maxn]; struct DLX {int N, SZ; // number of rows, total number of nodes int s [maxn]; // total number of nodes in each column int row [maxnode], Col [maxnode]; // number of nodes in the row and column int L [maxnode], R [maxnode], U [maxnode], d [maxnode]; // cross linked list int anSd, ANS [maxn]; // returns void Init (int n) {This-> N = N; For (int I = 0; I <= N; I ++) {u [I] = I; d [I] = I; L [I] = I-1; R [I] = I + 1;} R [N] = 0; L [0] = N; SZ = n + 1; memset (S, 0, sizeof (s);} void addrow (int r, vector <int> C1) {int first = SZ; For (INT I = 0; I <c1.size (); I ++) {int c = c1 [I]; L [SZ] = SZ-1; R [SZ] = SZ + 1; d [SZ] = C; U [SZ] = U [c]; d [U [c] = SZ; U [c] = SZ; row [SZ] = R; col [SZ] = C; s [c] ++; SZ ++;} R [SZ-1] = first; L [first] = SZ-1;} // follow linked list A and traverse other elements except s # define for (I, A, S) for (INT I = A [s]; i! = S; I = A [I]) void remove (INT c) {L [R [c] = L [c]; R [L [c] = R [c]; for (I, D, C) for (J, R, I) {u [d [J] = U [J]; d [U [J] = d [J]; -- s [col [J];} void restore (INT c) {for (I, U, c) for (J, l, I) {++ s [col [J]; U [d [J] = J; d [U [J] = J;} l [R [c] = C; R [L [c] = C;} bool DFS (INT d) {If (D> = ne) {anSd = D; For (INT I = 0; I <ne; I ++) {int x = (ANS [I]-1)/Ne + 1; int y = (ANS [I]-1) % Ne + 1; anst [x] = y;} printf ("% d", anst [1]); For (INT I = 2; I <= ne; I ++) printf ("% d", anst [I]); printf ("\ n"); Return true ;} // find the smallest column of S, C int c = R [0]; for (INT I = R [c]; I; I = R [I]) {if (I> Ne) break; If (s [I] <s [c]) C = I;} remove (c); For (I, D, C) {ans [d] = row [I]; for (J, R, I) Remove (COL [J]); If (DFS (D + 1) return true; for (J, l, I) Restore (COL [J]);} restore (c); Return false;} void solve () {DFS (0 );}}; DLX solver; int puzzle [100] [100]; int main () {int TMP; while (scanf ("% D ", & ne )! = EOF) {memset (puzzle, 0, sizeof (puzzle); For (int K = 1; k <= ne; k ++) {scanf ("% d ", & TMP); If (TMP> 0) {for (INT I = 1; I <= ne; I ++) puzzle [k] [I] = puzzle [I] [TMP] =-1; for (INT I = 1; k-I> 0 & TMP-I> 0; I ++) puzzle [k-I] [TMP-I] =-1; for (INT I = 1; K + I <= ne & TMP + I <= ne; I ++) puzzle [K + I] [TMP + I] =-1; for (INT I = 1; k-I> 0 & TMP + I <= ne; I ++) puzzle [k-I] [TMP + I] =-1; for (INT I = 1; k + I <= ne & TMP-I> 0; I ++) puzzle [K + I] [TMP-I] =-1; puzzle [k] [TMP] = 1 ;}} solver. init (6 * ne-2); vector <int> Columns; For (INT I = 1; I <= ne; I ++) {for (Int J = 1; j <= ne; j ++) {columns. clear (); If (puzzle [I] [J]> = 0) {columns. push_back (I); columns. push_back (Ne + J); columns. push_back (ne * 2 + J-1 + I); columns. push_back (ne * 2 + 2 * ne-1 + NE-I + J); solver. addrow (I-1) * Ne + J, columns) ;}} solver. solve ();} return 0 ;}

  

 

Pick http://www.cnblogs.com/jh818012/p/3252154.html

Duplicate overwrite Template

Const int maxn = 360000; const int maxc = 500; const int maxr = 500; const int INF = 0x3f3f3f3f; int L [maxn], R [maxn], d [maxn], U [maxn], C [maxn]; int s [maxc], H [maxr], size; // The s domain void Link (int r, int C) is not required) {s [c] ++; C [size] = C; U [size] = U [c]; d [U [c] = size; d [size] = C; U [c] = size; If (H [R] =-1) H [R] = L [size] = R [size] = size; else {L [size] = L [H [R]; R [L [H [R] = size; R [size] = H [R]; L [H [R] = size;} size ++ ;} void remove (INT c ){ For (INT I = d [c]; I! = C; I = d [I]) L [R [I] = L [I], R [L [I] = R [I];} void resume (INT c) {for (INT I = U [c]; I! = C; I = U [I]) L [R [I] = R [L [I] = I;} int H () {// use exact overwrite to estimate pruning int ret = 0; bool vis [maxc]; memset (VIS, false, sizeof (VIS )); for (INT I = R [0]; I; I = R [I]) {If (vis [I]) continue; RET ++; vis [I] = true; for (Int J = d [I]; J! = I; j = d [J]) for (int K = R [J]; k! = J; k = R [k]) vis [C [k] = true;} return ret;} int ans; void Dance (int K) {// select to limit the search depth or directly solve the problem based on the specific problem. A * algorithm. Here we only need to find the optimal solution if (K + H ()> = ans) return; If (! R [0]) {If (k <ans) ans = K; return;} int c = R [0]; for (INT I = R [0]; I; I = R [I]) if (s [I] <s [c]) C = I; for (INT I = d [c]; I! = C; I = d [I]) {remove (I); For (Int J = R [I]; J! = I; j = R [J]) Remove (j); Dance (k + 1); For (Int J = L [I]; J! = I; j = L [J]) Resume (j); resume (I) ;}return ;}void initl (INT X) {// col is 1 ~ X, row start from 1 for (INT I = 0; I <= x; ++ I) {s [I] = 0; d [I] = U [I] = I; L [I + 1] = I; R [I] = I + 1 ;} /// initialize R [x] = 0; size = x + 1; // The real elements start from m + 1 memset (H,-1, sizeof (h); // mark the name of each position} DLX overwrites the Template

 

Precise coverage Template

Struct DLX {int N, SZ; // number of rows, total number of nodes int s [maxn]; // total number of nodes in each column int row [maxnode], Col [maxnode]; // number of nodes in the row and column: int L [maxnode], R [maxnode], U [maxnode], d [maxnode]; // cross linked list: int anSd, ANS [maxn]; // solution void Init (int n) {This-> N = N; For (INT I = 0; I <= N; I ++) {u [I] = I; d [I] = I; L [I] = I-1; R [I] = I + 1 ;} R [N] = 0; L [0] = N; SZ = n + 1; memset (S, 0, sizeof (s);} void addrow (int r, vector <int> C1) {int fi RST = SZ; For (INT I = 0; I <c1.size (); I ++) {int c = c1 [I]; L [SZ] = SZ-1; R [SZ] = SZ + 1; d [SZ] = C; U [SZ] = U [c]; d [U [c] = SZ; U [c] = SZ; row [SZ] = r; Col [SZ] = C; s [c] ++; SZ ++ ;} R [SZ-1] = first; L [first] = SZ-1;} // follow the chain table A and traverse other elements except s # define for (I,, s) for (INT I = A [s]; I! = S; I = A [I]) void remove (INT c) {L [R [c] = L [c]; R [L [c] = R [c]; for (I, D, C) for (J, R, I) {u [d [J] = U [J]; d [U [J] = d [J]; -- s [col [J];} void restore (INT c) {for (I, U, c) for (J, l, I) {++ s [col [J]; U [d [J] = J; d [U [J] = J;} l [R [c] = C; R [L [c] = C;} bool DFS (INT d) {If (R [0] = 0) {anSd = D; return true ;} // find the smallest column C int c = R [0]; for (I, R, 0) if (s [I] <s [c]) C = I; remove (c); For (I, D, C) {ans [d] = row [I]; for (J, R, I) Remove (COL [J]); If (DFS (D + 1) return true; For (J, l, I) restore (COL [J]);} restore (c); Return false;} bool solve (vector <int> & V) {v. clear (); If (! DFS (0) return false; For (INT I = 0; I <anSd; I ++) v. push_back (ANS [I]); Return true ;}}; DLX solver; int main () {int n, m; while (scanf ("% d ", & N, & M )! = EOF) {solver. init (m); int C, X; vector <int> C1; For (INT I = 1; I <= N; I ++) {scanf ("% d ", & C); c1.clear (); For (Int J = 0; j <C; j ++) {scanf ("% d", & X ); c1.push _ back (x);} solver. addrow (I, C1) ;}vector <int> ans; bool flag; flag = solver. solve (ANS); If (FLAG) {int size1 = ans. size (); printf ("% d", size1); For (INT I = 0; I <size1; I ++) printf ("% d ", ans [I]); printf ("\ n");} else printf ("NO \ n");} return 0 ;}

  

 

Spoj 1771 & DLX precise coverage, repeated coverage

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.