最近在研究倉(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],// 1 - 6
[-1,0,-1,-1,3,-1,-1,-1,-1,-1,-1,-1,-1,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