ZOJ 3209 Treasure Map (precise DLX coverage)

Source: Internet
Author: User
Tags bitset x2 y2

ZOJ 3209 Treasure Map (precise DLX coverage)

Your boss once had got extends copies of a treasure map. unfortunately, all the copies are now broken to your rectangular pieces, and what make it worse, he has lost some of the pieces. luckily, it is possible to figure out the position of each piece in the original map. now the boss asks you, the talent programmer, to make a complete treasure map with these pieces. you need to make only one complete map and it is not necessary to use all the pieces. but remember, pieces are not allowed to overlap with each other (See sample 2 ).


The first line of the input contains an integerT(T<= 500), indicating the number of instances.

For each case, the first line contains three integersN m p(1 <=N,M<= 30, 1 <=P<= 500), the width and the height of the map, and the number of pieces. ThenPLines follow, each consists of four integersX1 y1 x2 y2(0 <=X1<X2<=N, 0 <=Y1<Y2<=M), Where (x1, y1) is the coordinate of the lower-left corner of the rectangular piece, and (x2, y2) is the coordinate of the upper-right corner in the original map.

Cases are separated by one blank line.



If you can make a complete map with these pieces, output the least number of pieces you need to achieve this. If it is impossible to make one complete map, just output-1.

Sample Input

35 5 10 0 5 55 5 20 0 3 52 0 5 530 30 50 0 30 100 10 30 200 20 30 300 0 15 3015 0 30 30

Sample Output



For sample 1, the only piece is a complete map.

For sample 2, the two pieces may overlap with each other, so you can not make a complete treasure map.

For sample 3, you can make a map by either use the first 3 pieces or the last 2 pieces, and the latter approach one needs less pieces.


Q: I want to tell you a map fragment of p. You can build a map of n * m. The fragments cannot overlap. Fragments are not required

Run out. DLX practice: the size of n * m is divided into n * m squares as M, and p map fragments as N. Each map Fragment

It can cover several squares, so it is converted into the precise coverage problem of DLX. You can set up a template with precise DLX coverage.


           using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )typedef long long LL;typedef pair
            pil;const int maxn = 1100;const int maxnode = 500010;const int mod = 1000000007;struct DLX{ int n,m,size; int U[maxnode],D[maxnode],R[maxnode],L[maxnode],Row[maxnode],Col[maxnode]; int H[maxn],S[maxn]; int ansd; void init(int a,int b) { n = a; m = b; for(int i = 0;i <= m;i++) { S[i] = 0; U[i] = D[i] = i; L[i] = i-1; R[i] = i+1; } R[m] = 0; L[0] = m; size = m; for(int i = 1;i <= n;i++) H[i] = -1; } void Link(int r,int c) { ++S[Col[++size]=c]; Row[size] = r; D[size] = D[c]; U[D[c]] = size; U[size] = c; D[c] = size; if(H[r] < 0)H[r] = L[size] = R[size] = size; else { R[size] = R[H[r]]; L[R[H[r]]] = size; L[size] = H[r]; R[H[r]] = size; } } void remove(int c) { L[R[c]] = L[c]; R[L[c]] = R[c]; for(int i = D[c];i != c;i = D[i]) for(int j = R[i];j != i;j = R[j]) { U[D[j]] = U[j]; D[U[j]] = D[j]; --S[Col[j]]; } } void resume(int c) { for(int i = U[c];i != c;i = U[i]) for(int j = L[i];j != i;j = L[j]) ++S[Col[U[D[j]]=D[U[j]]=j]]; L[R[c]] = R[L[c]] = c; } void Dance(int d) { if(ansd!=-1&&d>ansd) return ; if(R[0]==0) { if(ansd==-1) ansd=d; else if(ansd>d) ansd=d; return ; } int c=R[0]; for(int i=R[0];i!=0;i=R[i]) { if(S[i]


Related Article

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.