我将网格表示为列表列表,类型[[Bool]]
. 我会定义一个函数来知道网格元素是否已满:
type Grid = [[Bool]]
isFullAt :: Grid -> (Int, Int) -> Bool -- returns True for anything off-grid
然后我会定义一个函数来查找邻居:
neighbors :: (Int, Int) -> [(Int, Int)]
要查找您的非完整邻居,point
可以使用 过滤filter (not . isFullAt) $ neighbors point
。
在这一点上,我将定义两个数据结构:
- 将每个点映射到
Maybe Cost
- 将具有已知成本的所有点存储在堆中
Initialize with only the start square A in the heap, with cost zero.
Then loop as follows:
- Remove a min-cost square from the heap.
- If it's not already in the finite map, add it and its cost
c
, and add all the non-full neighbors to the heap with cost c+1
.
When the heap is empty, you will have the costs of all reachable points and can look up B in the finite map. (This algorithm may be called "Dijkstra's algorithm"; I've forgotten.)
You can find finite maps in Data.Map
. I assume there's a heap (aka priority queue) somewhere in the vast library, but I don't know where.
I hope this is enough to get you started.