[ACM] ZOJ 3209 Treasure Map (Dancing Links precision Overlay, rectangle overlay)

Source: Internet
Author: User
Tags time limit x2 y2

Treasure Map Time Limit:2 Seconds Memory limit:32768 KB

Your boss once had got many copies of a treasure map. Unfortunately, all the copies is now broken to many rectangular pieces, and what do it worse, he has lost some of the P Ieces. Luckily, it's 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's not necessary to use all the pieces. But remember, pieces is not allowed to overlap with each other (see Sample 2).

Input

The first line of the input contains an integer t (t <=), indicating the number of cases.

For each case, the first line contains three integers n m p (1 <= N, M <=, 1 <= P <=), the width and th e height of the map, and the number of pieces. Then P lines follow, each consists of four integers x1 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 is separated by one blank line.

Output 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-make one complete map, just output-1.

Sample Input

3
5 5 1
0 0 5 5

5 5 2
0 0 3 5
2 0 5 5
ten 5 0 0
0 0
15 0 30 30

Sample Output

1
-1
2

Hint

For Sample 1, the only piece are a complete map.

For Sample 2, the both pieces may overlap with each of the other so that you can do a complete treasure map.

For Sample 3, you can make a map by either with the first 3 pieces or the last 2 pieces, and the latter approach one needs Less pieces. Author:hang, Hang
Source:the 6th Zhejiang Provincial Collegiate Programming Contest


Problem Solving Ideas:

Test instructions for a given a large rectangle of n*m, and then give the P small rectangle, give the upper left and upper right corner of the coordinates, ask can be from these small rectangle inside out some completely cover the given large rectangle, require small rectangle cover can not cross, if possible, find out the minimum number of small rectangles, otherwise output-1.

The subject can be transformed into a DLX complete coverage problem, constructing a 01 matrix. How to construct it. First we put a given p small rectangle, each as a row, then the structure of the matrix has P line, the original n*m large rectangle, can be divided into n*m, each small lattice as a column, and each small lattice has a number (1-n*m), to completely cover the large rectangle, then the number 1-n* All squares of M should be 1 (covered). The constructed matrix has a n*m column, then constructs a new matrix of P-line, n*m column. The next step is to split each small rectangle into a small lattice of 1*1, projecting it into the grid inside the corresponding row (lattice set to 1), and starting with the Pos[i][j] array to record the size of each small lattice inside a large rectangle with i,j in the large rectangle, Each line is projected by the coordinates of the small rectangle, a double loop, the small lattice one by one is projected onto the line (that is, to find each small lattice corresponding to the row of the first column). When the P-small rectangle is projected into the corresponding line, it becomes a 01 matrix, the problem is converted to extract the number of rows from the row, you can make these rows of the new matrix of each column has a 1 (because the column number is 1-n*m, the large rectangle is divided into the small lattice number is also 1-n*m, to want to completely cover, Each column must have a 1), and each column has only one 1 (because two small rectangles cannot intersect). The conversion number can be solved by DLX. Only template is not possible, the template is found on the return, and the subject is to find the smallest, find an answer to continue to find, can not return.

Code:

#include <iostream> #include <stdio.h> using namespace std;
const int maxnode=450010;
const int maxm=901;

const int maxn=505;
    struct DLX {int n,m,size;
    int U[maxnode],d[maxnode],r[maxnode],l[maxnode],row[maxnode],col[maxnode];

    int h[maxn];//Costume Node int s[maxm];//How many nodes of each column int ansd,ans[maxn];//If there is an answer, the ANSD line is selected, which lines are placed in the ans[] array, ans[0~ansd-1];
        void init (int _n,int _m) {n=_n,m=_m;
            for (int i=0;i<=m;i++) {s[i]=0;
            u[i]=d[i]=i;//initial state, up and down themselves point to their own l[i]=i-1;
        r[i]=i+1;
        } r[m]=0,l[0]=m; size=m;//number, each column has a head node, number 1-m for (int i=1;i<=n;i++) h[i]=-1;//the head node of each row} void link (int r,int c)//R line, column C {++s[col[++size]=c];//The size node is the column that contains C, the number of nodes in the forefront + + row[size]=r;//the size node row position is R d[size ]=d[c];//the following four-head interpolation method (the picture is inverted.)
        ) 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)//delete node C, and the row where the node C is located, each time this function is called, it is removed from the column head node, where c can also be understood as column C {//Because the header node of column C
        Number 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)//Restore node C, and the row of the top C node (same as above, can also be understood as starting from the head node of column C to recover {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];
    Fight this line too tangled T T l[r[c]]=r[l[c]]=c;
            } void Dance (int d)///recursive depth {if (Ansd!=-1&&ansd<=d)///minimum number of rows, previously found a set of answer Ansd<=d, currently no more recursive lookup
        Return
            if (r[0]==0) {if (ansd==-1) Ansd=d;
    else if (D&LT;ANSD)//required minimum number of blocks Ansd=d;        return;
        } int c=r[0];
        for (int i=r[0];i!=0;i=r[i]) if (s[i]<s[c]) c=i; Remove (c);//Find the column with the least number of nodes, the current element is not the 0,1 node on the original, but the column header node for (int i=d[c];i!=c;i=d[i]) {ans[d]=row[i];//column header section
            Point below a node for (int j=r[i];j!=i;j=r[j]) remove (col[j]);
        Dance (d+1);//Find, return for (int j=l[i];j!=i;j=l[j]) resume (col[j]);
    } resume (c);

}
};
DLX x;
int n,m;
int p,x1,y1,x2,y2;

int pos[32][32];//the number int ID of each rectangle in the large rectangle;
    void GetPos (int n,int m) {id=1;
for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) pos[i][j]=id++;
    } int main () {int t;scanf ("%d", &t);
        while (t--) {scanf ("%d%d%d", &n,&m,&p);
        X.init (P,N*M);
        GetPos (N,M);
            for (int r=1;r<=p;r++) {scanf ("%d%d%d%d", &x1,&y1,&x2,&y2);
         for (int i=x1+1;i<=x2;i++)       for (int j=y1+1;j<=y2;j++) X.link (R,pos[i][j]);
        } x.ansd=-1;
        X.dance (0);
    printf ("%d\n", X.ANSD);
} return 0;
 }


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.