数独(ナンプレ)を解くアルゴリズムの要点とパフォーマンスの検証№3
数独(ナンプレ)を解くアルゴリズムを例に、アルゴリズムの要点と、それによるパフォーマンスを検証します、
Option Explicit
Private tryCnt As Long
Sub main()
Debug.Print Timer
Dim SuAry(1 To 9, 1 To 9) As Integer
Dim i1 As Integer
Dim i2 As Integer
tryCnt = 0
Erase SuAry
For i1 = 1 To 9
For i2 = 1 To 9
If Cells(i1, i2) = "" Then
Cells(i1, i2).Font.Color = vbBlue
Else
SuAry(i1, i2) = Cells(i1, i2)
End If
Next
Next
Call trySu(SuAry)
Range("A1:I9").Value = SuAry
Debug.Print Timer
If getBlank(SuAry(), i1, i2) = False Then
MsgBox "解読成功" & vbLf & tryCnt
Else
MsgBox "あれれ・・・"
End If
End Sub
Function trySu(ByRef SuAry() As Integer) As Boolean
Dim i1 As Integer
Dim i2 As Integer
Dim su As Integer
Dim tryAry() As Integer
If getBlank(SuAry(), i1, i2) = False Then
trySu = True
Exit Function
End If
If chkSu(SuAry(), i1, i2, tryAry()) <> 0 Then
For su = 1 To 9
If tryAry(su) <> 0 Then
SuAry(i1, i2) = su
tryCnt = tryCnt + 1
Cells(i1, i2) = su
If trySu(SuAry) = True Then
trySu = True
Exit Function
End If
End If
Next
End If
SuAry(i1, i2) = 0
Cells(i1, i2) = ""
DoEvents
trySu = False
End Function
Function getBlank(ByRef SuAry() As Integer, ByRef i1 As Integer, ByRef i2 As Integer) As Boolean
Dim cnt As Integer
Dim tryMin As Integer
Dim i1Min As Integer
Dim i2Min As Integer
Dim tryAry() As Integer
tryMin = 10
For i1 = 1 To 9
For i2 = 1 To 9
If SuAry(i1, i2) = 0 Then
cnt = chkSu(SuAry, i1, i2, tryAry)
If tryMin > cnt Then
i1Min = i1
i2Min = i2
tryMin = cnt
End If
End If
Next
Next
If tryMin = 10 Then
getBlank = False
Else
i1 = i1Min
i2 = i2Min
getBlank = True
End If
End Function
Function chkSu(ByRef SuAry() As Integer, ByVal i1 As Integer, ByVal i2
As Integer, ByRef tryAry() As Integer) As Integer
Dim ix1 As Integer
Dim ix2 As Integer
Dim i1S As Integer
Dim i2S As Integer
chkSu = False
ReDim tryAry(1 To 9)
For ix1 = 1 To 9
tryAry(ix1) = ix1
Next
'横をチェック
For ix2 = 1 To 9
If ix2 <> i2 Then
If SuAry(i1, ix2) <> 0 Then
tryAry(SuAry(i1, ix2)) = 0
End If
End If
Next
'縦をチェック
For ix1 = 1 To 9
If ix1 <> i1 Then
If SuAry(ix1, i2) <> 0 Then
tryAry(SuAry(ix1, i2)) = 0
End If
End If
Next
'枠内をチェック
i1S = (Int((i1 + 2) / 3) - 1) * 3 + 1
i2S = (Int((i2 + 2) / 3) - 1) * 3 + 1
For ix1 = i1S To i1S + 2
For ix2 = i2S To i2S + 2
If ix1 <> i1 Or ix2 <> i2 Then
If SuAry(ix1, ix2) <> 0 Then
tryAry(SuAry(ix1, ix2)) = 0
End If
End If
Next
Next
chkSu = 0
For ix1 = 1 To 9
If tryAry(ix1) <> 0 Then
chkSu = chkSu + 1
End If
Next
End Function
大幅に変更しています。
№3のアルゴリズムは、90~800の試行回数で、0.05秒~0.2秒
ただし、必ずしも比例して改善されるわけではありません。
見直す機会があって、本の少し改善しました。
数独を解くアルゴリズムの要点とパフォーマンスの検証 №1 №2 №3 №4
同じテーマ「マクロVBAサンプル集」の記事
アメブロの記事本文をVBAでバックアップする№1
数独(ナンプレ)を解くVBAに挑戦№1
数独(ナンプレ)を解くアルゴリズムの要点とパフォーマンスの検証№1
ナンバーリンク(パズル)を解くVBAに挑戦№1
ナンバーリンクを解くVBAのパフォーマンス改善№1
オセロを作りながらマクロVBAを学ぼう
他ブックへのリンクエラーを探し解除
Excelシートの複雑な計算式を解析するVBA
Excel将棋:マクロVBAの学習用(№1)
Excel囲碁:万波奈穂先生に捧ぐ
Excel囲碁:再起動後も続けて打てるように改造
新着記事NEW ・・・新着記事一覧を見る
抜けている数値を探せ|エクセル雑感(2022-07-01)
.Net FrameworkのSystem.Collectionsを利用|VBA技術解説(2022-06-29)
迷路ネコが影分身の術を体得したら…|エクセル雑感(2022-06-27)
迷路にネコが挑戦したら、どうなるかな…|エクセル雑感(2022-06-26)
サロゲートペアに対応した自作関数(Len,Left,Mid,Right)|エクセル雑感(2022-06-24)
「マクロの登録」で登録できないプロシージャーは?|エクセル雑感(2022-06-23)
オブジェクトのByRef、ByVal、Variant|エクセル雑感(2022-06-22)
コメントから特定形式の年月を取り出す|エクセル雑感(2022-06-19)
4,9を使わない連番作成|エクセル雑感(2022-06-17)
連番を折り返して出力|エクセル雑感(2022-06-16)
アクセスランキング ・・・ ランキング一覧を見る
1.最終行の取得(End,Rows.Count)|VBA入門
2.RangeとCellsの使い方|VBA入門
3.変数宣言のDimとデータ型|VBA入門
4.繰り返し処理(For Next)|VBA入門
5.セルのコピー&値の貼り付け(PasteSpecial)|VBA入門
6.Excelショートカットキー一覧|Excelリファレンス
7.マクロって何?VBAって何?|VBA入門
8.並べ替え(Sort)|VBA入門
9.Range以外の指定方法(Cells,Rows,Columns)|VBA入門
10.エクセルVBAでのシート指定方法|VBA技術解説
- ホーム
- マクロVBA応用編
- マクロVBAサンプル集
- 数独(ナンプレ)を解くアルゴリズムの要点とパフォーマンスの検証№3
このサイトがお役に立ちましたら「シェア」「Bookmark」をお願いいたします。
記述には細心の注意をしたつもりですが、
間違いやご指摘がありましたら、「お問い合わせ」からお知らせいただけると幸いです。
掲載のVBAコードは動作を保証するものではなく、あくまでVBA学習のサンプルとして掲載しています。
掲載のVBAコードは自己責任でご使用ください。万一データ破損等の損害が発生しても責任は負いません。