網路城邦
回本城市首頁 唐老鴨之家
市長:  副市長:
加入本城市推薦本城市加入我的最愛訂閱最新文章
udn城市資訊科技網路分享【唐老鴨之家】城市/討論區/
討論區Excel VBA 字體:
上一個討論主題 回文章列表 下一個討論主題
深向搜尋和檢查迴圈的VBA程式
 瀏覽471|回應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 CommandButton1_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 DFS(j)
Next j
End Sub

Sub DFS(ByVal x As Integer)
visited(x) = True
For i = 1 To n
If Cells(x, i) = 1 And Not visited(i) Then
TextBox1.Text = TextBox1.Text & "-->" & Cells(i, n + 1) & " "
Call DFS(i)
End If
Next i
End Sub


Private Sub CommandButton2_Click()
n = 24
found = False
For j = 1 To n
For i = 1 To n
visited(i) = False
Next i
TextBox1.Text = TextBox1.Text & vbCrLf & vbCrLf & "started from " & Cells(j, n + 1) & vbCrLf
visited(j) = True
found = False
Call CHK(j, 0)
If found Then
TextBox1.Text = TextBox1.Text & vbCrLf & "--> There exists at least one cycle !"
Else
TextBox1.Text = TextBox1.Text & vbCrLf & "--> Do NOT find any cycle !"
End If
Next j
End Sub

Sub CHK(ByVal x As Integer, ByVal px As Integer)
For i = 1 To n
If Cells(x, i) = 1 And visited(i) And i <> px Then
TextBox1.Text = TextBox1.Text & "-->" & Cells(i, n + 1) & " "
found = True
Exit For
End If
Next i
For i = 1 To n
If found Then Exit For
If Cells(x, i) = 1 And Not visited(i) Then
TextBox1.Text = TextBox1.Text & "-->" & Cells(i, n + 1) & " "
visited(i) = True
Call CHK(i, x)
End If
Next i
End Sub

本文於 修改第 2 次
回應 回應給此人 推薦文章 列印 加入我的文摘

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