1 条题解

  • 0
    @ 2023-1-3 11:29:51

    【深基5.例7】工艺品制作

    解题思路

    用三维布尔数组记录哪些小方块已被切除。每次枚举切割范围设为 true,最后统计仍为 false 的位置。

    正确性说明

    布尔标记表示所有切割区域的并集,重复切除不会多计。未被任何切割覆盖的位置恰好是剩余体积。

    易错点

    范围含两个端点;切割重叠时不能直接重复减体积。

    复杂度

    时间 O((q+1)wxh),空间 O(wxh)。

    参考代码(C++11)

    #include <bits/stdc++.h>
    using namespace std;
    
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
        int w, x, h, q;
        cin >> w >> x >> h >> q;
        static bool cut[21][21][21] = {};
        while (q--) {
            int a, b, c, d, e, f;
            cin >> a >> b >> c >> d >> e >> f;
            for (int i = a; i <= d; i++)
                for (int j = b; j <= e; j++)
                    for (int k = c; k <= f; k++)
                        cut[i][j][k] = true;
        }
        int ans = 0;
        for (int i = 1; i <= w; i++)
            for (int j = 1; j <= x; j++)
                for (int k = 1; k <= h; k++)
                    if (!cut[i][j][k])
                        ans++;
        cout << ans << "\n";
        return 0;
    }
    
    • 1

    信息

    ID
    84
    时间
    1000ms
    内存
    125MiB
    难度
    进阶
    标签
    递交数
    1
    已通过
    1
    上传者