现有VB程序用于判断某图能否一笔画成,若能,则通过穷举边的顺序来找到一种合法的路径。如c图有三条边,第1条由①②相连,第2条由④③相连,第3条由③②相连,若边的顺序231就是其中一种合法的边的顺序,代表先描第2条边,再描第3边,最后描第1条边,对应的路径④→③→②→①。在程序运行过程中,在文本框Text1中输入点的个数n,表示图中有n个点编号为1~n。在文本框Text2中输入若干点的编号,每两个点代表一条边(保证输入为偶数个点),以“,”开头,点击“一笔画”按钮Command1后,在Label3中输出结果,在若能一笔画成,则输出一条路径,否则,提示“无法一笔画!”。程序界面如c图:
c图
Dim n As Integer, t As Integer, m As Integer, I As Integer, j As Integer
Dim ans As Integer, u As Integer, st As Integer, p As Long
Dim a(21) As Integer, b(11) As Integer, f(11) As Integer
Dim c(11)As Integer '用于存储构成一笔画各边的序号
Dim s As String, ss As String
Private Sub Command1_Click()
n= Val(Text1.Text)
s= Text2.Text: t= 0
For j= 1 To Len(s)
ch = Mid(s, j, 1)
If ch = "," Then
b(a(t)) = b(a(t)+1
t=t+1
a(t)= 0
Else
a(t)= a(t)*10 + Val(ch)
End If
Next j
: m=t\2: ans=0
st= 1
For i= 1 To n
If b(i) Mod 2=1 Then ans = ans+ 1: st=i
Next i
If ans=0 Or ans=2 Then
For p=1 To m^m '穷举边的顺序
If try(p) Then
u= st: ss = Str(u)
For j=1 To m
If (a(c(j)* 2)<>u) And (a(c(j)*2-1)<>u) Then Exit For
If Then u= a(c(j)*2- 1) Else u=a(c(j)*2)
ss=ss +"->"+ Str(u)
Next j
If j> m Then Exit For
End If
Next p
Else
ss="无法一笔画!"
End If
Label3.Caption=ss
End Sub
Function try(x As Long) As Boolean '生成边的访问顺序,并判断有无重复的边
Dim k As Integer, y As Long
y=x
For k=1 To m
c(k)=y Mod m+1: y-y\m
If f(c(k)) = x Then Exit For
Next k
If k> m Then try=True Else try = False
End Function