⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.frm

📁 数据结构中经典的汉诺塔算法。就是用递归移动盘子的问题。
💻 FRM
字号:
VERSION 5.00
Begin VB.Form Form1 
   BorderStyle     =   3  'Fixed Dialog
   Caption         =   "汉诺塔(xinfresh编写)"
   ClientHeight    =   5520
   ClientLeft      =   45
   ClientTop       =   435
   ClientWidth     =   9915
   LinkTopic       =   "Form1"
   MaxButton       =   0   'False
   MinButton       =   0   'False
   ScaleHeight     =   5520
   ScaleWidth      =   9915
   ShowInTaskbar   =   0   'False
   StartUpPosition =   3  '窗口缺省
   Begin VB.ComboBox Combo1 
      Height          =   300
      ItemData        =   "Form1.frx":0000
      Left            =   8640
      List            =   "Form1.frx":0002
      TabIndex        =   2
      Text            =   "Combo1"
      Top             =   240
      Width           =   1095
   End
   Begin VB.CommandButton Command1 
      Caption         =   "开  始"
      Height          =   375
      Left            =   8640
      TabIndex        =   1
      Top             =   720
      Width           =   1095
   End
   Begin VB.PictureBox Picture1 
      Height          =   4575
      Left            =   240
      ScaleHeight     =   4515
      ScaleWidth      =   5475
      TabIndex        =   0
      Top             =   240
      Width           =   5535
      Begin VB.Line Line1 
         BorderWidth     =   3
         Index           =   3
         X1              =   4320
         X2              =   120
         Y1              =   4320
         Y2              =   4320
      End
      Begin VB.Line Line1 
         BorderWidth     =   3
         Index           =   2
         X1              =   3360
         X2              =   3360
         Y1              =   1080
         Y2              =   4440
      End
      Begin VB.Line Line1 
         BorderWidth     =   3
         Index           =   1
         X1              =   2160
         X2              =   2160
         Y1              =   1080
         Y2              =   4440
      End
      Begin VB.Line Line1 
         BorderWidth     =   3
         Index           =   0
         X1              =   840
         X2              =   840
         Y1              =   1080
         Y2              =   4440
      End
      Begin VB.Shape Shape1 
         BackColor       =   &H80000013&
         BackStyle       =   1  'Opaque
         BorderWidth     =   2
         FillColor       =   &H00FF0000&
         FillStyle       =   7  'Diagonal Cross
         Height          =   375
         Index           =   0
         Left            =   360
         Top             =   3960
         Width           =   1095
      End
   End
   Begin VB.Label Label2 
      Height          =   375
      Left            =   8640
      TabIndex        =   4
      Top             =   1680
      Width           =   1095
   End
   Begin VB.Label Label1 
      AutoSize        =   -1  'True
      Caption         =   "步数记录:"
      Height          =   180
      Left            =   8640
      TabIndex        =   3
      Top             =   1320
      Width           =   810
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Dim map(2, 5) As Integer
Const def = 32767

Private Sub Combo1_Click()
    Init Val(Combo1.Text) - 1
End Sub
Private Sub Command1_Click()
    MoveHs (0)
    Label2.Caption = "完成!!"
End Sub
Private Sub Form_Load()
    Dim m As Integer
    For m = 1 To 5
        Combo1.AddItem m
    Next
    For m = 1 To 4
        Load Shape1(m)
    Next
    
    Combo1.Text = "5"
    Combo1_Click
    
End Sub

Private Sub MoveHs(ByVal n As Integer)
    Dim m As Integer
    If n = Val(Combo1.Text) - 1 Then
        If Shape1(n).Left < Line1(1).X1 Then
            MoveAction n, 1, 2
            Delays (1)
            MoveAction n, 2, 3
            Delays (1)
        ElseIf Shape1(n).Left > Line1(2).X1 Then
            MoveAction n, 3, 2
            Delays (1)
            MoveAction n, 2, 1
            Delays (1)
        End If
        Exit Sub
    End If
    
    If Shape1(n).Left < Line1(1).X1 Then
        MoveHs (n + 1)
        MoveAction n, 1, 2
        Delays (1)
        MoveHs (n + 1)
        MoveAction n, 2, 3
        Delays (1)
        MoveHs (n + 1)
    ElseIf Shape1(n).Left > Line1(2).X1 Then
        MoveHs (n + 1)
        MoveAction n, 3, 2
        Delays (1)
        MoveHs (n + 1)
        MoveAction n, 2, 1
        Delays (1)
        MoveHs (n + 1)
    End If
    
End Sub
Private Sub Delays(ByVal n As Integer)
    Dim Tstart As Long
    Dim Tend As Long
    Tstart = Timer
    Do While Timer - Tstart < n
        DoEvents
    Loop
End Sub
Private Sub MoveAction(ByVal n As Integer, ByVal s As Integer, ByVal e As Integer)
    Dim locx As Single, locy As Single
    Dim m As Integer
    For m = 0 To 5
        If map(e - 1, m) = def Then
            Clearmap (n)
            map(e - 1, m) = n
            Exit For
        End If
    Next
    locx = Line1(e).X1 - Shape1(n).Width / 2
    locy = Line1(0).Y1 + Shape1(n).Height * (m + 1)
    Shape1(n).Move locx, locy
    Label2.Caption = "第" & CStr(Val(Mid(Label2.Caption, 2, 1)) + 1) & "步"
    DoEvents
End Sub
Private Sub Clearmap(ByVal x As Integer)
    Dim m As Integer, n As Integer
    For m = 0 To 2
        For n = 0 To 5
            If map(m, n) = x Then
                map(m, n) = def
                Exit Sub
            End If
        Next
    Next
End Sub
Private Sub Init(ByVal num As Integer)
    Dim m As Integer, n As Integer
    
    Me.Width = 10000
    Me.Height = 6000
    
    For m = 1 To 4
        Shape1(m).Visible = False
    Next
    
    Picture1.Width = 8000
    Picture1.Height = 5000
    Picture1.Scale (0, Picture1.Height)-(Picture1.Width, 0)
    With Line1(0)
        .X1 = 0
        .Y1 = Picture1.Height * 0.05
        .X2 = Picture1.Width
        .Y2 = .Y1
    End With
    With Line1(1)
        .X1 = Picture1.Width * 0.2
        .Y1 = Picture1.Height * 0.05
        .X2 = .X1
        .Y2 = Picture1.Height * 0.9
    End With
    With Line1(2)
        .X1 = Picture1.Width * 0.5
        .Y1 = Picture1.Height * 0.05
        .X2 = .X1
        .Y2 = Picture1.Height * 0.9
    End With
    With Line1(3)
        .X1 = Picture1.Width * 0.8
        .Y1 = Picture1.Height * 0.05
        .X2 = .X1
        .Y2 = Picture1.Height * 0.9
    End With
    
    With Shape1(0)
        .Width = 1600
        .Height = 400
        .Left = Line1(1).X1 - .Width / 2
        .Top = Line1(0).Y1 + .Height
    End With
    
    For m = 1 To num
        With Shape1(m)
            .Width = Shape1(m - 1).Width - 300
            .Height = 400
            .Left = Line1(1).X1 - .Width / 2
            .Top = Shape1(m - 1).Top + .Height
            .Visible = True
        End With
    Next
    
    For m = 0 To 2
        For n = 0 To 5
            map(m, n) = def
        Next
    Next
    
    For m = 0 To num
        map(0, m) = m
    Next
End Sub

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -