|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
本帖最后由 yangyangzhifeng 于 2013-10-24 12:55 编辑
- Sub sudoku()
- Dim ar, i&, j&, jg&(1 To 81), x&, t&, tt
- Dim f_g&(1 To 9, 1 To 9), f_h&(1 To 9, 1 To 9), f_l&(1 To 9, 1 To 9)
- Dim g&(1 To 81), h&(1 To 81), l&(1 To 81)
- tt = Timer
- ar = Sheet1.[a1:i9]
- For i = 1 To 9
- For j = 1 To 9
- x = x + 1
- h(x) = i: l(x) = j
- g(x) = ((i - 1) \ 3) * 3 + (j - 1) \ 3 + 1
- jg(x) = ar(i, j)
- If jg(x) > 0 Then
- If f_g(g(x), jg(x)) + f_h(h(x), jg(x)) + f_l(l(x), jg(x)) > 0 Then MsgBox "无解": Exit Sub
- f_g(g(x), jg(x)) = 1
- f_h(h(x), jg(x)) = 1
- f_l(l(x), jg(x)) = 1
- End If
- Next
- Next
- x = 1: t = 1
- Do
- If x = 0 Then MsgBox "无解": Exit Sub
- If x > 81 Then Exit Do
- If ar(h(x), l(x)) = 0 Then
- For i = jg(x) + 1 To 9
- If f_g(g(x), i) + f_h(h(x), i) + f_l(l(x), i) = 0 Then
- If jg(x) > 0 Then
- f_g(g(x), jg(x)) = 0
- f_h(h(x), jg(x)) = 0
- f_l(l(x), jg(x)) = 0
- End If
- jg(x) = i: t = 1
- f_g(g(x), jg(x)) = 1
- f_h(h(x), jg(x)) = 1
- f_l(l(x), jg(x)) = 1
- Exit For
- End If
- Next
- If i > 9 Then
- t = -1
- If jg(x) > 0 Then
- f_g(g(x), jg(x)) = 0
- f_h(h(x), jg(x)) = 0
- f_l(l(x), jg(x)) = 0
- jg(x) = 0
- End If
- End If
- End If
- x = x + t
- Loop
- For i = 1 To 81
- ar(h(i), l(i)) = jg(i)
- Next
- Sheet2.[a1:i9] = ar
- MsgBox Format(Timer - tt, "0.000s "): Sheet2.Activate
- Sheet2.Cells.Font.Bold = False
- Sheet2.Range(Sheet1.Range("a1:i9").SpecialCells(xlCellTypeConstants, 1).Address(0, 0)).Font.Bold = True
- End Sub
复制代码
shudu.rar
(13.62 KB, 下载次数: 155)
改良版- Sub sudoku2()
- Dim br, ar&(1 To 9, 1 To 9), i&, j&, jg&(1 To 81), x&, t&, tt, jwz&(1 To 81), y&, k&
- Dim f_g&(1 To 9, 1 To 9), f_h&(1 To 9, 1 To 9), f_l&(1 To 9, 1 To 9)
- Dim g&(1 To 81), h&(1 To 81), l&(1 To 81)
- tt = Timer
- br = Sheet1.[b2].Resize(9, 9)
- For i = 1 To 9
- For j = 1 To 9
- ar(i, j) = br(i, j)
- Next
- Next
- For i = 1 To 9
- For j = 1 To 9
- x = x + 1
- h(x) = i: l(x) = j
- g(x) = ((i - 1) \ 3) * 3 + (j - 1) \ 3 + 1
- jg(x) = ar(i, j)
- If jg(x) > 0 Then
- If f_g(g(x), jg(x)) + f_h(h(x), jg(x)) + f_l(l(x), jg(x)) > 0 Then MsgBox "无解": Exit Sub
- f_g(g(x), jg(x)) = 1
- f_h(h(x), jg(x)) = 1
- f_l(l(x), jg(x)) = 1
- Else
- y = y + 1
- jwz(y) = x
- End If
- Next
- Next
- j = 0: t = 1
- Do
- j = j + t
- If j > y Then Exit Do
- If t = 1 Then
- jwz(j) = get_wz(ar)
- End If
- x = jwz(j)
- If x <> 0 Then
- For i = jg(x) + 1 To 9
- If f_g(g(x), i) = 0 Then
- If f_h(h(x), i) = 0 Then
- If f_l(l(x), i) = 0 Then
- jg(x) = i: t = 1: ar(h(x), l(x)) = i
- f_g(g(x), jg(x)) = 1
- f_h(h(x), jg(x)) = 1
- f_l(l(x), jg(x)) = 1
- Exit For
- End If
- End If
- End If
- Next
- If i > 9 Then
- t = -1
- jg(x) = 0: ar(h(x), l(x)) = 0
- If j > 1 Then
- x = jwz(j - 1)
- f_g(g(x), jg(x)) = 0
- f_h(h(x), jg(x)) = 0
- f_l(l(x), jg(x)) = 0
- Else
- MsgBox "无解": Exit Sub
- End If
- End If
- Else
- t = -1
- End If
-
- Loop
- For i = 1 To 81
- ar(h(i), l(i)) = jg(i)
- Next
- Sheet1.[b12].Resize(9, 9) = ar
- MsgBox Format(Timer - tt, "0.000s ")
- Sheet1.[b12].Resize(9, 9).Interior.ColorIndex = -4142
- Sheet1.[b2].Resize(9, 9).SpecialCells(xlCellTypeConstants, 1).Offset(10).Interior.Color = vbRed
- End Sub
- Function get_wz(ar&()) As Long
- Dim i&, j&, k, jg&(1 To 9), x&, r&, c&
- x = -1
- For i = 1 To 9
- For j = 1 To 9
- If ar(i, j) = 0 Then
- For k = 1 To 9
- If ar(i, k) > 0 Then jg(ar(i, k)) = 1
- If ar(k, j) > 0 Then jg(ar(k, j)) = 1
- Next
- For r = 1 + ((i - 1) \ 3) * 3 To 3 * ((i + 2) \ 3)
- For c = 1 + ((j - 1) \ 3) * 3 To 3 * ((j + 2) \ 3)
- If ar(r, c) > 0 Then jg(ar(r, c)) = 1
- Next
- Next
- r = 0
- For k = 1 To 9
- r = r + jg(k)
- Next
- Erase jg
- If r > x Then
- x = r
- get_wz = (i - 1) * 9 + j
- If x > 7 Then Exit Function
- End If
- End If
- Next
- Next
- End Function
复制代码
|
评分
-
2
查看全部评分
-
|