ZOJ3209Treasure Map accurately covers DLX

Source: Internet
Author: User
Tags x2 y2

 

Treasure Map

Time Limit: 2 seconds
Memory limit: 32768 KB

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
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 ).

Input

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.

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 to 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
30 30 5
0 0 30 10
0 10 30 20
0 20 30 30
0 0 15 30
15 0 30 30

Sample Output

1
-1
2

Hint

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.

Author: HANG, Hang
Source: The 6th Zhejiang Provincial Collegiate Programming Contest
SubmitStatus

The question is to give you a n * m rectangle with a P small rectangle. Ask how many small rectangles do not need to be repeated to create n * m rectangles. Very bare precision coverage is solved with DLX, But I wa it for countless times... Figure: P is a small rectangle and each row has n * m columns in each row. The lattice of each column I * m + J is overwritten by this small rectangle. My error is: 1. the column is I * m + J. I write it as I * n + j2.dfs backtracing to find the minimum value. I write it once and then return it... The Code on the Internet basically uses static arrays. I use static linked lists, but the efficiency is higher than that of them. Code:
Run ID Submit Time Judge Status Problem ID Language Run Time (MS) Run Memory (KB) User Name
2644280 14:34:16 Accepted 3209 C ++ 180 1156 Magic Wing
#include<cstdio>#define N 505#define M 1005int m,n,H,cnt,size[M],ans;struct Node{    int r,c;    Node *U,*D,*L,*R;}node[40005],row[N],col[M],head;void init(int r,int c){    cnt=0;    head.r=r;    head.c=c;    head.L=head.R=head.U=head.D=&head;    for(int i=0;i<c;i++){        col[i].r=r;        col[i].c=i;        col[i].L=&head;        col[i].R=head.R;        col[i].U=col[i].D=col[i].L->R=col[i].R->L=&col[i];        size[i]=0;    }    for(int i=r-1;i>=0;i--){        row[i].r=i;        row[i].c=c;        row[i].U=&head;        row[i].D=head.D;        row[i].L=row[i].R=row[i].U->D=row[i].D->U=&row[i];    }}void insert(int r,int c){    Node *p=&node[cnt++];    p->r=r;    p->c=c;    p->R=&row[r];    p->L=row[r].L;    p->L->R=p->R->L=p;    p->U=&col[c];    p->D=col[c].D;    p->U->D=p->D->U=p;    ++size[c];}void delLR(Node *p){    p->L->R=p->R;    p->R->L=p->L;}void delUD(Node *p){    p->U->D=p->D;    p->D->U=p->U;}void resumeLR(Node *p){p->L->R=p->R->L=p;}void resumeUD(Node *p){p->U->D=p->D->U=p;}void cover(int c){    if(c==H)        return;Node *R,*C;    delLR(&col[c]);    for(C=col[c].D;C!=&col[c];C=C->D)for(R=C->L;R!=C;R=R->L){--size[R->c];delUD(R);}}void resume(int c){    if(c==H)        return;    Node *R,*C;    for(C=col[c].U;C!=&col[c];C=C->U)for(R=C->R;R!=C;R=R->R){++size[R->c];resumeUD(R);}    resumeLR(&col[c]);}void dfs(int k){    if(head.L==&head){if(k<ans)ans=k;return;}if(k>=ans)return;    int INF=-1u>>1,c=-1;Node *p,*rc;    for(p=head.L;p!=&head;p=p->L)        if(size[p->c]<INF)            INF=size[c=p->c];if(!INF)return;    cover(c);    for(p=col[c].D;p!=&col[c];p=p->D){        for(rc=p->L;rc!=p;rc=rc->L)            cover(rc->c);        dfs(k+1);        for(rc=p->R;rc!=p;rc=rc->R)            resume(rc->c);    }    resume(c);}int main(){int t,p;scanf("%d",&t);    while(t--){int i,j,k,x0,x1,y0,y1;scanf("%d%d%d",&n,&m,&p);init(p+1,H=n*m);for(k=0;k<p;k++){scanf("%d%d%d%d",&x0,&y0,&x1,&y1);for(i=x0;i<x1;i++)for(j=y0;j<y1;j++)insert(k,i*m+j);}ans=999;dfs(0);if(ans<999)printf("%d\n",ans);elseputs("-1");}}

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.