zoj 3209 Treasure Map(dancing links 精准覆盖问题)

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

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.

题解:
dancing links精准覆盖问题
注意要将每个小矩形作为行,将全部小单元格作为列。将每个小矩形(行)圈出来的小单元格(列)标记出来放入大矩阵内,我们就得到了初始标记好了的矩阵

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

#define clr(x) memset(x, 0, sizeof(x))
#define clrlow(x) memset(x, -1, sizeof(x))
const int mn = 1005;
const int mnn = 1e5+5+mn;

struct DL{
    int n, m, s;//n行数m列数s目前有的节点数
    int U[mnn], D[mnn], L[mnn], R[mnn], col[mnn], row[mnn];
    int H[mn], S[mn];
    int ansd;
    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;
            L[i]=i-1;
            R[i]=i+1;
        }
        R[m]=0;
        L[0]=m;
        s=m;
        clrlow(H);
        ansd=-1;
        return ;
    }
    void push(int r, int c){
        s++;
        col[s]=c;
        row[s]=r;
        D[s]=D[c];
        U[s]=c;
        U[D[c]]=s;
        D[c]=s;
        S[c]++;
        if(H[r] < 0){//如果第r行没有元素
            H[r]=L[s]=R[s]=s;
        }
        else{
            L[s]=H[r];
            R[s]=R[H[r]];
            L[R[H[r]]]=s;
            R[H[r]]=s;
        }
    }
    void del(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[col[j]];//该元素所在的列数目减一
            }
        return ;
    }
   void reback(int c){//恢复第c列及该列上节点所在的行
       for(int i=U[c]; i!=c; i=U[i]){
            for(int j=L[i]; j!=i; j=L[j]){
                D[U[j]]=j;
                U[D[j]]=j;
                S[col[j]]++;
            }
       }
       R[L[c]]=L[R[c]]=c;//c列头指针左右相连
       return ;
   }
    bool dancing(int dep){
        if(ansd!=-1&&ansd<dep)//剪枝 不过不剪也不会wa你们可以试试反正我试过了
            return 0;
        if(R[0] == 0){
            if(ansd == -1) ansd=dep;
            else if(ansd > dep) ansd = dep;
            return 1;
        }
        int c=R[0];
        for(int i=R[0]; i!=0; i=R[i]){
            if(S[i] < S[c])
                c=i;
        }
        del(c);
        for(int i=D[c]; i!=c; i = D[i]){
            for(int j=R[i]; j!=i; j=R[j]){
                del(col[j]);
            }
            dancing(dep+1);
            for(int j=L[i]; j!=i; j=L[j])
                reback(col[j]);
        }
        reback(c);//这个可有可无,写只是为了完整性
        return 0;
    }
}dlx;

int main(){
    int T, n, m, p;
    scanf("%d", &T);
    while(T--){
        scanf("%d%d%d", &n, &m, &p);
        dlx.init(p, n*m);//将块当成行,所有的单元格看成列
        for(int i=1; i<=p; i++){//将大矩阵内所有的小单元格标号
            int x1, x2, y1, y2;
            scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
            for(int k=x1; k<x2; k++){//标记每个小矩形内的小单元格
                for(int f=y1; f<y2; f++){
                    dlx.push(i, k*m+f+1);
                }
            }
        }
        dlx.dancing(0);
        printf("%d\n", dlx.ansd);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值