上級177回のコード

Option Explicit


'数字のフラグ完了時に開始位置を記録する
'隙間を埋める処理に完了済みの数字の黒を含める

Sub test10()

    
    Dim セット番号 As Integer
    Dim マス番号 As Integer
    

    

    Dim sum As Integer


    Dim マス数(1) As Integer
    
    Dim numsArr(1) As Variant
    Dim numsSet() As Variant
    Dim nums As Collection
    
    Dim nd As NumData

    Dim 行列 As Integer
    Const 行 As Integer = 0
    Const 列 As Integer = 1


    'セットの塗りつぶし状況
    Dim 現状() As Integer
    'セット内の空白の数
    Dim 残数 As Integer
    'セット内の黒の数
    Dim 黒数 As Integer
    


    '全体処理 ループ判定フラグ
    Dim mainFg As Boolean
    Dim 次無Fg As Boolean
    Dim 前後閉鎖Fg As Boolean
    Dim 空白出現Fg As Boolean
    
    Dim dg As Boolean
    
    'カウンタ変数
    Dim i As Integer
    Dim 空白 As Integer
    
    
    Dim 基点 As Range
    
    Set 基点 = ThisWorkbook.Worksheets(2).Range("H98")
    
    マス数(行) = 10
    マス数(列) = 15

'++++++++++++++++++++++++++++++++++++++++++++++
'
'   すべての数字の取り込み
'
'++++++++++++++++++++++++++++++++++++++++++++++
    
    '行と列の処理のループ 0:行 1:列
    For 行列 = 0 To 1
    
        ReDim numsSet(1 To マス数(行列))
        
        '1マスずつ処理するループ
        For マス番号 = 1 To マス数(行列)
    
    
            '数字の取り込み
            Set nums = New Collection
            i = 0
            sum = 0
    
            Do While myOffset(基点, i, マス番号, 行列) <> ""
                Set nd = New NumData
                nd.num = myOffset(基点, i, マス番号, 行列)
                nums.Add nd

                i = i - 1
            Loop
            
            Set numsSet(マス番号) = nums
    
        Next マス番号
        
        numsArr(行列) = numsSet
    
    Next 行列
    
    
    
'++++++++++++++++++++++++++++++++++++++++++++++
'
'   メインの処理ここから
'
'++++++++++++++++++++++++++++++++++++++++++++++

    mainFg = True
    
    
    '全体のループ 塗れなくなるまで繰り返す
    Do While mainFg
        DoEvents
        
        '1か所でも新たに塗りつぶしが出来たらTrueにしてループ続行
        '塗れない場合はループ終了なので、最初にFalseにしておく
        mainFg = False
        

        '行と列ぞれぞれの処理 0:1:列
        For 行列 = 0 To 1
        
            'セットのループ
            For セット番号 = 1 To マス数(行列)
                ReDim 現状(1 To マス数((行列 - 1) * -1))
                
                
                myOffset(基点, 0, セット番号, 行列).Activate
                If セット番号 = 15 And 行列 = 1 Then
                    'デバッグ用 特定の行列で止めたい時に使う
'                    dg = True
                Else
                    dg = False
                End If
                
                If myOffset(基点, 0, セット番号, 行列) <> "" Then
                    
                    残数 = 0
                    黒数 = 0
                    '+++ 現状の状態を取得 +++++++++++++++++++++++++++++++
                    
                    'マスのループ
                    For マス番号 = 1 To マス数((行列 - 1) * -1)
                        Select Case myOffset(基点, マス番号, セット番号, 行列).Interior.Color
                        
                            Case RGB(0, 0, 0)
                                現状(マス番号) = 1
                                黒数 = 黒数 + 1
                                
                                
                            Case RGB(255, 240, 230)
                                現状(マス番号) = 2

                                
                            Case Else
                                現状(マス番号) = 0
                                残数 = 残数 + 1
                        End Select
                        
                        
                    Next マス番号
                
                    
                    '+++ 数字が完了しているかの判定と完了時の処理 +++++++++++++++++++++++++++++++
                    '両端から同じ処理を行う
                    
                    Dim k As Integer
                    Dim m As Integer
                    Dim cnt As Integer
                    
                    
                    
                    
                    '数字を左(上)からみていく
                    m = 1
                    For i = numsArr(行列)(セット番号).Count To 1 Step -1
                        Set nd = numsArr(行列)(セット番号)(i)
                        
                        '完了済みの数字ならスキップ
                        If Not nd.fg Then
                        
                            空白出現Fg = False
                            cnt = 0 '連続する黒の個数
                            
                            空白 = 0
                            
                            For マス番号 = m To マス数((行列 - 1) * -1)
                        
                                If 現状(マス番号) = 1 Then
                                        cnt = cnt + 1
                                
                                Else
                                    '空白が見つかった
                                    If 現状(マス番号) = 0 Then
                                        空白出現Fg = True
                                        空白 = 空白 + 1
                                    End If
                                    
                                    '黒が1つ以上あるなら
                                    If cnt > 0 Then

                    
                                        '数字の分塗りつぶしている場合
                                        If cnt = nd.num Then
                                        
                                            'この塗りつぶしが他の数字の可能性を検証
    
                                            次無Fg = False
                                            
                                            '次の数字があるか?
                                            If i = 1 Then
                                                次無Fg = True
                                            Else
                                                '塗りつぶしの前後が閉鎖されているか?
                                                If 現状(マス番号) = 2 Then
                                                    前後閉鎖Fg = False
                                                    If (マス番号 - 1 - cnt) = 0 Then
                                                        
                                                        前後閉鎖Fg = True
                                                    ElseIf 現状(マス番号 - 1 - cnt) = 2 Then
                                                        
                                                        前後閉鎖Fg = True
                                                    End If
                                                    
                                                    If 前後閉鎖Fg Then
                                                        '以降に同じ数字が存在するか確認
                                                        次無Fg = True
                                                        For k = i - 1 To 1 Step -1
                                                            If nd.num = numsArr(行列)(セット番号)(k).num Then
                                                                次無Fg = False
                                                                Exit For
                                                            End If
                                                            
                                                        Next k
                                                    End If
                                                    
                                                End If
                                            
                                            End If
                                            
                                            
                                            '手前に収納可能か?
                                            If 次無Fg Or Not 手前に入るかな(現状, m, マス番号 - 2 - cnt, cnt) Then
                                                '完了FG
                                                nd.fg = True
                                                
                                                '完了した数字に色を付ける
                                                myOffset(基点, 1 - i, セット番号, 行列).Interior.Color = RGB(240, 255, 230)
                                                
                                                '開始位置の記録
                                                nd.m = マス番号 - cnt
                                                
                                                
                                                '手前に空白が存在しているならすべてオレンジにする
                                                If 空白出現Fg Then
                                                    For k = m To マス番号 - 1 - cnt
'                                                        If k > 0 Then
                                                            If 現状(k) = 0 Then
                                                                mainFg = True
                                                                現状(k) = 2
                                                                残数 = 残数 - 1
                                                                myOffset(基点, k, セット番号, 行列).Interior.Color = RGB(255, 240, 230)
                                                            End If
'                                                        End If
                                                    Next k
                                                End If
                                                
                                                '後をオレンジする
                                                
                                                If 現状(マス番号) = 0 Then
                                                    mainFg = True
                                                    残数 = 残数 - 1
                                                    現状(マス番号) = 2
                                                    myOffset(基点, マス番号, セット番号, 行列).Interior.Color = RGB(255, 240, 230)
                                                End If
                                                Exit For
                                            End If
                                            
                                            '手前に収納可能か?ここまで
                                            
                                        ElseIf 現状(マス番号) = 2 Then

                                            '黒の数と空白の数が数字と一致
                                            If cnt + 空白 = nd.num Then
                                            
                                                '手前に収納可能か?空白の個数も考慮
                                                If Not 手前に入るかな(現状, m, マス番号 - 1 - cnt - 空白, cnt) Then
                                                    '完了FG
                                                    nd.fg = True
                                                    
                                                    '完了した数字に色を付ける
                                                    myOffset(基点, 1 - i, セット番号, 行列).Interior.Color = RGB(240, 255, 230)
                                                    
                                                    '開始位置の記録
                                                    nd.m = マス番号 - cnt - 空白
                                                    
                                                    For k = マス番号 - 1 To マス番号 - cnt - 空白 Step -1
                                                        
                                                        If 現状(k) = 0 Then
                                                            mainFg = True
                                                            現状(k) = 1
                                                            残数 = 残数 - 1
                                                            黒数 = 黒数 + 1
                                                            
                                                            myOffset(基点, k, セット番号, 行列).Interior.Color = RGB(0, 0, 0)
                                                        End If
                                                        
                                                    Next k
                                                    
                                                    
                                                End If
                                                
                                                
                                            End If
                                            
                                            Exit For
                                        End If
            
                                        
                                        cnt = 0
    
                                        
                                    ElseIf 現状(マス番号) = 2 Then
                                        'オレンジなら空白の数をリセット
                                        空白 = 0
                                    End If
                                End If
    
                            Next マス番号
                            
                            '未完了の場合
                            If Not nd.fg Then
                                'とりあえず処理を抜ける
                                Exit For
                            End If
                            
                            'ここが適切か分からない
                            m = マス番号 + 1
                            
                        Else
                            m = nd.m + nd.num + 1
                        End If
                        
    
                    Next

        
                Else
                    myResize(myOffset(基点, 1, セット番号, 行列), マス数((行列 - 1) * -1), 1, 行列).Interior.Color = RGB(255, 240, 230)
                
                End If
                
                
            Next セット番号
        Next 行列
    Loop
End Sub



Function myOffset(base As Range, r As Integer, c As Integer, mode As Integer) As Range
    
    If mode = 1 Then
        Set myOffset = base.Offset(r, c)
    Else
        Set myOffset = base.Offset(c, r)
    End If
    
End Function

Function myResize(base As Range, r As Integer, c As Integer, mode As Integer) As Range
    
    If mode = 1 Then
        Set myResize = base.Resize(r, c)
    Else
        Set myResize = base.Resize(c, r)
    End If
    
End Function


Function 絶対塗れるヤツ(max As Integer, nums As Variant)
    Dim i As Integer
    Dim n As Integer
    Dim d As Integer
    
    Dim cnt As Integer
    
    
    Dim sum As Integer
    
    Dim 配列A() As Integer
    Dim 配列B() As Integer
    Dim 配列C() As Integer
    
    If max > 0 Then
        ReDim 配列A(1 To max)
        ReDim 配列B(1 To max)
        ReDim 配列C(1 To max)
        
        i = 1
        cnt = 0
        
        '配列A
        For d = 1 To nums.Count
            '追加 完了済みはスキップ
            If Not nums(d).fg Then
                cnt = cnt + 1
                sum = sum + nums(d).num
                n = 1
                Do While n <= nums(d).num
                    配列A(i) = d
                    i = i + 1
                    n = n + 1
                Loop
                
                '余った部分を0で埋める(無くてもよい)
                If i < max Then
                    配列A(i) = 0
                    i = i + 1
                End If
            End If
        Next d
        
        '配列B cntに変更
        For i = 1 To max
            If i <= max - (cnt - 1 + sum) Then
                配列B(i) = 0
            Else
                配列B(i) = 配列A(i - (max - (cnt - 1 + sum)))
            End If
        Next i
        
        '配列C
        For i = 1 To max
            If 配列A(i) * 配列B(i) <> 0 And 配列A(i) = 配列B(i) Then
                配列C(i) = 1
            End If
        Next i
    
    End If
    絶対塗れるヤツ = 配列C
End Function


Function 手前に入るかな(arr() As Integer, m1 As Integer, m2 As Integer, size As Integer)
    
    Dim cnt As Integer
    Dim i As Integer
    
    
    For i = m1 To m2

        If cnt = 0 And arr(i) <> 2 Then
            cnt = 1
        ElseIf arr(i) <> 2 Then
            cnt = cnt + 1
            
        ElseIf cnt >= size Then
            Exit For
        Else
            cnt = 0
        End If
    Next i
    
    If cnt = 0 Then
        手前に入るかな = False
    Else
        手前に入るかな = size <= cnt
    End If
End Function




Function dgStop(fg As Boolean)
    If fg Then
        Stop
    End If
End Function




Function 手前に入るかな(arr() As Integer, m1 As Integer, m2 As Integer, size As Integer)
    
    Dim cnt As Integer
    Dim i As Integer
    
    
    For i = m1 To m2

        If cnt = 0 And arr(i) <> 2 Then
            cnt = 1
        ElseIf arr(i) <> 2 Then
            cnt = cnt + 1
            
        ElseIf cnt >= size Then
            Exit For
        Else
            cnt = 0
        End If
    Next i
    
    If cnt = 0 Then
        手前に入るかな = False
    Else
        手前に入るかな = size <= cnt
    End If
End Function




Function dgStop(fg As Boolean)
    If fg Then
        Stop
    End If
End Function

この記事が気に入ったらサポートをしてみませんか?