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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章