作者:
Leeng (Leeng)
2015-11-03 09:37:56所以說啊 人家英國5歲小孩就會寫程式才是對的
雖然我推文講過了
之前修張耀文的演算法,第一堂課就放海角七號,告訴我們郵差怎麼規劃路徑
可見與郵差工作最相關的就是演算法
路徑規劃的精神就是走cost最低的路線,走過的盡量不要重複
最常用的就是A*演算法
嫌太難,再不然可以考Dijkstra演算法,但是要背出 pseudo code
背科是文組拿手,這個只有短短十幾行,應該不難
貼在這邊,不用謝我
function Dijkstra(Graph, source):
create vertex set Q
for each vertex v in Graph: // Initialization
dist[v] ← INFINITY // Unknown distance from source to v
prev[v] ← UNDEFINED // Previous node in optimal path from source
add v to Q // All nodes initially in Q (unvisited nodes)
dist[source] ← 0 // Distance from source to source
while Q is not empty:
u ← vertex in Q with min dist[u] // Source node will be selected first
remove u from Q
for each neighbor v of u: // where v is still in Q.
alt ← dist[u] + length(u, v)
if alt < dist[v]: // A shorter path to v has been found
dist[v] ← alt
prev[v] ← u
return dist[], prev[] 1 function Dijkstra(Graph, source):
再不然,也可以考怎麼操作Google地圖。