zoj 3209(Dancing Links应用)

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 are now broken to many 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).

Input

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

For each case, the first line contains three integers n m p (1 <= nm <= 30, 1 <= p <= 500), the width and the 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 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

题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3209

分析:这题可以转化为精确覆盖问题,然后用DLX来解决

把每个单位格子作为1列,把每块地图作为一行,每个地图能够覆盖的格子那列就标记为1,题目就转化为选择最少的行,使得每一列恰好有一个1的问题,也就是精确覆盖的DLX,敲个代码就能A掉了。。。

代码(效率不怎样,可能别人都写估值函数了吧,不会阿~~~):

#include<cstdio>
#define mm 555555
#define mn 999
int U[mm],D[mm],L[mm],R[mm],C[mm];
int H[mn],S[mn];
int id[33][33];
int n,m,p,size,T,ans;
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[C[j]];
}
void resume(int c)
{
    L[R[c]]=R[L[c]]=c;
    for(int i=U[c];i!=c;i=U[i])
        for(int j=L[i];j!=i;j=L[j])
            ++S[C[U[D[j]]=D[U[j]]=j]];
}
void Dance(int k)
{
    if(k>=ans)return;
    if(!R[0])
    {
        ans=k;
        return;
    }
    int i,j,tmp,c;
    for(tmp=mm,i=R[0];i;i=R[i])
        if(S[i]<tmp)tmp=S[c=i];
    remove(c);
    for(i=D[c];i!=c;i=D[i])
    {
        for(j=R[i];j!=i;j=R[j])remove(C[j]);
        Dance(k+1);
        for(j=L[i];j!=i;j=L[j])resume(C[j]);
    }
    resume(c);
}
void Link(int r,int c)
{
    ++S[C[++size]=c];
    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 prepare(int r,int c)
{
    for(int i=0;i<=c;++i)
    {
        S[i]=0;
        U[i]=D[i]=i;
        R[i]=i+1;
        L[i+1]=i;
    }
    R[c]=0;
    while(r)H[r--]=-1;
}
int main()
{
    int i,j,k,x1,y1,x2,y2;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d%d",&n,&m,&p);
        for(size=i=0;i<n;++i)
            for(j=0;j<m;++j)id[i][j]=++size;
        prepare(p,size);
        for(k=1;k<=p;++k)
        {
            scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
            for(i=x1;i<x2;++i)
                for(j=y1;j<y2;++j)Link(k,id[i][j]);
        }
        ans=mm;
        Dance(0);
        if(ans<mm)printf("%d\n",ans);
        else puts("-1");
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值