【发布时间】:2012-03-24 11:27:25
【问题描述】:
三个食人者和三个传教士必须过河。他们的船只能载两个人。如果食人族的人数超过传教士,在河的两边,传教士就有麻烦了(我不会描述结果)。每个传教士和每个食人者都可以划船。六个人怎么能过河?
我找不到使用 IDDFS(迭代深化深度优先搜索)和 GreedyBFS(贪婪最佳优先搜索)解决此问题的算法。关于如何解决这个问题的想法也会让我很高兴。
已编辑:
我在 wiki 上找到了 IDDFS 的算法:
IDDFS(root, goal)
{
depth = 0
repeat
{
result = DLS(root, goal, depth)
if (result is a solution)
return result
depth = depth + 1
}
}
DLS(node, goal, depth)
{
if (depth == 0 and node == goal)
return node
else if (depth > 0)
for each child in expand(node)
DLS(child, goal, depth-1)
else
return no-solution
}
但我无法弄清楚 DLS() 中的 expand(node) 应该在我的问题中完成什么。 这是我的节点类:
public class Node{
//x-no of missionaries
//y-no of cannibals
//z-position of boat
int x,y;
boolean z;
Node(int x,int y,boolean z){
this.x=x;
this.y=y;
this.z=z;
}
public int getM(){
return this.x;
}
public int getC(){
return this.y;
}
public boolean getB(){
return this.z;
}
public void setM(int x){
this.x=x;
}
public void setC(int y){
this.y=y;
}
public void setB(boolean z){
this.z=z;
}
}
我将不胜感激。
【问题讨论】:
-
您是否正在苦苦思索如何通过搜索来探索状态空间,或者最佳路径的标准应该是什么?
-
“六个人怎么能过河?” 好吧,如果你的意思是“活着或半消化”,问题就简单了! ;)
-
这通常不是搜索方法,而是您对问题的表述。这个问题中所有可能的动作和状态是什么?另请注意,人们可以在河的任何一侧上船,而不仅仅是他们开始的一侧。
-
我正在尝试使用 IDS 和 GBFS 实现搜索算法。但我无法弄清楚它们(IDS 和 GBFS)是如何工作的。
-
如果船上有大部分食人族,那会怎样?
标签: java algorithm artificial-intelligence river-crossing-puzzle