第18講 一般種法×末項確定法(魔方陣作成マクロVer.4)への挑戦

第3話 番号付けコード例
Dim mah(12, 12) As Byte, x(144) As Byte, y(144) As Byte, p(144) As Byte, a(12, 12) As Integer
Dim n As Byte, cn As Integer, gk As Integer
Private Sub CommandButton1_Click()
  Dim i, j As Integer
  n = Cells(3, 8)
  Rnd (-1)
  syokika
  zhy
  zhy1 (0)
  zhykakunin
  'ms (0)
End Sub

Sub syokika()
  Dim i As Byte
  Dim j As Byte
  For i = 0 To n * n - 1
    p(i) = 0
  Next
  sk = 0
  cn = 0
  gk = n
  For i = 0 To n - 1
    For j = 0 To n - 1
      a(i, j) = -1
    Next
  Next
End Sub

Sub zhy()
  Dim i As Byte
  For i = 0 To n - 1
    a(i, i) = i
  Next
  For i = 0 To n - 1
    If a(i, n - 1 - i) = -1 Then
      a(i, n - 1 - i) = gk
      gk = gk + 1
    End If
  Next
End Sub

Sub zhy1(g As Byte)
  Dim i As Byte
  For i = g + 1 To n - 1
    If a(g, i) = -1 Then
      a(g, i) = gk
      gk = gk + 1
    End If
  Next
  zhy2 (g)
End Sub

Sub zhy2(g As Byte)
  Dim i As Byte
  Dim j As Byte
  For i = g + 1 To n - 1
    If a(i, g) = -1 Then
      a(i, g) = gk
      gk = gk + 1
    End If
  Next
  If g + 1 < n Then
    zhy1 (g + 1)
  Else
    For i = 0 To n - 1
      For j = 0 To n - 1
        y(a(i, j)) = i
        x(a(i, j)) = j
      Next
    Next
    tm = 1
    Exit Sub
  End If
End Sub

Sub zhykakunin()
  Dim i As Byte
  For i = 0 To n * n - 1
    Cells(5 + y(i), 1 + x(i)) = i
  Next
End Sub




第2話へ 第4話へ

004
  

VBA講義第1部へ
vc++講義へ
vb講義へ
VB講義基礎へ
初心者のための世界で一番わかりやすいVisual C++入門基礎講座へ
初心者のための世界で一番わかりやすいVisual Basic入門基礎講座へ

数学研究室に戻る