- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用动态编程解决类似桥梁和 torch 的问题。有关此问题的更多信息,请参见维基百科 (http://en.wikipedia.org/wiki/Bridge_and_torch_problem)。故事是这样的:
Four people come to a river in the night. There is a narrow bridge, but it can only hold two people at a time. They have one torch and, because it's night, the torch has to be used when crossing the bridge. Person A can cross the bridge in one minute, B in two minutes, C in five minutes, and D in eight minutes. When two people cross the bridge together, they must move at the slower person's pace. The question is, can they all get across the bridge in 15 minutes or less?
现在我已经设法使用图来解决问题,但我不知道如何使用动态规划来解决此类问题。您如何将问题分解为子问题?以及子问题的解如何导致整个问题的最优解?有哪些阶段和状态?
有人知道如何使用 DP 解决这个问题吗?也许告诉我如何用 Java 解决这个难题?
最佳答案
我已经用 python 为 torch 和桥梁拼图编写了代码,您可以在其他语言中使用相同的逻辑。
src=[1,2,5,8]
src=sorted(src)
def minimum_time(src):
count=0
if len(src)==3:
count=sum(src)
else:
count=src[1]*(len(src)-2)+src[0]*(len(src)//3)
for i in range(len(src)-1,-1,-2):
#print(src[i])
count+=src[i]
return(count)
print(minimum_time(src))
关于java - 用动态规划解决桥梁和 torch 难题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25399477/
我想将 JavaScript 函数移动到 来自输入标签的标签,但它不起作用。 这个有效: 这不是: function FieldOnKeyUp() { this.value=this.
我遇到了这个问题:说给定两个权重1和3,您可以权衡1,2(乘以3-1),3,4(乘以3 + 1)(使用平衡的两面)。现在找到最小的砝码数量,以便可以测量1到1000。 答案是1,3,9,27 ...
这是代码 war 的套路,我似乎无法弄明白。我以前从未使用过 JavaScript。 我知道答案可能很简单,但即使经过许多小时的搜索,我似乎也无法弄清楚他们在寻找什么。我知道 greet 函数中的 n
在完成一项学校作业时,我有一个抽象类 Person、一个扩展 Person 的抽象类 Student 和一个扩展学生的普通类 CollegeStudent。 CollegeStudent 从文件中读取
下面的代码让我很头疼 var somearr = [1, 2, 3]; function operations() { for (var i
我在 3 个文件中有以下代码: Defines.h #ifndef Defines_h extern const unsigned int SIZE; #endif Defines.cpp #incl
我的任务是尝试创建一个从文本文档中删除个人信息的自动化系统。 电子邮件、电话号码相对容易删除。名字不是。这个问题很难,因为文档中有需要保留的名称(例如,引用资料、名人、人物等)。需要从内容中删除作者姓
我卡在这里了... #include #define DBG_LVL(lvl, stmt) \ do{ \ if(lvl>1) printf stmt; \ }while(0) #defi
我正在尝试使用动态编程解决类似桥梁和 torch 的问题。有关此问题的更多信息,请参见维基百科 (http://en.wikipedia.org/wiki/Bridge_and_torch_probl
我有数组 A[0...N]的 double和数组 B[0...N]的 int .每B[i]变化在 [0...P] .我只需要计算数组 C[0...P] : C[j] = SUM( A[i] : B[i
我目前在使用 jQuery 中的scrollTop() 函数时遇到困难。目前,平滑滚动功能正在滚动经过预期部分,然后在功能完成运行后弹回该部分。我在本文末尾添加了一个 jsFiddle,但这是我目前的
PHP代码 $t = strtotime( '2012-09-21T03:00:00+00:00 America/Chicago' ); $t2 = date('c',$t); echo $t2;
我知道使用 .运算符将函数链接在一起,如下所示: isLessThanZero x | x a -> a -> a 还可以看到: subtract :: Num a => a -> (a ->
PHP代码 $t = strtotime( '2012-09-21T03:00:00+00:00 America/Chicago' ); $t2 = date('c',$t); echo $t2;
我创建了两个 jar 文件 my.common.jar,其中包含辅助类和方法(主要是静态方法)。我还创建了一个 jar 文件 test.jar,其中包含一个 main 方法,该方法调用 my.comm
已解决:@Desolator 已让我的代码在下面的评论中完全正常工作 好的,所以我创建了 3 个类,它们都相互链接: 启动画面 > 项目分配 > CompareSignature 我想谈论的类是闪屏类
我正在尝试使用 firestore 的 .where() 功能来检测某个字符串是否在数据库的数组中。我曾尝试通过添加方括号和其他东西来表示数组的一部分来操纵函数的第一个参数,但无济于事。 //in t
我有一个 PHP 系统,允许用户以 1 - 5 的范围对照片进行投票,我想要做的是突出显示两个人给彼此相同的投票/分数的地方。我目前无法弄清楚我的 PHP 函数的 SQL。 数据库看起来像这样 id,
我在使用 SQLAlchemy 处理 Unicode 时遇到了一个奇怪的问题。简而言之,当我将 Python unicode 字符串插入 Unicode 列时我的 MySQL 数据库,我可以毫不费力地
我正在尝试使用 Selenium 自动执行 Google 翻译网络界面(但无需了解 Selenium 即可理解此问题,只需要知道它会找到元素并单击它们即可)。我一直在选择要翻译的语言。 我无法打开下拉
我是一名优秀的程序员,十分优秀!