ZOJ-3209 Treasure Map(dancing links,舞蹈链算法,DLX)

ZOJ - 3209

题目描述

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 X算法题,对着kuangbin的模版照葫芦画瓢做出来的。
就是将这个二维的地图拉伸成一个一维的,地图碎片上的每个点代表都对应到一维数组的一个位置上,作为稀疏矩阵的一列。然后每个碎片作为一行。用舞蹈链求一个准确覆盖就可以了。
舞蹈链算法讲解,可以看我上一个转载的博客。

代码

#include <stdio.h>
const int maxnode = 500010;
const int maxm = 1010;
const int maxn = 510;

struct DLX{
    int n,m,size;
    int up[maxnode],down[maxnode],left[maxnode],right[maxnode];
    int Row[maxnode],Col[maxnode];
    int H[maxn],S[maxm];
    int ansd;
//	int ans[maxn];
    void init(int _n,int _m){
        n = _n;
        m = _m;
        for(int i = 0;i<=m;i++){//Initialize column headers
            S[i] = 0;//列上的节点个数
            up[i] = down[i] = i;
            left[i] = i-1;
            right[i] = i+1;
        }
        right[m] = 0;
        left[0] = m;
        size = m;
        for(int i = 1;i<=n;i++){
            H[i] = -1;
        }
    }
    void Link(int r,int c){
        S[c]++;
        Col[++size] = c;
        Row[size] = r;

        int cur = size;//上下连接
        down[cur] = down[c];
        up[cur] = c;
        up[down[c]] = cur;
        down[c] = cur;

        if(H[r]<0){
            H[r] = left[cur] = right[cur] = size;
        }else{
            right[cur] = right[H[r]];
            left[cur] = H[r];

            left[right[H[r]]] = cur;
            right[H[r]] = cur;
        }
    }
    void remove(int c){
        left[right[c]] = left[c];
        right[left[c]] = right[c];
        for(int i = down[c];i!=c;i = down[i]){
            for(int j = right[i];j!=i;j = right[j]){
                up[down[j]] = up[j];
                down[up[j]] = down[j];
                S[Col[j]]--;
            }
        }
    }
    void resume(int c){
        for(int i = up[c];i!=c;i = up[i]){
            for(int j = left[i];j!=i;j = left[j]){
                up[down[j]] = down[up[j]] = j;
                S[Col[j]]++;
            }
        }
        left[right[c]] = right[left[c]] = c;
    }
    //d为递归深度
    void Dance(int d){
    	if(ansd!=-1 && ansd<=d)	return;
        if(right[0] == 0){//如果矩阵为空,找到答案返回true
        	if(ansd == -1)	ansd = d;
        	else if(d<ansd)	ansd = d;
            return;
        }
        int c = right[0];
        for(int i = right[0];i!=0;i = right[i]){//找一个包含节点最少的一列 
            if(S[i]<S[c]){
                c = i;
            }
        }
        remove(c);//删除c列
        for(int i = down[c];i!=c;i = down[i]){//c列中已有元素的行所在的列也删除 
//            ans[d] = Row[i];
            for(int j = right[i];j!=i;j = right[j]){
                remove(Col[j]);
            }
            Dance(d+1);
            for(int j = left[i];j!=i;j = left[j]){//将删除的列再搞回来 
                resume(Col[j]);
            }
        }
        resume(c);//取消删除c列,回溯 
        return;
    }
};

DLX g;

int main(){
    int n,m;
    int t;scanf("%d",&t);
    while(t--){
    	int p;
		scanf("%d%d%d",&n,&m,&p);
        g.init(p,n*m);
        int x1,y1,x2,y2;
        for(int k = 1;k<=p;k++){
            scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
            for(int i = x1+1;i<=x2;i++){
            	for(int j = y1+1;j<=y2;j++){
            		g.Link(k,(i-1)*m+j);
				}
			}
        }
        g.ansd = -1;
        g.Dance(0);
        printf("%d\n",g.ansd);
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值