对不起,我忍不住
我在Yahoo! Answers 上看到了同样的谜题(呃问题)弹出窗口,我
回答了。
实施最初是基于创建树的设计
遍历图;但它最终与设计相匹配
早前由Alex Brown表达。
最初是planning was done in Haskell,因此这个辅助函数:
fun replicate len el =
if len = 0 then nil else el::replicate (len -1) el
主要实现:
fun routes dst (edges:(int * int) list) src =
let val (very_possible,remotely_possible) =
if null edges
then (nil,nil)
else List.partition ((fn s=> s = src) o #1) edges
val (raw_solutions,dsts_is_nx_srcs) =
List.partition ((fn d => d = dst) o #2) very_possible
val solutions = replicate (length raw_solutions) [src,dst]
val full_rest_routes =
let val rest_rest_routes =
map (routes dst remotely_possible)
( map #2 dsts_is_nx_srcs )
in map (fn lst => src::lst) (List.concat rest_rest_routes)
end
in case (very_possible, solutions, remotely_possible)
of (nil, _, _) => nil
| (_::_, (p::ps), _) => solutions @ full_rest_routes
| (_::_, nil, _::_) => full_rest_routes
| (_ , nil, nil ) => nil
end
用户界面:
fun getPaths edges src dst = routes dst edges src
以上代码来自routes4.sml;
但是省略了测试和IO。虽然时间不长,但还是很期待
它可以更简单。