網路城邦
回本城市首頁 唐老鴨之家
市長:  副市長:
加入本城市推薦本城市加入我的最愛訂閱最新文章
udn城市資訊科技網路分享【唐老鴨之家】城市/討論區/
討論區Excel VBA 字體:
上一個討論主題 回文章列表 下一個討論主題
廣向搜尋和檢查距離的VBA程式
 瀏覽381|回應0推薦0


等級:6
留言加入好友
Dim visited(100), found As Boolean
Dim dist(100) As Integer
Dim que(100) As Integer
Dim n, turn As Integer

Private Sub CommandButton6_Click()
n = 24
For j = 1 To n
For i = 1 To n
visited(i) = False
Next i
TextBox1.Text = TextBox1.Text & vbCrLf & vbCrLf & Cells(j, n + 1) & " "
Call BFS(j)
Next j
End Sub

Sub BFS(v)
front = 1
rear = 1
visited(v) = True
que(rear) = v
Do While front <= rear
v = que(front)
front = front + 1
For i = 1 To n
If Cells(v, i) = 1 And Not visited(i) Then
TextBox1.Text = TextBox1.Text & "-->" & Cells(i, n + 1) & " "
visited(i) = True
rear = rear + 1
que(rear) = i
End If
Next i
Loop
End Sub

Private Sub CommandButton7_Click()
n = 24

SS = InputBox("Input Node Label : " & vbCrLf & vbCrLf & "[XXXX]" & vbCrLf & vbCrLf & "e.g. [1234],[1243]")
SS = Val(SS)

For j = 1 To n
If Cells(n + 1, j) = SS Then
For i = 1 To n
visited(i) = False
Next i
TextBox1.Text = TextBox1.Text & vbCrLf & vbCrLf & Cells(j, n + 1) & " "
Call FDS(j)
Exit For
End If
Next j
End Sub

Sub FDS(v)
front = 1
rear = 1
visited(v) = True
dist(v) = 0
que(rear) = v
Do While front <= rear
v = que(front)
front = front + 1
For i = 1 To n
If Cells(v, i) = 1 And Not visited(i) Then
visited(i) = True
dist(i) = dist(v) + 1
TextBox1.Text = TextBox1.Text & "-->" & Cells(i, n + 1) & " (" & dist(i) & ") "

rear = rear + 1
que(rear) = i
End If
Next i
'TextBox1.Text = TextBox1.Text & vbCrLf & "(" & front & ":" & rear & ")"
Loop
End Sub
回應 回應給此人 推薦文章 列印 加入我的文摘

引用
引用網址:http://city.udn.com/forum/trackback.jsp?no=58536&aid=5570426