过河者的一般 DFS
我正在尝试编写一个 DFS 来解决多个过河问题(狐狸山羊白菜、嫉妒的丈夫、雇佣兵和食人族等)。我已经编写了谜题类,但在构造我的求解器时遇到了困难。我了解 DFS 的工作原理,但我不知道从哪里开始使其适应这种设计。
每个谜题都有一个 move() 方法,如果移动有效则返回 true,如果违反规则集则返回 false。乘客在一对代表河流两侧的列表中进行跟踪。求解器可以访问这些列表,但我不确定如何使用它来生成可能的移动集,因为每次带乘客过河时,可能的移动都会发生变化。
I'm trying to write a DFS to solve multiple river crossing problems (Fox Goat Cabbage, Jealous Husbands, Mercenaries and Cannibals, etc.). I've written the puzzle classes, but I'm having trouble structuring my solver. I understand how DFS works, but I can't figure out where to start to adapt it to this design.
Each puzzle has a move() method which returns true if it's a valid move, or false if it breaks the ruleset. The passengers are tracked in a pair of lists which represent each respective side of the river. The solver has access to these lists, but I'm not sure how to use this to generate a possible move set, as the possible moves will change each time a passenger is taken across the river.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在状态(或节点)对象中创建一个
validMoves
方法,该方法返回一个数组、一个Collection
,或者更好的是,一个Iterator
超过移动(或超过有效状态)。从搜索算法中调用它(在“节点扩展”阶段)。Create a
validMoves
method in your state (or node) objects that returns an array of, aCollection
of, or, better yet, anIterator
over moves (or over valid states). Call this from the search algorithm (in the "node expansion" phase).