题解:P1034 [NOIP 2002 提高组] 矩形覆盖

· · 题解

解题思路

先给每个点分配到 k 个矩形中的一个,判断每次分配后矩形是否严格分离(边、顶点都不接触)。在所有点分配完后,计算总面积,更新最小值。最后用回溯恢复状态,枚举所有合法分配方案。

整体思路难度不大,各位考虑实现。

AC 代码:

#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
int n, k, ans = INF;
int x[55], y[55];
// 定义矩形结构:左、右、下、上
struct Block {
    int l, r, b, t;
    // 判断是否为空矩形
    bool empty() {
        return l == 0 && r == 0 && b == 0 && t == 0;
    }
    // 计算面积
    int area() {
        if (empty()) return 0;
        return (r - l) * (t - b);
    }
    // 把点 (x,y) 加入这个矩形,更新边界
    void push(int px, int py) {
        if (empty()) {
            l = r = px;
            b = t = py;
            return;
        }
        l = min(l, px);
        r = max(r, px);
        b = min(b, py);
        t = max(t, py);
    }
}
all[10];// 最多 k 个矩形
// 判断两个矩形是否相交/接触
bool check(Block a, Block b) {
    if (a.empty() || b.empty()) return false;
    // 严格相交/触碰,不满足题目要求
    return a.l <= b.r && a.r >= b.l && a.b <= b.t && a.t >= b.b;
}
// 检查所有矩形是否两两都不相交、不触碰
bool is_valid() {
    for (int i = 1; i <= k; i++) {
        for (int j = i + 1; j <= k; j++) {
            if (check(all[i], all[j]))
                return false;
        }
    }
    return true;
}
// DFS:正在分配第 s 个点
void dfs(int s) {
    // 所有点分配完毕,更新答案
    if (s == n + 1) {
        int sum = 0;
        for (int i = 1; i <= k; i++)
            sum += all[i].area();
        ans = min(ans, sum);
        return;
    }
    // 尝试把第 s 个点放到第 i 个矩形里
    for (int i = 1; i <= k; i++) {
        Block backup = all[i];// 备份
        all[i].push(x[s], y[s]);// 放入点
        if (is_valid())// 满足矩形分离条件才继续
            dfs(s + 1);
        all[i] = backup;// 回溯
    }
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> k;
    for (int i = 1; i <= n; i++) {
        cin >> x[i] >> y[i];
    }
    dfs(1);// 从第 1 个点开始分配
    cout << ans << endl;
    return 0;
}

无注释版:

#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
int n, k, ans = INF;
int x[55], y[55];
struct Block {
    int l, r, b, t;
    bool empty() {
        return l == 0 && r == 0 && b == 0 && t == 0;
    }
    int area() {
        if (empty()) return 0;
        return (r - l) * (t - b);
    }
    void push(int px, int py) {
        if (empty()) {
            l = r = px;
            b = t = py;
            return;
        }
        l = min(l, px);
        r = max(r, px);
        b = min(b, py);
        t = max(t, py);
    }
}all[10];
bool check(Block a, Block b) {
    if (a.empty() || b.empty()) return false;
    return a.l <= b.r && a.r >= b.l && a.b <= b.t && a.t >= b.b;
}
bool is_valid() {
    for (int i = 1; i <= k; i++) {
        for (int j = i + 1; j <= k; j++) {
            if (check(all[i], all[j]))
                return false;
        }
    }
    return true;
}
void dfs(int s) {
    if (s == n + 1) {
        int sum = 0;
        for (int i = 1; i <= k; i++)
            sum += all[i].area();
        ans = min(ans, sum);
        return;
    }
    for (int i = 1; i <= k; i++) {
        Block backup = all[i];
        all[i].push(x[s], y[s]);
        if (is_valid())
            dfs(s + 1);
        all[i] = backup;
    }
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> k;
    for (int i = 1; i <= n; i++) {
        cin >> x[i] >> y[i];
    }

    dfs(1);
    cout << ans << endl;
    return 0;
}