增加了路况功能:0修路不通行;1单向通行;2双向通行
自定义排序函数代码在附件中。
- Sub lqxs_zd()
- ks = [a2]
- js = [b2]
- R = Sheets("题目").Range("a" & Rows.Count).End(xlUp).Row
- arr = Sheets("题目").Range("a2:d" & R)
- ReDim a2rr(1 To UBound(arr) * 2, 1 To UBound(arr, 2) + 1)
- ReDim jilurr(1 To UBound(a2rr), 1 To 2)
- ii = 0
- For i = 1 To UBound(arr)
- If arr(i, 4) > 0 Then
- ii = ii + 1
- a2rr(ii, 1) = arr(i, 1)
- a2rr(ii, 2) = arr(i, 2)
- a2rr(ii, 3) = arr(i, 3)
- End If
- If arr(i, 4) > 1 Then
- ii = ii + 1
- a2rr(ii, 1) = arr(i, 2)
- a2rr(ii, 2) = arr(i, 1)
- a2rr(ii, 3) = arr(i, 3)
- End If
- Next
- a2rr = YjhSort0(a2rr, "a,1", "1,3") '按开始点、距离升序
- juli = 0
- lujin = "-" & ks '路径
- If ks = js Then
-
- Else
- Call chazao(ks, js, a2rr, juli, lujin, jilurr, jls)
- End If
- R = Range("c" & Rows.Count).End(xlUp).Row
- Range("c2:d" & R).ClearComments
- If jls > 0 Then
- Range("c2").Resize(UBound(jilurr), 2) = jilurr
- Else
- [c2] = ""
- [d2] = "未找到路径"
- End If
-
- End Sub
- Sub chazao(ks, js, a2rr, juli, lujin, jilurr, jls)
- For i = 1 To UBound(a2rr) '可二分法提速
- If ks = a2rr(i, 1) Then Exit For
- Next
- If i > UBound(a2rr) Then Exit Sub
- If ks = a2rr(i, 2) Then
- ii = 1 '原路返回了
- Else
- ii = 0
- End If
- Do
- If a2rr(i + ii, 1) <> ks Then
- Exit Do '该点所有路径走完
- End If
-
- If a2rr(i + ii, 2) = js Then '找到终点
- jls = jls + 1
- If jls = 1 Then
- jilurr(1, 1) = juli + a2rr(i + ii, 3)
- jilurr(1, 2) = Mid(lujin, 2) & "-" & js
- ElseIf jilurr(1, 1) > juli + a2rr(i + ii, 3) Then
- jilurr(jls, 1) = jilurr(1, 1)
- jilurr(jls, 2) = jilurr(1, 2)
- jilurr(1, 1) = juli + a2rr(i + ii, 3)
- jilurr(1, 2) = Mid(lujin, 2) & "-" & js
- Else
- jilurr(jls, 1) = juli + a2rr(i + ii, 3)
- jilurr(jls, 2) = Mid(lujin, 2) & "-" & js
- End If
- Else
- If Not lujin Like "*-" & a2rr(i + ii, 2) & "-*" Then '无重复点,找下一点
- ks1 = a2rr(i + ii, 2)
- juli1 = juli + a2rr(i + ii, 3)
- lujin1 = lujin & "-" & ks1
- Call chazao(ks1, js, a2rr, juli1, lujin1, jilurr, jls)
- End If
- End If
- ii = ii + 1
- Loop Until i + ii > UBound(a2rr)
- End Sub
复制代码
|