Shortest path in 2d arrays

前端 未结 3 864
暗喜
暗喜 2020-12-19 15:31
*...*..D
.G..*.....
**...**.
.S....*.
........
...G**..
........
.G..*...

Here is 2d array where
S- Source
D-Destination
G-Point must be

3条回答
  •  自闭症患者
    2020-12-19 15:47

    Here there are many algorithms like dijkstra or BFS but if you need to learn an path finding algorithm then i suggest the A* algorithm as it is quicker than dijkstra or BFS and can be easily implemented on a 2D matrix. As in case of must visit node you can try all sequences in which you visit the nodes for example say S->G1->G2->G3->D find the minimum for this path as min(S,G1)+min(S,G2)+min(G3,D). try all permutations of the G's and get the minimum of them all.

提交回复
热议问题