'1----2----3
'| | |
'4----5----6
'| | |
'7----8----9
'田字形网格上有9个点 , 现要求的是
'编程列举出从任意一点到另外任意一点的所有路径. (每个点只能走一次)
将以下内容存为文本文件,并将文件扩展名改为 .frm
VERSION 5.00
Begin VB.Form Form1
Caption = "Form1"
ClientHeight = 3930
ClientLeft = 60
ClientTop = 345
ClientWidth = 7620
LinkTopic = "Form1"
ScaleHeight = 3930
ScaleWidth = 7620
StartUpPosition = 3 '窗口缺省
Begin VB.ComboBox Combo2
Height = 300
Left = 2160
TabIndex = 6
Text = "Combo1"
Top = 2400
Width = 615
End
Begin VB.ComboBox Combo1
Height = 300
Left = 600
TabIndex = 5
Text = "1"
Top = 2400
Width = 615
End
Begin VB.TextBox Text3
Height = 3855
Left = 3120
MultiLine = -1 'True
ScrollBars = 2 'Vertical
TabIndex = 4
Top = 0
Width = 4455
End
Begin VB.CommandButton Command1
Caption = "求解"
Height = 375
Left = 1920
TabIndex = 0
Top = 3240
Width = 1095
End
Begin VB.Label Label2
AutoSize = -1 'True
Caption = "终点:"
Height = 180
Index = 1
Left = 1560
TabIndex = 3
Top = 2490
Width = 540
End
Begin VB.Label Label2
AutoSize = -1 'True
Caption = "起点:"
Height = 180
Index = 0
Left = 0
TabIndex = 2
Top = 2490
Width = 540
End
Begin VB.Label Label1
Caption = $"Form1.frx":0000
Height = 1455
Left = 0
TabIndex = 1
Top = 240
Width = 3015
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
'1----2----3
'| | |
'4----5----6
'| | |
'7----8----9
'田字形网格上有9个点 , 现要求的是
'编程列举出从任意一点到另外任意一点的所有路径. (每个点只能走一次)
Dim a(2, 2) As Boolean, x, y, b(8)
Private Sub Command1_Click()
Dim wzs As Integer, mbs As Integer
Erase a
x = Array(-1, 0, 1, 0)
y = Array(0, -1, 0, 1)
wzs = Combo1.Text: mbs = Combo2.Text
a((wzs - 1) Mod 3, (wzs - 1) / 3) = True: b(0) = wzs
Text3 = " 从起点位置【" & wzs & "】到终点位置【" & mbs & "】的不重复的路径为:" & vbCrLf
Call dg(1, wzs, mbs, a)
End Sub
Sub dg(t, wz, mb, a)
Dim i As Integer, x1 As Integer, y1 As Integer, sz As Integer, j As Integer
For i = 0 To 3
x1 = (wz - 1) Mod 3 + x(i): y1 = (wz - 1) / 3 + y(i)
If x1 >= 0 And y1 >= 0 And x1 < 3 And y1 < 3 Then
If a(x1, y1) = False Then
sz = y1 * 3 + x1 + 1: b(t) = sz
If sz = mb Then
For j = 0 To t
Text3 = Text3 & b(j) & IIf(j < t, " → ", "")
Next: Text3 = Text3 & vbCrLf
Else
a(x1, y1) = True
dg t + 1, sz, mb, a
a(x1, y1) = False
End If
End If
End If
Next
End Sub
Private Sub Form_Load()
Dim i As Integer
For i = 1 To 9
Combo1.AddItem i
Combo2.AddItem i
Next
Combo1.Text = 1
Combo2.Text = 9
End Sub