aboutsummaryrefslogtreecommitdiff
path: root/challenge-128/eric-cheung/excel-vba/ch-1.bas
blob: f44af35acc23cf6cde5d1663e4c66281978524e9 (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
Attribute VB_Name = "ModTask_01"
Option Explicit
Option Base 1
Public Const strMyTitle As String = "Eric Cheung"

''Public Const nRowNum As Integer = 3
''Public Const nColNum As Integer = 6

Public Const nRowNum As Integer = 3
Public Const nColNum As Integer = 4

Function bIsMatrixOne _
( _
    ByRef nMatrix, _
    nStartRowIndx As Integer, _
    nStartColIndx As Integer, _
    nEndRowIndx As Integer, _
    nEndColIndx As Integer _
) As Boolean
    
    Dim nRowSubLoop As Integer, nColSubLoop As Integer
    bIsMatrixOne = False
    
    For nRowSubLoop = nStartRowIndx To nEndRowIndx
        For nColSubLoop = nStartColIndx To nEndColIndx
            If nMatrix(nRowSubLoop, nColSubLoop) = 1 Then
                bIsMatrixOne = True
                Exit Function
            End If
        Next nColSubLoop
    Next nRowSubLoop
    
End Function

Sub Task_01()
    
    Dim nBinArr(1 To nRowNum, 1 To nColNum) As Integer
    Dim nRowLoop As Integer, nColLoop As Integer, nRowNestLoop As Integer, nColNestLoop As Integer
    Dim nRowZeroMax As Integer, nColZeroMax As Integer
    Dim nTempRowNum As Integer, nTempColNum As Integer
    Dim strMsg As String
    
    'Initialize
''    nBinArr(1, 1) = 1: nBinArr(1, 2) = 0: nBinArr(1, 3) = 0: nBinArr(1, 4) = 0: nBinArr(1, 3) = 1: nBinArr(1, 6) = 0
''    nBinArr(2, 1) = 1: nBinArr(2, 2) = 1: nBinArr(2, 3) = 0: nBinArr(2, 4) = 0: nBinArr(2, 3) = 0: nBinArr(2, 6) = 1
''    nBinArr(3, 1) = 1: nBinArr(3, 2) = 0: nBinArr(3, 3) = 0: nBinArr(3, 4) = 0: nBinArr(3, 3) = 0: nBinArr(3, 6) = 0
    
    nBinArr(1, 1) = 0: nBinArr(1, 2) = 0: nBinArr(1, 3) = 1: nBinArr(1, 4) = 1
    nBinArr(2, 1) = 0: nBinArr(2, 2) = 0: nBinArr(2, 3) = 0: nBinArr(2, 4) = 1
    nBinArr(3, 1) = 0: nBinArr(3, 2) = 0: nBinArr(3, 3) = 1: nBinArr(3, 4) = 0
    
    If Application.Sum(nBinArr) = nRowNum * nColNum Then
        'All 1
        nRowZeroMax = 0: nColZeroMax = 0
    Else
        nRowZeroMax = 1: nColZeroMax = 1
        
        For nRowLoop = 1 To nRowNum
            For nColLoop = 1 To nColNum
                If nBinArr(nRowLoop, nColLoop) = 0 Then
                    For nRowNestLoop = nRowLoop + 1 To nRowNum
                        For nColNestLoop = nColLoop + 1 To nColNum
                            If Not bIsMatrixOne(nBinArr, nRowLoop, nColLoop, nRowNestLoop, nColNestLoop) Then
                                nTempRowNum = nRowNestLoop - nRowLoop + 1
                                nTempColNum = nColNestLoop - nColLoop + 1
                                If nTempRowNum * nTempColNum > nRowZeroMax * nColZeroMax Then
                                    nRowZeroMax = nTempRowNum
                                    nColZeroMax = nTempColNum
                                End If
                            End If
                        Next nColNestLoop
                    Next nRowNestLoop
                End If
            Next nColLoop
        Next nRowLoop
    End If
    
    strMsg = "Max. SubMatrix having only : " & nRowZeroMax & " * " & nColZeroMax

    MsgBox strMsg, vbOKOnly, strMyTitle
    
End Sub