Option Explicit
Private Type sbwfn
num As Long
b_setp As Long
End Type
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Dim uff(1 To 5) As sbwfn
Dim e_tem As sbwfn
Dim e_com As CommandButton
Dim numeric As Byte
Dim time_ss As Long
Dim sd As Boolean
Private Sub Command1_Click()
sd = False
End Sub
Sub yy(tem_str As String) '初使化过程
Dim i As Byte
Dim m As Long
For i = 1 To 5
m = Val(Mid(tem_str, i, 1))
If m = 1 Then
uff(i).b_setp = 1
Else
uff(i).b_setp = -1
End If
Next i
uff(1).num = 30
uff(2).num = 70
uff(3).num = 110
uff(4).num = 170
uff(5).num = 230
numeric = 5
time_ss = 0
End Sub
Private Sub Command3_Click()
Dim a As Byte, b As Byte, c As Byte, d As Byte, e As Byte
Dim zh_string As String
Dim tim As Byte
Dim u As Byte, m As Byte
sd = True
List1.Clear
For a = 1 To 2
For b = 1 To 2
For c = 1 To 2
For d = 1 To 2
For e = 1 To 2
zh_string = a & b & c & d & e '所有可能的组合
yy zh_string '初使化开始值
Do While numeric > 0 And sd '判断所有蚂蚁是否走完的循环
For tim = 1 To numeric '内部循环,判断每只蚂蚁是否到达终点,或起始点
If uff(tim).num = 270 Or uff(tim).num = 0 Then '如果到达终点或起始点
e_tem = uff(tim) ';;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
uff(tim) = uff(numeric) '把该位置上的蚂蚁与最后一只蚂蚁位置对调
uff(numeric) = e_tem ';;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Set e_com = Command2(tim - 1)
Command2(tim - 1).Left = Command2(numeric - 1).Left
Command2(numeric - 1).Left = -100
numeric = numeric - 1
Exit For
Else
For u = 1 To numeric '内部循环判断,检查tim蚂蚁位置是否与每只蚂蚁的位置相同
If uff(tim).num = uff(u).num And tim <> u Then '如果相同
If uff(tim).b_setp > 0 Then ';;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
uff(tim).b_setp = -1
Else '则改变tim蚂蚁的头方向,使其朝相反的方向走
uff(tim).b_setp = 1
End If
End If ';;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Next u '如果与第一只蚂蚁位置不相同,则一直往后逐个检察
End If '结束tim蚂蚁是否到达终点的判断
Next tim
For m = 1 To numeric
uff(m).num = uff(m).num + uff(m).b_setp
Command2(m - 1).Left = uff(m).num
Next m
DoEvents
Sleep 100
time_ss = time_ss + 1
Loop
List1.AddItem zh_string & "-----" & time_ss
Next e
Next d
Next c
Next b
Next a
End Sub
Private Sub Form_Unload(Cancel As Integer)
Unload Me
End Sub
大家帮忙看看我这对吗?有什么错的地方请各位大哥多多指点。