- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
想象这样的谜题: puzzle
我有几种形状,例如:
10圈
8个三角形
9个正方形
我也有一些盘子用来放形状,例如:
A板:2个圆孔,3个三角孔,1个方孔
B板:1个圆孔,0个三角孔,3个方孔
C板:2个圆孔,2个三角孔,2个方孔
我想找到最小数量的盘子来放置所有形状(盘子不需要完全填满)
例如:
我可以选择 6 个盘子 [A, A, A, B, B, C],我可以插入所有形状
但我也可以选择 [A, A, C, C, C] 这也可以,
所以这个问题的答案是:5
如果这个问题推广到 N 型形状和 M 型板,解决这个问题的最佳算法是什么?答案的时间复杂度是多少?
最佳答案
这个问题是一个 NP-hard 问题,一旦你意识到有一个非常简单的 polynomial time reduction 就更容易看出来了来自 bin packing problem到这个问题。
我建议您使用 integer linear programming技术来解决它。
解决您问题的 ILP 可以是:
// Data
Shapes // array of integers of size n, contains the number of each shape to fit
Plates // 2D array of size n * m, Plates[i][j] represents the number of shape of type i
// that fit on a plate of type j
// Decision variables
X // array of integer of size m, will represent the number of plates of each type to use
// Constraints
For all j in 1 .. m, X[j] >= 0 // number of plates cannot be negative
For all i in 1 .. n, sum(j in 1..m) Plates[i][j] * X[j] >= Shapes[i] // all shapes must fit
Objective function:
minimize sum(j in 1..n) X[j]
在 OPL 中编写伪代码,将其提供给 linear programming solver ,考虑到此问题与装箱问题的相似性,您应该可以相当快地获得解决方案。
编辑:如果您不想经历学习 LP 基础知识、OPL、LP 求解器等的麻烦......那么解决此问题的最好和最简单的方法就是一个很好的旧方法 branch and bound这个问题的实现。分支定界法是一种非常简单而强大的算法,可用于解决范围广泛的问题....必知。
关于algorithm - 解决 'toy matching puzzle' 的最佳算法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56456426/
有一个 Person 类,它包含以下内容: 智力玩具 列出 child SumToys() SumToys() 返回直接对象中的所有“玩具”(调用 SumToys)及其直接子对象(不是子对象的子对象)
*Pastebin 的完整日志:* http://pastebin.com/QJyzzxNA 每当我尝试编译The Powder Toy时,它给了我一个链接器错误,说该库与 x86_64 不兼容。我该
基本上我正在制作一款类似于 The Powder Toy 的游戏。在一个给定的帧中,世界最多可以有 256,000 个粒子。在我旧的 Javascript 实现中,我遍历了每个像素,这导致了严重的延迟
我想按两个FromX和ToY字段的值构成的范围内的指定值进行查询,并通过Title查询将text作为query_string字段进行搜索。 这个例子显示了我的目标: Id | FromX | ToY
想象这样的谜题: puzzle 我有几种形状,例如: 10圈 8个三角形 9个正方形 我也有一些盘子用来放形状,例如: A板:2个圆孔,3个三角孔,1个方孔 B板:1个圆孔,0个三角孔,3个方孔 C板
我正在尝试使用行为框架、python 通过 BDD 测试来覆盖项目。问题是所有 BDD Material 都使用不真实的玩具示例。我的项目相当大,我遇到了以下问题 在不同的 .feature 文件中,
这是我正在开发的一个非常简单的模块。它只是将 sys_ni_syscall 替换为我的新系统调用 log_message。这里是源文件template_syscall_hacking.c: #incl
在 Redux+Immutable js 新手中,我发现 toJS 方法存在一些性能问题,但在我的用例中我找不到任何替代方案。那么如何将列表转换为对象数组。 我的初始状态 const initialS
已结束。此问题不符合 Stack Overflow guidelines .它目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是无关紧要的,
请考虑以下简单的MWE:。最佳解决方案是使所有参数相等,而不是介于4和6之间。例如,全部为3。这给出的函数值为0。对于不同的Scipy优化器,优化的成功程度各不相同,但使用DIRECT几乎立即失败。它
请考虑以下简单的MWE:。最佳解决方案是使所有参数相等,而不是介于4和6之间。例如,全部为3。这给出的函数值为0。对于不同的Scipy优化器,优化的成功程度各不相同,但使用DIRECT几乎立即失败。它
我正在使用 exec() 语句来设置一个值,如下所示: foo = 3 def return_4(): return 4 instruction = 'foo = return_4()' ex
我是一名优秀的程序员,十分优秀!