第19講 対角線法による魔方陣自動生成ソフトの高速化
第7話 対角線法に乱数を組み込む
ランダム版

を実現するプログラム例
Dim a(10, 10) As Byte, n As Byte, cn As Long, y(100) As Byte, x(100) As Byte
Private Sub CommandButton1_Click()

  CommandButton2_Click
  hj = Timer
  cn = 0
  n = Cells(4, 2)
  Rnd (-1)
  If n = 4 Then
    Randomize (90)
  End If
  If n = 5 Then
    Randomize (2)
  End If
  Call zy
  Call f(0) 'n次魔方陣作成プロシージャ
  ow = Timer
  Cells(4, 16) = "生成にかかった時間は"
  Cells(4, 20) = ow - hj
  Cells(4, 21) = "秒です。 "

End Sub

Sub zy()

  Dim i As Byte, j As Byte, k As Byte
  Dim b(10, 10) As Byte
  
  For i = 0 To n - 1
    For j = 0 To n - 1
      b(i, j) = n * n + 1
    Next
  Next
  For i = 0 To n - 1
    b(i, i) = i
  Next
  k = n
  For i = 0 To n - 1
    If b(i, n - 1 - i) = n * n + 1 Then
      b(i, n - 1 - i) = k
      k = k + 1
    End If
  Next
  For i = 0 To n - 1
    For j = 0 To n - 1
      If b(i, j) = n * n + 1 Then
        b(i, j) = k
      k = k + 1
    End If
    Next
  Next
  
  For i = 0 To n - 1
    For j = 0 To n - 1
      y(b(i, j)) = i
      x(b(i, j)) = j
    Next
  Next
  
End Sub

Sub f(g As Byte)

  Dim i As Byte, j As Byte, w As Byte, ii As Byte, iii As Byte, k As Byte
  ii = Int(n * n * Rnd)
  For iii = 1 To n * n
    i = ((iii + ii) Mod n * n) + 1
    If g > 0 Then
      For j = 0 To g - 1
        If i = a(y(j), x(j)) Then GoTo tobi
      Next
    End If
    a(y(g), x(g)) = i
    If x(g) = n - 2 And y(g) = 0 Then
      w = 0
      For j = 0 To n - 2
        w = w + a(y(g), j)
      Next
      w = w + a(y(g), n - 1)
      If w <> Int(n * (n * n + 1) / 2) Then GoTo tobi
    End If
    If y(g) = n - 2 And x(g) = 0 Then
      w = 0
      For j = 0 To n - 2
        w = w + a(j, x(g))
      Next
      w = w + a(n - 1, x(g))
      If w <> Int(n * (n * n + 1) / 2) Then GoTo tobi
    End If
    If x(g) = n - 1 And y(g) > 0 And y(g) < n - 1 Then
      w = 0
      For j = 0 To n - 1
        w = w + a(y(g), j)
      Next
      If w <> Int(n * (n * n + 1) / 2) Then GoTo tobi
    End If
    If y(g) = n - 1 And x(g) > 0 And x(g) < n - 1 Then
      w = 0
      For j = 0 To n - 1
        w = w + a(j, x(g))
      Next
      If w <> Int(n * (n * n + 1) / 2) Then GoTo tobi
    End If
    If x(g) = n - 2 And y(g) = n - 1 Then
      w = 0
      For j = 0 To n - 2
        w = w + a(y(g), j)
      Next
      w = w + a(y(g), n - 1)
      If w <> Int(n * (n * n + 1) / 2) Then GoTo tobi
    End If
    If y(g) = n - 2 And x(g) = n - 1 Then
      w = 0
      For j = 0 To n - 2
        w = w + a(j, x(g))
      Next
      w = w + a(n - 1, x(g))
      If w <> Int(n * (n * n + 1) / 2) Then GoTo tobi
    End If
    If x(g) = 0 And y(g) = n - 1 Then
      w = 0
      For j = 0 To n - 1
        w = w + a(j, n - 1 - j)
      Next
      If w <> Int(n * (n * n + 1) / 2) Then GoTo tobi
    End If
    If x(g) = n - 1 And y(g) = n - 1 Then
      w = 0
      For j = 0 To n - 1
        w = w + a(j, j)
      Next
      If w <> Int(n * (n * n + 1) / 2) Then GoTo tobi
    End If

    If g + 1 < n * n Then
      Call f(g + 1)
      If n = 4 And cn = 100 Then Exit Sub
      If n = 5 And cn = 50 Then Exit Sub
    Else
      Call h
      If n = 4 And cn = 100 Then Exit Sub
      If n = 5 And cn = 50 Then Exit Sub
    End If
tobi:
  Next
  
End Sub

Sub h()

  Dim i As Byte, j As Byte, s As Byte, am As Byte, w As Byte
    
  For i = 0 To n * n - 1
    s = Int(i / n)
    am = i Mod n
    Cells(6 + s + (n + 1) * Int(cn / 5), 2 + am + (n + 1) * (cn Mod 5)) = a(s, am)
  Next
  cn = cn + 1
  If cn = 1 Then Cells(4, 8) = "魔方陣が"
  Cells(4, 10) = cn
  If cn = 1 Then Cells(4, 11) = "個生成されました。 "
  
End Sub

Private Sub CommandButton2_Click()
  
  Rows("5:20000").Select
  Selection.ClearContents
  Range("H4:U4").Select
  Selection.ClearContents
  Cells(1, 1).Select
  
End Sub
参考ダウンロード添付ファイル

  Rnd (-1)
  If n = 4 Then
    Randomize (90)
  End If

は最適シード値探索プログラムより見つけました。
5次の方も粘り強く実験すれば、
おそらく、
wewsx
20秒を切るシード値を発見できると思いますが、
現時点では見つかっていません。
次話では、
最適シード値探索プログラムを紹介します。

第6話へ 第8話へ
004

eclipse c++ 入門
魔方陣 数独で学ぶ VBA 入門
数独のシンプルな解き方・簡単な解法の研究
vc++講義へ
excel 2013 2010 2007 vba入門へ
VB講義基礎へ
初心者のための世界で一番わかりやすいVisual C++入門基礎講座へ
初心者のための世界で一番わかりやすいVisual Basic入門基礎講座へ
専門用語なしの C言語 C++ 入門(Visual C++ 2010で学ぶ C言語 C++ 入門)
専門用語なしの excel vba マクロ 入門 2013 2010 2007 対応講義 第1部
eclipse java 入門へ
excel 2016 vba 入門へ
小学生からエンジニアまでのRuby入門へ
本サイトトップへ