POJ 1084 Square Destroyer【Dancing Links重复覆盖】

本文深入解析了如何通过精确的匹配与破坏策略来优化网格结构,详细介绍了计算最小匹配数以消除所有存在的正方形的方法,适用于不同大小的网格布局。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Square Destroyer
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 2695 Accepted: 1101

Description

The left figure below shows a complete 3*3 grid made with 2*(3*4) (=24) matchsticks. The lengths of all matchsticks are one. You can find many squares of different sizes in the grid. The size of a square is the length of its side. In the grid shown in the left figure, there are 9 squares of size one, 4 squares of size two, and 1 square of size three. 

Each matchstick of the complete grid is identified with a unique number which is assigned from left to right and from top to bottom as shown in the left figure. If you take some matchsticks out from the complete grid, then some squares in the grid will be destroyed, which results in an incomplete 3*3 grid. The right figure illustrates an incomplete 3*3 grid after removing three matchsticks numbered with 12, 17 and 23. This removal destroys 5 squares of size one, 3 squares of size two, and 1 square of size three. Consequently, the incomplete grid does not have squares of size three, but still has 4 squares of size one and 1 square of size two. 

As input, you are given a (complete or incomplete) n*n grid made with no more than 2n(n+1) matchsticks for a natural number 5 <= n . Your task is to compute the minimum number of matchsticks taken 
out to destroy all the squares existing in the input n*n grid.

Input

The input consists of T test cases. The number of test cases (T ) is given in the first line of the input file. 
Each test case consists of two lines: The first line contains a natural number n , not greater than 5, which implies you are given a (complete or incomplete) n*n grid as input, and the second line begins with a nonnegative integer k , the number of matchsticks that are missing from the complete n*n grid, followed by 
k numbers specifying the matchsticks. Note that if k is equal to zero, then the input grid is a complete n*n grid; otherwise, the input grid is an incomplete n*n grid such that the specified k matchsticks are missing from the complete n*n grid.

Output

Print exactly one line for each test case. The line should contain the minimum number of matchsticks that have to be taken out to destroy all the squares in the input grid.

Sample Input

2
2
0
3
3 12 17 23

Sample Output

3
3

Source


Dancing Links重复覆盖,难在如何构建0/1矩阵。把火柴杆作为行,图中存在的正方形作为列,如果第i根火柴是第j个正方形的边的一部分,那么mtx[i][j] = 1;否则,mtx[i][j] = 0。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath> 

using namespace std;

const int maxn = 65*6*6*6;
const int oo = 1 << 30;
const int maxrow = 65;
const int maxcol = 6*6*6;
bool mtx[maxrow][maxcol];
int t, n, k, ans;
int has[65];

int totRow, totCol, head, idx;
int L[maxn], R[maxn], U[maxn], D[maxn];
int RH[maxn], CH[maxn], S[maxn];

bool ok(int x1, int y1, int x2, int y2)
{
    bool flag = true;
    int id; 
    for (int i = y1; i <= y2; ++i) {
        id = (x1 - 1) * (2 * n + 1) + i;
        if (!has[id]) {
            flag = false;
            break;
        }
        id = (x2) * (2 * n + 1) + i;
        if (!has[id]) {
            flag = false;
            break;
        }
    }
    if (!flag) return false;
    for (int i = x1; i <= x2; ++i) {
        id = (i - 1)*(2*n+1) + n + y1;
        if (!has[id]) {
            flag = false;
            break;  
        }
        id = (i - 1)*(2*n+1) + n + y2 + 1;
        if (!has[id]) {
            flag = false;
            break;
        }
    }
    if (!flag) return false;
    return true;
}

void add(int x1, int y1, int x2, int y2, int c)
{
    int id;
    for (int i = y1; i <= y2; ++i) {
        id = (x1 - 1) * (2 * n + 1) + i;
        mtx[id-1][c] = 1;
        id = (x2) * (2 * n + 1) + i;
        mtx[id-1][c] = 1;
    }
    for (int i = x1; i <= x2; ++i) {
        id = (i - 1)*(2*n+1) + n + y1;
        mtx[id-1][c] = 1;
        id = (i - 1)*(2*n+1) + n + y2 + 1;
        mtx[id-1][c] = 1;
    }
}

void initMtx()
{
    int cnt = 0;
    memset(mtx, 0, sizeof(mtx));
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= n; ++j) {
            for (int k = 0; i + k <= n && j + k <= n; ++k) {
                if (ok(i, j, i + k, j + k)) {
                    add(i, j, i + k, j + k, cnt);
                    cnt++;
                }
            }
        }
    }
    totRow = 2 * n * (n + 1);
    totCol = cnt;
}

int newNode(int up, int down, int left, int right)
{
    U[idx] = up;    D[idx] = down;
    L[idx] = left;  R[idx] = right;
    U[down] = D[up] = L[right] = R[left] = idx;
    return idx++;
}

void build()
{
    idx = maxn - 1;
    head = newNode(idx, idx, idx, idx);
    idx = 0;
    for (int j = 0; j < totCol; ++j) {
        newNode(idx, idx, L[head], head);
        CH[j] = j;  S[j] = 0;
    }
    for (int i = 0; i < totRow; ++i) {
        int k = -1;
        for (int j = 0; j < totCol; ++j) {
            if (!mtx[i][j]) continue;
            if (-1 == k) {
                k = newNode(U[CH[j]], CH[j], idx, idx);
                RH[k] = i;  CH[k] = j;  S[j]++;
            } else {
                k = newNode(U[CH[j]], CH[j], k, R[k]);
                RH[k] = i;  CH[k] = j;  S[j]++;
            }
        }
    }
}

void remove(int c)
{
    for (int i = D[c]; i != c; i = D[i]) {
        L[R[i]] = L[i]; R[L[i]] = R[i]; /*S[CH[i]]--;*/
    }
}

void resume(int c)
{
    for (int i = U[c]; i != c; i = U[i]) {
        L[R[i]] = R[L[i]] = i; /*S[CH[i]]++;*/ 
    }
}

/*估价函数*/
int h()
{
    bool vis[maxcol];
    memset(vis, false, sizeof(vis));
    int ret = 0;
    for (int i = R[head]; i != head; i = R[i]) {
        if (!vis[i]) {
            ret++;
            vis[i] = true;
            for (int j = D[i]; j != i; j = D[j]) {
                for (int k = R[j]; k != j; k = R[k]) {
                    vis[CH[k]] = true;
                }
            }
        }
    }
    return ret;
}

void dance(int cnt)
{
    if (cnt + h() >= ans) {
        return ;
    }
    if (R[head] == head) {
        ans = cnt;
        return ;
    }
    int c, Min = oo;
    for (int i = R[head]; i != head; i = R[i]) {
        if (S[i] < Min) {
            Min = S[i]; c = i;
        }
    }
    for (int i = D[c]; i != c; i = D[i]) {
        remove(i);
        for (int j = R[i]; j != i; j = R[j]) {
            remove(j);
        } 
        dance(cnt + 1);
        
        for (int j = L[i]; j != i; j = L[j]) {
            resume(j);
        }
        resume(i);
    }
    return ; 
}

int main()
{
    scanf("%d", &t);
    while (t--) {
        scanf("%d", &n);
        scanf("%d", &k);
        memset(has, true, sizeof(has));
        int num;
        for (int i = 0; i < k; ++i) {
            scanf("%d", &num);
            has[num] = false;
        }
        initMtx();
        build();
        ans = oo;
        dance(0);
        printf("%d\n", ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值