开启辅助访问 切换到宽版

精易论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

用微信号发送消息登录论坛

新人指南 邀请好友注册 - 我关注人的新帖 教你赚取精币 - 每日签到


求职/招聘- 论坛接单- 开发者大厅

论坛版规 总版规 - 建议/投诉 - 应聘版主 - 精华帖总集 积分说明 - 禁言标准 - 有奖举报

查看: 759|回复: 1
收起左侧

[已解决] 有没有三维装箱算法

 关闭 [复制链接]
结帖率:97% (28/29)
发表于 2023-10-6 12:39:01 | 显示全部楼层 |阅读模式   山东省泰安市
12精币
RT  感谢大佬

计算箱子能不能装得下物品

最佳答案

查看完整内容

using System; using System.Collections.Generic; public class Box { public int Length { get; set; } public int Width { get; set; } public int Height { get; set; } public bool IsPacked { get; set; } public Box(int length, int width, int height) { Length = length; Width = width; Height = height; IsPacked = false; } } public class Containe ...

回答提醒:如果本帖被关闭无法回复,您有更好的答案帮助楼主解决,请发表至 源码区 可获得加分喔。
友情提醒:本版被采纳的主题可在 申请荣誉值 页面申请荣誉值,获得 1点 荣誉值,荣誉值可兑换荣誉会员、终身vip用户组。
快捷通道:申请荣誉值无答案申请取消悬赏投诉有答案未采纳为最佳
结帖率:73% (8/11)
发表于 2023-10-6 12:39:02 | 显示全部楼层   河北省廊坊市
using System;
using System.Collections.Generic;

public class Box
{
    public int Length { get; set; }
    public int Width { get; set; }
    public int Height { get; set; }
    public bool IsPacked { get; set; }

    public Box(int length, int width, int height)
    {
        Length = length;
        Width = width;
        Height = height;
        IsPacked = false;
    }
}

public class Container
{
    public int Length { get; set; }
    public int Width { get; set; }
    public int Height { get; set; }

    public Container(int length, int width, int height)
    {
        Length = length;
        Width = width;
        Height = height;
    }
}

public class PackingAlgorithm
{
    public List<Box> PackBoxes(List<Box> boxes, Container container)
    {
        List<Box> packedBoxes = new List<Box>();
        List<Box> unpackedBoxes = new List<Box>(boxes); // 将所有箱子标记为未装载状态

        while (unpackedBoxes.Count > 0)
        {
            Box bestFitBox = null;
            int bestFitVolume = int.MaxValue;

            foreach (Box box in unpackedBoxes)
            {
                if (box.Length <= container.Length && box.Width <= container.Width && box.Height <= container.Height)
                {
                    int volume = box.Length * box.Width * box.Height;
                    if (volume < bestFitVolume)
                    {
                        bestFitBox = box;
                        bestFitVolume = volume;
                    }
                }
            }

            if (bestFitBox != null)
            {
                packedBoxes.Add(bestFitBox);
                bestFitBox.IsPacked = true;
                container.Length -= bestFitBox.Length;
                container.Width -= bestFitBox.Width;
                container.Height -= bestFitBox.Height;
                unpackedBoxes.Remove(bestFitBox);
            }
            else
            {
                break; // 无法找到适合的箱子
            }
        }

        return packedBoxes;
    }
}

public class Program
{
    public static void Main(string[] args)
    {
        List<Box> boxes = new List<Box>()
        {
            new Box(10, 5, 7),
            new Box(8, 6, 4),
            new Box(3, 9, 2),
            new Box(5, 4, 6),
            new Box(7, 7, 7)
        };

        Container container = new Container(20, 15, 12);

        PackingAlgorithm algorithm = new PackingAlgorithm();
        List<Box> packedBoxes = algorithm.PackBoxes(boxes, container);

        Console.WriteLine("装箱结果:");
        foreach (Box box in packedBoxes)
        {
            Console.WriteLine($"长:{box.Length}  宽:{box.Width}  高:{box.Height}");
        }
    }
}

评分

参与人数 1荣誉 +1 收起 理由
项目部004 + 1 热心帮助他人,荣誉+1,希望继续努力(*^__^*) 嘻嘻!

查看全部评分

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 致发广告者

发布主题 收藏帖子 返回列表

sitemap| 易语言源码| 易语言教程| 易语言论坛| 易语言模块| 手机版| 广告投放| 精易论坛
拒绝任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论,本站内容均为会员发表,并不代表精易立场!
论坛帖子内容仅用于技术交流学习和研究的目的,严禁用于非法目的,否则造成一切后果自负!如帖子内容侵害到你的权益,请联系我们!
防范网络诈骗,远离网络犯罪 违法和不良信息举报电话0663-3422125,QQ: 793400750,邮箱:wp@125.la
网站简介:精易论坛成立于2009年,是一个程序设计学习交流技术论坛,隶属于揭阳市揭东区精易科技有限公司所有。
Powered by Discuz! X3.4 揭阳市揭东区精易科技有限公司 ( 粤ICP备12094385号-1) 粤公网安备 44522102000125 增值电信业务经营许可证 粤B2-20192173

快速回复 返回顶部 返回列表