装箱暴力法

2024-01-02

我需要制作解决装箱问题的程序,但我已经制作了首次拟合和贪婪算法,但我的讲师说在某些情况下它不会找到问题的最小解决方案。所以我决定尝试暴力破解,但我不知道它应该如何检查所有可能的解决方案。所以是的..有人可以向我解释一下或者给出伪代码什么的。我将非常感激。


注意装箱 http://en.wikipedia.org/wiki/Bin_packing_problem是一个 NP 难问题,基本上意味着即使对于相对较小的输入,也需要很长时间才能对其进行暴力破解,所以暴力破解 NP 难题几乎从来都不是一个好主意。上面的链接显示了一些替代方案(或近似值)。但我会继续...

递归 http://www.cse.wustl.edu/~kjg/cse131/Notes/Recursion/recursion.html使暴力破解变得容易。一旦你明白了基本的递归算法 https://stackoverflow.com/a/9724748/1711796, 继续阅读...

基本思想:(对于 3 件物品,2 个箱子,假设一切都合适,如果它不只是跳过该分支)

Put the first item in the first bin.
  Put the second item in the first bin.
    Put the third item in the first bin.
      Woo-hoo! We have a solution!
    Remove the third item from the first bin and put it into the second bin.
      Woo-hoo! We have a solution!
    Remove the third item from the second bin.
  Remove the second item from the first bin and put it into the second bin.
    Put the third item in the first bin.
      Woo-hoo! We have a solution!
    Remove the third item from the first bin and put it into the second bin.
      Woo-hoo! We have a solution!
    Remove the third item from the second bin.
  Remove the second item from the second bin.
Remove the first item from the first bin and put it into the second bin.
  Put the second item in the first bin.
    Put the third item in the first bin.
      Woo-hoo! We have a solution!
    Remove the third item from the first bin and put it into the second bin.
      Woo-hoo! We have a solution!
    Remove the third item from the second bin.
  Remove the second item from the first bin and put it into the second bin.
    Put the third item in the first bin.
      Woo-hoo! We have a solution!
    Remove the third item from the first bin and put it into the second bin.
      Woo-hoo! We have a solution!
    Remove the third item from the second bin.
  Remove the second item from the second bin.
Remove the first item from the second bin.

(看看已经有多少步了?这只是 3 件物品和 2 个垃圾箱)

伪代码:

recurse(int itemID)
  if pastLastItem(itemID)
    if betterThanBestSolution
      bestSolution = currentAssignment
    return
  for each bin i:
    putIntoBin(itemID, i)
    recurse(itemID+1)
    removeFromBin(itemID, i)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

装箱暴力法 的相关文章

随机推荐

  • 使用 IE 11 的 Angular 4 应用程序“无法从释放的脚本执行代码”

    我有一个 Angular 应用程序 我认为它是版本 4 IE 11 在此应用程序中的登录序列期间崩溃 无法从释放的脚本执行代码 根据 IE 控制台 错误发生在 polyfills 包的第 10939 行 var testString del
  • C++ 中结构的奇怪行为 [第 1 部分]

    考虑 struct box int array 3 int main box a 1 如果上面的代码在 C 中有效 那么为什么下面的代码不起作用 struct box int simple int int main box b 2 是什么原
  • Android构建工具1.1.0,单元测试文件夹?

    我最近在我的 android 项目中安装了来自 google 的最新工具 buildscript repositories jcenter mavenCentral dependencies classpath com android to
  • 是什么触发为 AWS Lambda 访问 VPC 资源创建 ENI

    我们部署了多个 lambda 可以使用以下方式访问我们的 VPC VpcConfig环境 据我了解 AWS Lambda 通常会按需创建 lambda 但如果您将它们连接到您的 VPC 那么 AWS 将 在某些时候 在指定的子网之一上创建
  • 在 Windows 10 中使用 WPF 对大写单词进行拼写检查

    我有一个 WPF 应用程序 其中有一些文本框CharacterCasing CharacterCasing Upper也SpellCheck IsEnabled true 在 Windows 7 中 这工作正常 但在 Windows 10
  • Rails 中的业务逻辑在哪里?

    我是一名 ASP NET MVC 开发人员 刚刚开始我的第一个 Rails 大型项目 但是我很困惑将业务逻辑放在哪里 在 ASP NET 上 我创建了一个包含处理业务逻辑的服务 域驱动设计 的库 我听说 Rails 使用胖模型瘦控制器的概念
  • NestJS:每个模块导入 HttpModule 的新实例

    我的nestjs系统是一个通过API调用连接多个系统的系统 对于每个系统 我创建了一个模块来处理它们的进程 每个模块都导入HttpModule 我想为每个模块的 HttpModule 都有单独的 Axios 拦截器 这是我测试功能的尝试 o
  • Java 8 流条件处理

    我感兴趣的是将一个流分成两个或多个子流 并以不同的方式处理元素 例如 一个 大 文本文件可能包含 A 类型的行和 B 类型的行 在这种情况下我想要执行以下操作 File lines path filter line gt isTypeA l
  • 使用 mysql-proxy 操作登录信息

    是否可以在 mysql proxy 的 lua 脚本中拦截和更改登录信息 例如 如果用户像这样访问代理 mysql h localhost P 4040 u bob D orders p 我希望连接不仅重定向到后端服务器 而且还更改用户名
  • 如何将“2021-01-19T16:20:04+0000”解析为 time.Time

    我已经尝试过以下格式 应该可以捕获 0000我读到的时区偏移量https golang org pkg time pkg constants https golang org pkg time pkg constants ts err ti
  • 根据 Woocommerce 中的每日时间范围对特定产品进行折扣

    在我的 WooCommerce 网站上 我正在尝试举办 特别午餐 每日活动 这意味着 从 11 00 到 15 00 特定产品将因 午餐特惠 每日活动而打折 我希望这个 特别午餐 活动能够在一周的每一天重复发生 如何才能实现这一目标 我在网
  • C++:创建未初始化的占位符变量而不是默认对象

    我现在正从 Java 转向 C 每当 Java 中常用的概念不能直接映射到 C 时 我就会遇到一些困难 例如 在 Java 中我会这样做 Fruit GetFruit String fruitName Fruit fruit if frui
  • 如何解决将 Java 8 代码迁移到 Java 11 时的依赖性和包问题

    我正在准备将我团队的整个项目组合迁移到 Java 11 所有应用程序均基于多年来内部开发的框架 其中某些部分已有 12 年以上的历史 我已遵循中的建议这个帖子 https winterbe com posts 2018 08 29 migr
  • Hibernate 如何在提交事务之前对乐观锁进行行版本检查

    当在提交当前事务之前hibernate检查行的版本时 它应该发出一条sqlselect用于获取行的语句 假设发出后select语句 hibernate 发现行版本没有更改 因此它应该继续提交事务 我想知道 hibernate 如何确保在选择
  • 如何卸载特定版本的 NuGet 包?

    不知何故 我的一个项目中有同一个 NuGet 包的两个版本 有没有办法卸载该软件包的特定版本 我试过了Uninstall Package name 进而Install Package name 尽管这似乎保留了两个版本 这导致我的代码出现问
  • 在 QLabel 上显示 12 位双精度型或整数,而不是 3.23223e+9

    C 正在显示一个数字3232235975 on QLabel as 3 23223e 9 同时QCustomPlot轴为3 23223 10 9 我不涉及流 例如std cout 因此std setprecision不适合我的情况 我实际上
  • 如何使用 Python Pandas 处理多级数据?

    我一直在尝试获取一些多级数据的数据 我的初始数据如下所示 使用 python 脚本我获取这些数据 df pd read csv path header 0 1 读取后的数据 Name Unnamed 1 level 0 Address Un
  • #shadow-root 是什么,为什么它在我的 font Awesome 类上不显示任何内容? [复制]

    这个问题在这里已经有答案了 为什么 Shadow root 样式在我的上不显示任何内容 fa xxx课程 影子根的东西将我使用的类添加到显示无堆中 但仅添加我添加的特定类 我使用 PHP 来包含导航 页脚等 但这些不受它的影响 例如 它在我
  • 具有自签名证书的 https iOS

    我有一台带有自签名证书的服务器 我想以 https 形式将我的设备与服务器连接 我听说我必须接受这种联系 但我不知道怎么办 我有一个自签名证书 因为它是测试服务器 但我想用https形式访问 当我尝试使用 https 访问时出现错误 SUR
  • 装箱暴力法

    我需要制作解决装箱问题的程序 但我已经制作了首次拟合和贪婪算法 但我的讲师说在某些情况下它不会找到问题的最小解决方案 所以我决定尝试暴力破解 但我不知道它应该如何检查所有可能的解决方案 所以是的 有人可以向我解释一下或者给出伪代码什么的 我