(简单) ZOJ 3209 Treasure Map , DLX+精确覆盖。

本文介绍了一种使用矩形拼图覆盖大矩形的方法,通过构建01矩阵和运用DLX算法解决拼图问题,确保矩形不重叠且能够完全覆盖目标区域。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  Description

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

 

  题目就是找到几个矩形来覆盖一个大的矩形,而且要求不能重叠。

  构造01矩阵的话就是把n*m这个大矩形的每一个点都作为一列(也就是有n*m列),然后把每一个可选择矩形都当做一行。。。

 

代码如下:

#include<iostream>
#include<cstring>

using namespace std;

const int INF=10e8;
const int MaxN=600;
const int MaxM=33*33;
const int MaxNode=900*500+10;

struct DLX
{
    int n,m,size;
    int U[MaxNode],D[MaxNode],L[MaxNode],R[MaxNode],col[MaxNode];
    int S[MaxM],H[MaxN];
    int ans;

    void init(int _n,int _m)
    {
        n=_n;
        m=_m;

        ans=INF;

        for(int i=0;i<=m;++i)
        {
            S[i]=0;
            U[i]=D[i]=i;
            L[i]=i-1;
            R[i]=i+1;
        }
        L[0]=m;
        R[m]=0;

        size=m;

        for(int i=0;i<=n;++i)
            H[i]=-1;
    }

    void Link(int r,int c)    //r is row , c is col.
    {
        col[++size]=c;
        ++S[c];

        U[size]=U[c];
        D[U[c]]=size;
        D[size]=c;
        U[c]=size;

        if(H[r]<0)
            H[r]=L[size]=R[size]=size;
        else
        {
            R[size]=H[r];
            L[size]=L[H[r]];
            R[L[H[r]]]=size;
            L[H[r]]=size;
        }
    }

    void remove(int c)
    {
        R[L[c]]=R[c];
        L[R[c]]=L[c];

        for(int i=D[c];i!=c;i=D[i])
            for(int j=R[i];j!=i;j=R[j])
            {
                D[U[j]]=D[j];
                U[D[j]]=U[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[j]];
                D[U[j]]=U[D[j]]=j;
            }

        R[L[c]]=L[R[c]]=c;
    }

    void Dance(int d)
    {
        if(R[0]==0)
        {
            if(d<ans)
                ans=d;

            return;
        }

        if(d>ans)
            return;

        int c=R[0];
        
        for(int i=R[0];i!=0;i=R[i])
            if(S[i]<S[c])
                c=i;

        remove(c);

        for(int i=D[c];i!=c;i=D[i])
        {
            for(int j=R[i];j!=i;j=R[j])
                remove(col[j]);

            Dance(d+1);

            for(int j=L[i];j!=i;j=L[j])
                resume(col[j]);
        }

        resume(c);
    }
};

DLX dlx;

int main()
{
    ios::sync_with_stdio(false);

    int T;
    int n,m,p;
    int x1,x2,y1,y2;
    cin>>T;

    while(T--)
    {
        cin>>n>>m>>p;

        dlx.init(p,(n)*(m));

        for(int i=1;i<=p;++i)
        {
            cin>>x1>>y1>>x2>>y2;

            for(int x=x1;x<x2;++x)
                for(int y=y1;y<y2;++y)
                    dlx.Link(i,y*(n)+x+1);
        }

        dlx.Dance(0);

        if(dlx.ans==INF)
            cout<<-1<<endl;
        else
            cout<<dlx.ans<<endl;
    }

    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/whywhy/p/4263872.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值