求每个cluster中box的个数

该C++代码通过队列算法处理一组Box,判断它们之间的交集形成簇,统计每个簇中Box的数量。
摘要由CSDN通过智能技术生成

题目描述
有一堆box,若干个box有交集就是一个簇,求每个cluster中box的个数。注意:可以自定义box的结构体, 无需考虑旋转角度。
请用C+实现上述代码

#include <vector>
#include <queue>
#include <algorithm>

struct Box {
    int x1, y1, x2, y2;
};

bool isIntersect(const Box& a, const Box& b) {
    return !(a.x2 < b.x1 || a.x1 > b.x2 || a.y2 < b.y1 || a.y1 > b.y2);
}

int main() {
    std::vector<Box> boxes = {/* 输入box数据 */};
    std::vector<std::vector<int>> clusters;
    std::vector<bool> visited(boxes.size(), false);

    for (int i = 0; i < boxes.size(); ++i) {
        if (visited[i]) continue;

        std::queue<int> q;
        q.push(i);
        visited[i] = true;

        std::vector<int> cluster;
        while (!q.empty()) {
            int j = q.front();
            q.pop();
            cluster.push_back(j);

            for (int k = 0; k < boxes.size(); ++k) {
                if (visited[k] || !isIntersect(boxes[j], boxes[k])) continue;
                q.push(k);
                visited[k] = true;
            }
        }

        clusters.push_back(cluster);
    }
    for (const auto& cluster : clusters) {
        std::cout << "Cluster size: " << cluster.size() << std::endl;
    }
    return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值