ZOJ 3209 Treasure Map

题目链接

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 <= n, m <= 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

大致题意
t 组数据,每组数据第一行 n , m , k 表示行列和接下来几个碎片,每个碎片是矩形,接下里四个数 x1 , y1 , x2 , y2 分别表示这个矩形的左下角坐标和右上角坐标,问至少需要几个矩形,才可以凑出完整的 n*m 矩形

具体思路
这道题目看的第一眼就知道是DLX,但是一个个矩阵还是不知道怎么去链接,最后想到二维矩阵压缩一维,结果其实是一样的,都是满足整个数组所有位置都有点,套了上次的模板T了,改了好久,写了剪枝还是T,最后上网找博客,才发现博客的数组都开得很大,不清楚是不是数据有问题,按照题意的数据开数组莫名的T掉,于是改了和博客一样得数组范围,然后A了,人类迷惑行为???

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1010;
const int maxm = 1010;
const int maxnode = 500010;
int L[maxnode], R[maxnode], U[maxnode], D[maxnode];
int C[maxnode],H[maxn],cnt[maxm],vis[maxn],ans[maxn],Row[maxnode];
int n,m,id,len;

void init()
{
    for(int i=0; i<=m*n; i++)
    {
        cnt[i] = 0;
        U[i] = D[i] = i;
        L[i + 1] = i;
        R[i] = i + 1;
    }
    R[m*n] = 0;
    id = m*n + 1;
    memset(H, -1, sizeof(H));
}

void Link(int r, int c)
{
    cnt[c]++;
    C[id] = c;
    Row[id] = r;
    U[id] = U[c];
    D[ U[c] ] = id;
    D[id] = c;
    U[c] = id;
    Row[id] = r;
    if(H[r] == -1) H[r] = L[id] = R[id] = id;
    else
    {
        L[id] = L[ H[r] ];
        R[ L[ H[r] ] ] = id;
        R[id] = H[r];
        L[ H[ r ] ] = id;
    }
    id++;
}

void Remove(int Size)
{
    L[ R[Size] ] = L[Size];
    R[ L[Size] ] = R[Size];
    for(int i=D[Size]; i!=Size; i=D[i])
    {
        for(int j=R[i]; j!=i; j=R[j])
        {
            U[ D[j] ] = U[j];
            D[ U[j] ] = D[j];
            cnt[ C[j] ]--;
        }
    }
}

void Resume(int Size)
{
    for(int i=D[Size]; i!=Size; i=D[i])
    {
        for(int j=R[i]; j!=i; j=R[j])
        {
            U[ D[j] ] = j;
            D[ U[j] ] = j;
            cnt[ C[j] ]++;
        }
    }
    L[ R[Size] ] = Size;
    R[ L[Size] ] = Size;
}

void DLX(int k)
{

    int pos,mm = maxn;
    if(R[0] == 0)
    {
        if (len==-1)
        {
            len=k;
        }else
        {
            len = min(len,k);
        }
        return ;
    }else if (len!=-1&&k>=len)
    {
        return ;
    }
    for(int i=R[0]; i; i=R[i])
    {
        if(mm > cnt[i])
        {
            mm = cnt[i];
            pos = i;
        }
    }
    Remove(pos);
    for(int i=D[pos]; i!=pos; i=D[i])
    {
        ans[k] = Row[i];
        for(int j=R[i]; j!=i; j=R[j]) Remove(C[j]);
        DLX(k + 1);
        for(int j=L[i]; j!=i; j=L[j]) Resume(C[j]);
    }
    Resume(pos);
    return ;
}

int main()
{
    int u,v,a,t,k;
    scanf("%d",&t);
    while (t--)
    {
        scanf("%d %d %d",&n,&m,&k);
        init();
        for (int i=1; i<=k; i++)
        {
            int x1,y1,x2,y2;
            scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
            for (int j=x1+1; j<=x2; j++)
            {
                for (int k=y1+1; k<=y2; k++)
                {
                    Link(i,k+(j-1)*m);
                }
            }
        }
        len=-1;
        DLX(0);
        printf("%d\n",len);

    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值