所以我正在对一个 8 谜题进行 A* 搜索,它似乎有效,但问题是一旦找到解决方案,它就会继续遍历优先级映射并输出所有可能性。一旦我满足条件,有没有办法停止并输出?
注意:我觉得用 for 循环让 doseqs 变得懒惰,让它们不评估整个事情会是最好的。有没有办法做到这一点?
这是我的代码:
(defn a-star
([board history]
(if (at-end? board) (print-board board)
(let [options (filter #(possible-move? % board) *moves*)
move (into (pm/priority-map) (for [move options] [move (global-man-dis (move-tile board move))]))]
(doseq [pair move :let [next-move (key pair)]]
(print-board (move-tile board next-move))
(println)
(a-star (move-tile board next-move) next-move (conj history board))
)
)
))
([board prev-move history]
(if (or (at-end? board) (history-check history board)) (print-board board)
(let [options (get-queue board (dont-go-back prev-move))
move (into (pm/priority-map) (for [move options] [move (global-man-dis (move-tile board move))]))]
(doseq [pair move :let [next-move (key pair)]]
(print-board (move-tile board next-move))
(println)
(a-star (move-tile board next-move) next-move (conj history board))
)
)
)))
(defn -main [& args]
(println "insert a list all numbers no spaces or letters")
(def board (mapv (fn [^Character c] (Character/digit c 10)) (read-line)))
;(def testt [0 8 4 7 2 1 3 5 6])
;(def testt [1 2 3 5 4 6 8 0 7])
(a-star board [])
)