2026-03-19 16:04:16
发布于:江苏
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;
struct Rectangle {
int x1, y1, z1, x2, y2, z2;
Rectangle() : x1(0), y1(0), z1(0), x2(0), y2(0), z2(0) {}
Rectangle(int a, int b, int c, int d, int e, int f) {
x1 = min(a, d); x2 = max(a, d);
y1 = min(b, e); y2 = max(b, e);
z1 = min(c, f); z2 = max(c, f);
}
};
// 优化的相交检测函数
inline bool intersect(const Rectangle& r1, const Rectangle& r2) {
return (r1.x1 <= r2.x2 && r2.x1 <= r1.x2) &&
(r1.y1 <= r2.y2 && r2.y1 <= r1.y2) &&
(r1.z1 <= r2.z2 && r2.z1 <= r1.z2);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<Rectangle> rectangles(n);
for (int i = 0; i < n; ++i) {
int x1, y1, z1, x2, y2, z2;
cin >> x1 >> y1 >> z1 >> x2 >> y2 >> z2;
rectangles[i] = Rectangle(x1, y1, z1, x2, y2, z2);
}
// 计算冲突的矩形对数
long long count = 0;
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
if (intersect(rectangles[i], rectangles[j])) {
count++;
}
}
}
cout << count << "\n";
return 0;
}
这里空空如也

















有帮助,赞一个