aboutsummaryrefslogtreecommitdiff
path: root/challenge-121/eric-cheung/excel-vba/ch-2.bas
blob: 1a3afb2b08f45b8176d1f23e80806fce743e9bfe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
Attribute VB_Name = "ModTask_02"
Option Explicit

Function GetMatrixPath(RngFill As Range, ByRef nMinCost As Integer)

    Dim nMatrixSize As Integer, nCntLoop As Integer
    Dim nSubLoop As Integer, nCheckLoop As Integer
    Dim nArrCnt() As Integer, nArrPath() As Integer
    Dim bArrTempUsed() As Boolean, bArrCheck() As Boolean
    Dim nCost As Integer
    Dim bPathValid As Boolean, bFirstPath As Boolean
    Dim nLastPos As Integer
    
    nMatrixSize = RngFill.Rows.Count
    
    ReDim nArrCnt(1 To nMatrixSize)
    ReDim nArrPath(1 To nMatrixSize)
    ReDim bArrTempUsed(1 To nMatrixSize)
    ReDim bArrCheck(1 To nMatrixSize)
    
    'Initialize
    nArrCnt(1) = 1
    
    bFirstPath = True
    nMinCost = 0
    
    '''Loop
    nCntLoop = 1
    Do
        Do While nArrCnt(nCntLoop) <= nMatrixSize
            Do While nCntLoop < nMatrixSize
                nCntLoop = nCntLoop + 1
                nArrCnt(nCntLoop) = 1
            Loop
            
            '' ===== Check Path Is Valid =====
            bPathValid = True
            For nSubLoop = 1 To nMatrixSize
                bArrTempUsed(nSubLoop) = False
            Next nSubLoop
            
            For nSubLoop = 1 To nMatrixSize
                If bArrTempUsed(nArrCnt(nSubLoop)) Then
                    bPathValid = False
                    Exit For
                Else
                    bArrTempUsed(nArrCnt(nSubLoop)) = True
                End If
            Next nSubLoop
            '' ===== Check Path Is Valid =====
            
            If bPathValid Then
                nCost = 0
                nLastPos = nArrCnt(1)
                For nSubLoop = 1 To nMatrixSize
                    If nSubLoop = nMatrixSize Then
                        nCost = nCost + RngFill(nLastPos, nArrCnt(1))
                    Else
                        nCost = nCost + RngFill(nLastPos, nArrCnt(nSubLoop + 1))
                        nLastPos = nArrCnt(nSubLoop + 1)
                    End If
                Next nSubLoop
                
                If bFirstPath Or nCost < nMinCost Then
                    For nSubLoop = 1 To nMatrixSize
                        nArrPath(nSubLoop) = nArrCnt(nSubLoop)
                    Next nSubLoop
                    nMinCost = nCost
                    bFirstPath = False
                End If
            End If

            nArrCnt(nCntLoop) = nArrCnt(nCntLoop) + 1
        Loop
        
        nCntLoop = nCntLoop - 1
        nArrCnt(nCntLoop) = nArrCnt(nCntLoop) + 1
        
    Loop Until nArrCnt(1) > nMatrixSize
    
    GetMatrixPath = nArrPath

End Function

Private Sub Task_02_Func(rngToCheck As Range)
    
    Dim arrResult
    Dim nPathCost As Integer, nLoop As Integer
    Dim strTourPath As String
    
    If rngToCheck.Rows.Count <> rngToCheck.Columns.Count Then
        Exit Sub
    End If

    arrResult = GetMatrixPath(rngToCheck, nPathCost)
    
    For nLoop = LBound(arrResult) To UBound(arrResult)
        If strTourPath <> "" Then
            strTourPath = strTourPath & ", "
        End If
        strTourPath = strTourPath & Format(arrResult(nLoop) - 1, "0")
    Next nLoop
    
    strTourPath = strTourPath & ", " & Format(arrResult(1) - 1, "0")
    strTourPath = "Min Cost: " & nPathCost & ", " & "Tour: " & strTourPath
    
    MsgBox strTourPath
    
End Sub

Private Sub Task_02_GenTable(nNum)

    Dim nLoop_01 As Integer, nLoop_02 As Integer
    Dim rngPos As Range
    
    Set rngPos = Range("B9")
    
    For nLoop_01 = 1 To nNum
        With rngPos.Offset(nLoop_01, 0)
            .Value = nLoop_01 - 1
            .HorizontalAlignment = xlCenter
        End With
        
        With rngPos.Offset(0, nLoop_01)
            .Value = nLoop_01 - 1
            .HorizontalAlignment = xlCenter
        End With
    
        For nLoop_02 = 1 To nNum
            With rngPos.Offset(nLoop_01, nLoop_02)
                .Value = Int((8 - 1 + 1) * Rnd + 1)
                .HorizontalAlignment = xlCenter
            End With
        Next nLoop_02
    Next nLoop_01
    
    Task_02_Func Range(rngPos.Offset(1, 1), rngPos.Offset(nNum, nNum))
    
End Sub

Sub Task_02()
    Task_02_Func Range("Rng_Task02")
End Sub

Sub Task_02_Bonus_1()

    Dim varInputNum
    
    Do
        varInputNum = InputBox("Please enter a number", strMyTitle, 5)
        If varInputNum = "" Then Exit Sub
    Loop Until varInputNum > 1
    
    Task_02_GenTable varInputNum

End Sub

Sub Task_02_Bonus_2()

    Dim varInputNum
    
    Do
        varInputNum = InputBox("Please enter either 12 or 15", strMyTitle, 12)
        If varInputNum = "" Then Exit Sub
    Loop Until varInputNum = 12 Or varInputNum = 15
    
    Task_02_GenTable varInputNum

End Sub