ZOJ 3209 Treasure Map(舞蹈链)

题目链接[kuangbin带你飞]专题三 Dancing Links B - Treasure Map

题意

给一矩形和k个小矩形,问选取最小数量为多少的小矩形可以对大矩形进行精确覆盖。

思路

仍然是个模版题,把二维的n*m的大矩形看作是一维的n*m的一条线。k个小矩形同理,那么就转化成01矩阵精确覆盖的问题了。

代码

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <vector>

using namespace std;

const int N = 1009;
const int MAX = 1000009;

int U[MAX], D[MAX], L[MAX], R[MAX];//数组模拟链表指向(上下左右)
int C[MAX], M[MAX];//节点所在列与行
int S[N];//储存每列的元素数量
int H[N];//行头指针
int ans;

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

void link(int row, int col, int id)//将节点加入链表
{
    C[id] = col; M[id] = row;//记录行列
    U[id] = U[col]; D[U[col]] = id;//上下连接
    D[id] = col; U[col] = id;
    if(H[row] == -1)//左右连接(使用表头方便头插)
        H[row] = L[id] = R[id] = id;
    else
    {
        L[id] = L[H[row]]; R[L[H[row]]] = id;
        L[H[row]] = id; R[id] = H[row];
    }
    S[col]++;
}

void remove(int col)//删除列
{
    R[L[col]] = R[col];
    L[R[col]] = L[col];
    for(int i=D[col]; i!=col; i=D[i])
    {
        for(int j=R[i]; j!=i; j=R[j])
        {
            U[D[j]] = U[j];
            D[U[j]] = D[j];
            S[C[j]]--;
        }
    }
}

void resume(int col)//恢复列(先删的后恢复,后删的先恢复,所以跟remove反向操作)
{
    R[L[col]] = col;
    L[R[col]] = col;
    for(int i=U[col]; i!=col; i=U[i])
    {
        for(int j=L[i]; j!=i; j=L[j])
        {
            U[D[j]] = j;
            D[U[j]] = j;
            S[C[j]]++;
        }
    }
}

void dance(int k)
{
    if(ans!=-1 && k>=ans)
        return;
    if(!R[0])
    {
        ans = k;
        return;
    }
    int col = R[0];
    for(int i=R[0]; i!=0; i=R[i])
        if(S[i] < S[col])
            col = i;
    remove(col);
    for(int i=D[col]; i!=col; i=D[i])
    {
        for(int j=R[i]; j!=i; j=R[j])
            remove(C[j]);
        dance(k+1);
        for(int j=L[i]; j!=i; j=L[j])
            resume(C[j]);
    }
    resume(col);
}

int main()
{
    int n, m, k, T;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d%d%d", &n, &m, &k);
        init(k, n*m);
        int id = n*m+1;
        for(int i=1; i<=k; i++)
        {
            int x1, x2, y1, y2;
            scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
            for(int x=x1+1; x<=x2; x++)
                for(int y=y1+1; y<=y2; y++)
                    link(i, y+(x-1)*m, id++);
        }
        ans = -1;
        dance(0);
        printf("%d\n", ans);
    }
    return 0;
}
时间: 2024-12-29 16:19:57

ZOJ 3209 Treasure Map(舞蹈链)的相关文章

ZOJ 3209 Treasure Map (Dancing Links)

Treasure Map Time Limit: 2 Seconds      Memory Limit: 32768 KB 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. Luck

ZOJ 3209 Treasure Map(DLX精确覆盖)

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

[ACM] ZOJ 3209 Treasure Map ( Dancing Links 精确覆盖,矩形覆盖)

Treasure Map Time Limit: 2 Seconds      Memory Limit: 32768 KB 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. Luck

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

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 e

ZOJ 3209 Treasure Map DLX

用最少的矩阵覆盖n*m的地图,注意矩阵不能互相覆盖. 这里显然是一个精确覆盖,但由于矩阵拼接过程中,有公共的边,这里需要的技巧就是把矩阵的左边和下面截去一个单位. #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <set> #

ZOJ 3209 Treasure Map

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3209 dancing links 每个点作为一列. 每个块作为一行. 不知道为什么插节点的时候 如果把循环写成 for(int i = y1+1; i <= y2; i++){ //行 for(int j = x1+1; j <= x2; j++){ //列 } }然后col = (y-1)*n+x就会超时. 1 #include <iostream&g

ZOJ 3209 Treasure Map (Dancing Links 精确覆盖 )

题意 :  给你一个大小为 n * m 的矩形 , 坐标是( 0 , 0 ) ~ ( n , m )  .然后给你 p 个小矩形 , 坐标是( x1 , y1 ) ~ ( x2 , y2 ) , 你选择最小的几个矩形 , 使得这些矩形可以覆盖整个矩形 , 并且互相不会重叠 .( n , m <= 30 ) 思路 : Dancing Links 的精确覆盖问题 . 我们将 n * m 的矩形分成 n * m 个小正方形 ,那么我们只要保证每个小正方形被覆盖且只被覆盖一次即可 . 那么列表示每个小正

zoj 3209.Treasure Map(DLX精确覆盖)

直接精确覆盖 开始逐行添加超时了,换成了单点添加 #include <iostream> #include <cstring> #include <cstdio> #include <cmath> #include <vector> using namespace std; #define FOR(i,A,s) for(int i = A[s]; i != s; i = A[i]) #define exp 1e-8 const int MAX =

ZOJ 3209 Treasure Map 精确覆盖

题目链接 精确覆盖的模板题, 把每一个格子当成一列就可以. S忘记初始化TLE N次, 哭晕在厕所...... 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define pb(x) push_back(x) 4 #define ll long long 5 #define mk(x, y) make_pair(x, y) 6 #define lson l, m, rt<<1 7 #define mem(a) memset(