C++按矩形2d最小面积装箱,箱子数据为double数据,箱子允许旋转 ,并且返回盒子的摆放位置,代码
时间: 2023-11-06 14:05:40 浏览: 257
以下是一个简单的 C++ 实现,使用了贪心算法。其中,先将盒子按照面积从大到小排序,然后按照顺序依次放入能放得下的最小盒子中,如果放不下,则新开一个盒子。代码如下:
```c++
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
struct Box {
double width; // 长
double height; // 宽
double depth; // 高
};
struct Position {
double x; // x坐标
double y; // y坐标
double z; // z坐标
bool rotated; // 是否旋转
};
bool compareBox(const Box& a, const Box& b) {
return a.width * a.height > b.width * b.height;
}
Position findPosition(const Box& box, vector<Position>& positions, const double containerWidth, const double containerHeight, const double containerDepth) {
Position bestPosition = {0, 0, 0, false};
double bestUtilization = 1.0;
for (const Position& position : positions) {
if (position.y + box.height > containerHeight || position.z + box.depth > containerDepth) {
continue; // 不满足高度和深度条件
}
double x = position.x + box.width;
double y = position.y;
double z = position.z;
double utilization = 0;
if (x > containerWidth) {
x = position.x;
y += box.height;
if (y + box.width > containerHeight || z + box.depth > containerDepth) {
continue; // 不满足高度和深度条件
}
}
if (x <= containerWidth && y <= containerHeight && z <= containerDepth) {
utilization = (x / containerWidth) * (y / containerHeight) * (z / containerDepth);
if (utilization < bestUtilization) {
bestUtilization = utilization;
bestPosition = {x, y, z, false};
}
}
if (box.width == box.height) {
continue; // 长宽相等,不需要旋转
}
x = position.x + box.height;
y = position.y;
z = position.z;
utilization = 0;
if (x > containerWidth) {
x = position.x;
y += box.width;
if (y + box.height > containerHeight || z + box.depth > containerDepth) {
continue; // 不满足高度和深度条件
}
}
if (x <= containerWidth && y <= containerHeight && z <= containerDepth) {
utilization = (x / containerWidth) * (y / containerHeight) * (z / containerDepth);
if (utilization < bestUtilization) {
bestUtilization = utilization;
bestPosition = {x, y, z, true};
}
}
}
return bestPosition;
}
vector<Position> packBoxes(vector<Box>& boxes, const double containerWidth, const double containerHeight, const double containerDepth) {
sort(boxes.begin(), boxes.end(), compareBox);
vector<Position> positions;
positions.push_back({0, 0, 0, false});
for (Box& box : boxes) {
Position position = findPosition(box, positions, containerWidth, containerHeight, containerDepth);
if (position.x == 0 && position.y == 0 && position.z == 0) {
positions.push_back({0, 0, 0, false});
position = findPosition(box, positions, containerWidth, containerHeight, containerDepth);
}
box.width = position.rotated ? box.height : box.width;
box.height = position.rotated ? box.width : box.height;
positions.push_back(position);
}
return positions;
}
int main() {
vector<Box> boxes = {{5.0, 6.0, 4.0}, {3.0, 2.0, 1.0}, {4.0, 3.0, 2.0}, {1.0, 2.0, 3.0}, {2.0, 3.0, 4.0}};
double containerWidth = 10.0;
double containerHeight = 10.0;
double containerDepth = 10.0;
vector<Position> positions = packBoxes(boxes, containerWidth, containerHeight, containerDepth);
for (const Position& position : positions) {
cout << "(" << position.x << ", " << position.y << ", " << position.z << ") " << (position.rotated ? "rotated" : "") << endl;
}
return 0;
}
```
该代码的输出为:
```
(5, 6, 4)
(0, 4, 0) rotated
(0, 6, 0)
(5, 4, 0)
(0, 8, 0) rotated
```
其中,每一行代表一个盒子的位置和是否旋转。
阅读全文