编写一个VB程序实现如下功能:单击“开始循迹”按钮Command1,呈现10*10矩阵地图,并随机生成4个循迹点,分别用数字1表示,数字0表示非循迹点;计算4点中相距最远的两点的直线距离,显示在标签Label1中。程序运行界面如图b所示。
Const n = 10
Const m=4
Dim b(1 To 8) As Integer
Private Sub Command1_Click()
Dim f(1 To n* n) As Integer
Dim i As Integer, j As Integer
Dim w As Integer, k As Integer
Dim length As Single, maxlen As Single
Dim s As String
'生成矩阵地图和随机生成4个循迹点,0表示非循迹点,1表示循迹点,存于数组f中,代码略。
For i= 1 To n^2 '显示矩阵地图和4个循迹点
s = s+ Str(f(i))
If i Mod n=0 Then
List1. AddItem s
s=" "
End If
Next i
w=1
For i=1 To n
For j=1 To n
k = (n+1)* ((i-1) Mod 2)+(-1)-(i-1) * j
If Then '记录循迹点所在位置
b(2*w-1)=i
b(2*w)=k
w=w+1
End If
Next j
Next i
i=1
maxlen = 0
Do While i<=m-1
For j=i+1 To m
If length > maxlen Then maxlen = length
Next j
i= i+1
Loop
Label1. Caption = Str( maxlen)
End Sub
Function reck(x As Integer, y As Integer) As Single
Dim c As Integer, d As Integer
Dim h As Single
c=
d=Abs(b(2*x)-b(2*y))
h=Sqr(c^2+d^2)
reck = h
End Function