Poj 3740 easy finding DLX

Source: Internet
Author: User

Question: Give You A 0, 1 matrix for exact coverage

Solution:

DLX

Solution code:

1 // file name: poj3740.cpp 2 // Author: darkdream 3 // created time: saturday, October 04, 2014, 31 seconds, 4 5 # include <vector> 6 # include <list> 7 # include <map> 8 # include <set> 9 # include <deque> 10 # include <stack> 11 # include <bitset> 12 # include <algorithm> 13 # include <functional> 14 # include <numeric> 15 # include <utility> 16 # include <sstream> 17 # include <iostream> 18 # include <iomanip> 19 # include <cstdio> 20 # I Nclude <cmath> 21 # include <cstdlib> 22 # include <cstring> 23 # include <ctime> 24 # define ll long 25 26 using namespace STD; 27 const int maxnode = 50000; 28 const int maxn = 18; 29 const int maxm = 400; 30 struct DLX 31 {32 int n, m, size; 33 int U [maxnode], d [maxnode], L [maxnode], R [maxnode], row [maxnode], Col [maxnode]; 34 int H [maxn], s [maxm]; 35 void Init (INT _ n, int _ m) 36 {37 n = _ n; 38 m = _ m; 39 for (INT I = 0; I <= m; I ++) 40 {41 s [I] = 0; 42 U [I] = I; 43 d [I] = I; 44 L [I] = I-1; 45 R [I] = I + 1; 46} 47 size = m; 48 L [0] = m; 49 R [m] = 0; 50 memset (H,-1, sizeof (h); 51} 52 void Link (int r, int c) 53 {54 + + s [col [++ size] = C]; 55 row [size] = r; 56 d [size] = d [c]; 57 U [d [c] = size; 58 U [size] = C; 59 d [c] = size; 60 if (H [R] <0) H [R] = L [size] = R [size] = size; 61 else {62 R [siz E] = R [H [R]; 63 l [R [H [R] = size; 64 L [size] = H [R]; 65 R [H [R] = size; 66} 67} 68 void remove (INT c) 69 {70 L [R [c] = L [c]; 71 R [L [c] = R [c]; 72 for (INT I = d [c]; I! = C; I = d [I]) 73 for (Int J = R [I]; J! = I; j = R [J]) 74 {75 U [d [J] = U [J]; 76 d [U [J] = d [J]; 77 -- s [col [J]; 78} 79} 80 void resume (INT c) 81 {82 L [R [c] = C; 83 R [L [c] = C; 84 For (INT I = d [c]; I! = C; I = d [I]) 85 for (Int J = R [I]; J! = I; j = R [J]) 86 {87 U [d [J] = J; 88 d [U [J] = J; 89 + + s [col [J]; 90} 91} 92 bool Dance () 93 {94 If (R [0] = 0) 95 {96 return true; 97} 98 int c = R [0]; 99 for (INT I = R [c]; I! = 0; I = R [I]) 100 if (s [I] <s [c]) 101 C = I; 102 remove (C ); 103 for (INT I = d [c]; I! = C; I = d [I]) 104 {105 for (Int J = R [I]; J! = I; j = R [J]) Remove (COL [J]); 106 If (Dance () return true; 107 for (Int J = R [I]; j! = I; j = R [J]) Resume (COL [J]); 108} 109 resume (c); 110 return false; 111} 112}; 113 114 DLX g; 115 int main () {116 int n, m; 117 while (scanf ("% d", & N, & M )! = EOF) 118 {119G. init (n, m); 120 int t; 121 for (INT I = 1; I <= N; I ++) 122 for (Int J = 1; j <= m; j ++) 123 {124 scanf ("% d", & T); 125 If (t) 126G. link (I, j); 127} 128 If (G. dance () 129 printf ("Yes, I found it \ n"); 130 else printf ("It is impossible \ n"); 131} 132 return 0; 133}
View code

 

Poj 3740 easy finding DLX

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.