代價(jià)一致算法 swift3.0 尋找最小路徑 無(wú)中間節(jié)點(diǎn)

最近在研究倉(cāng)庫(kù)沙盤(pán),在多點(diǎn)內(nèi)尋找最短路徑,從迪杰斯特拉到A*都有嘗試,最終用了代價(jià)一致搜索這個(gè)算法。

推薦這個(gè)算法的是我的同事,他告訴我能夠擴(kuò)展到中間節(jié)點(diǎn),中間路過(guò)點(diǎn),可我嘗試了半天, 最終還是放下中間節(jié)點(diǎn)的寫(xiě)法,因?yàn)闀?huì)有bug。

廢話不多說(shuō)了,直接上swift代碼。

classGraph{

//從初始點(diǎn)到當(dāng)前地點(diǎn)的距離之和

vardis =0

//是否被訪問(wèn)

varflag =0

//當(dāng)前節(jié)點(diǎn)的上一個(gè)節(jié)點(diǎn)

varbef =0

}

classGraphString {

varcities = [String]()//地址名

varpath = [[NSInteger]]()//地址距離

}

classpriceIsConsistentAlgorithm {

//

init(notes:GraphString,beginIndex:Int,endIndex:Int) {

varGraphArray = [Graph]()

foriin0..

letgraph =Graph()

graph.dis=0* i//單純?nèi)サ艟?/p>

graph.bef=-1

graph.flag=0

GraphArray.append(graph)

}

ucs_search(notes: notes, begin: beginIndex,end: endIndex, graph: GraphArray)

display(notes: notes, begin: beginIndex, end: endIndex, graph: GraphArray)

}

funcucs_search(notes:GraphString,begin:Int,end:Int,graph:[Graph]){

varlist = [NSInteger]()

//給數(shù)組添加起點(diǎn)

list.append(begin)

graph[begin].flag=1

//數(shù)組不為空時(shí)擴(kuò)展節(jié)點(diǎn)

while!list.isEmpty{

//給數(shù)組排序最小到前面

list =sorTLiset(list: list,graph: graph)

letnod = list.removeFirst()//單獨(dú)拿出最小節(jié)點(diǎn)最小節(jié)點(diǎn)移除

graph[nod].flag=1

//如果當(dāng)前節(jié)點(diǎn)為目標(biāo)節(jié)點(diǎn)則返回

ifnod == end{return}

foriin0..

//當(dāng)某個(gè)節(jié)點(diǎn)和初始點(diǎn)有結(jié)合路徑,并且不在數(shù)組隊(duì)列里,則加入數(shù)組并更新

if(notes.path[nod][i] >=0)&&(graph[i].flag==0) {

if!IsListArray(i:i, list:list){//多點(diǎn)的算法在這里判斷!!!!

list.insert(i,at:0)//插入在第一位

graph[i].dis= graph[nod].dis+ notes.path[nod][i]

graph[i].bef= nod

}elseif(graph[nod].dis+notes.path[nod][i] < graph[i].dis){

//如果此節(jié)點(diǎn)在隊(duì)列里面但是從上一個(gè)結(jié)點(diǎn)到此節(jié)點(diǎn)的總路程小于直接到達(dá)此節(jié)點(diǎn)的路程,則更新dis數(shù)組使

//到達(dá)此節(jié)點(diǎn)的路程為現(xiàn)今的最短,并更新父節(jié)點(diǎn)的值

graph[i].dis= graph[nod].dis+ notes.path[nod][i]

graph[i].bef= nod

print("執(zhí)行了")

}

}

}

}

}

//判斷是否在list中

funcIsListArray(i:NSInteger,list:[NSInteger])->Bool{

forjinlist{

ifj == i {

returntrue

}

}

returnfalse

}

//將dis中最小的距離的結(jié)點(diǎn)移動(dòng)至隊(duì)列最前

funcsorTLiset(list:[NSInteger],graph:[Graph])->[NSInteger]{

vararray = list

varmin = graph.first?.dis

varidx =0

for(i,a)inlist.enumerated(){

ifgraph[a].dis< min!{

min = graph[a].dis

idx = i

}

}

lettemp = list[idx]

array.remove(at: array.index(of:temp)!)

array.insert(temp,at:0)

returnarray

}

funcdisplay(notes:GraphString,begin:Int,end:Int,graph:[Graph]){

varstack = [NSInteger]()//數(shù)組容器充當(dāng)棧先進(jìn)后出

vargoal = end

whilegoal != begin {

stack.append(goal)

goal = graph[goal].bef

}

stack.append(begin)

print("進(jìn)過(guò)的路為:")

while!stack.isEmpty{

print("-->",notes.cities[stack.removeLast()])

}

print("經(jīng)過(guò)的總長(zhǎng)度為:",graph[end].dis)

}

}

以上是算法核心代碼 ?調(diào)用代碼為

graph.cities= ["1","2","3","4","5","6","7","8","9","10",

"11","12","13","14","15","16","17","18","19","20"]

graph.path=

[[0,-1,-1,-1,-1,3,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],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

[-1,-1,3,-1,-1,-1,-1,-1,-1,-1,-1,-1,3,-1,-1,-1,0,-1,-1,-1],// 17 - 3 13

[-1,-1,-1,3,-1,-1,-1,3,-1,-1,-1,3,-1,-1,-1,-1,-1,0,-1,-1],// 18 - 4 8 12

[-1,3,-1,3,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,0,-1],// 19 - 2 4

[-1,-1,-1,-1,3,-1,3,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,0]]// 20 - 5 7

_=priceIsConsistentAlgorithm(notes:graph, beginIndex:0, endIndex:18)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 背景 一年多以前我在知乎上答了有關(guān)LeetCode的問(wèn)題, 分享了一些自己做題目的經(jīng)驗(yàn)。 張土汪:刷leetcod...
    土汪閱讀 12,768評(píng)論 0 33
  • Ⅰ. 一眨眼2017已經(jīng)過(guò)去了很多個(gè)周末,下個(gè)周末就是4月份了。書(shū)稿已經(jīng)整理了一半,書(shū)名一直在《有個(gè)故事》《人人心...
    張善良_閱讀 136評(píng)論 0 0
  • 我不知道自己每天學(xué)習(xí)是為了什么,我想著我要好好學(xué)習(xí),可終會(huì)被身邊的誘惑所吸引,我不知道該怎么辦?我很困惑,突然覺(jué)得...
    易耳朵閱讀 120評(píng)論 0 0
  • 青果上線已經(jīng)快4個(gè)月了。
    青呱騎士閱讀 395評(píng)論 4 0