This popular Japanese number puzzle has just one easy rule:
In every Row, every Column and every 3x3 sub-grid, all the numbers from 1 to 9 should appear, but only once in each row, column and sub-grid.
+------+-------+------+
| 0 0 0 | 7 0 0 | 4 0 0 |
| 0 3 0 | 0 9 0 | 0 2 0 |
| 4 0 0 | 0 0 5 | 0 0 0 |
+------+-------+------+
| 0 0 8 | 0 0 0 | 0 0 5 |
| 0 9 0 | 0 3 0 | 0 7 0 |
| 6 0 0 | 0 0 0 | 3 0 0 |
+------+-------+------+
| 0 0 0 | 4 0 0 | 0 0 6 |
| 0 7 0 | 0 2 0 | 0 9 0 |
| 0 0 5 | 0 0 8 | 0 0 0 |
+------+-------+------+
Replace the 0's with the digits required to satisfy the rule.
(In reply to
re(4): Haley's Comet vrs. Penny's Loafer: And the winner is.... by Charlie)
My same program (basically), run under the VB 5.0 IDE takes 2 min, 14 sec to find the 82 solutions of the modified problem, but when compiled, the .EXE takes 1 min, 10 sec. on a 2 GHz processor.
Dim b(9, 9)
Dim solCt
Dim l$(9)
Private Sub cmdStart_Click()
Open "missing.txt" For Output As #2
Print Timer
For i = 1 To 9
l$(i) = Choose(i, "000000400", "030090020", "400005000", "008000005", "090030070", "600000300", "000400006", "070020090", "005008000")
Next i
For i = 1 To 9
For j = 1 To 9
b(i, j) = Val(Mid$(l$(i), j, 1))
Next
Next
find 1, 1
CurrentX = 2.5: CurrentY = 0.5
Print Timer, solCt
Close 2
End Sub
Sub find(v, h)
DoEvents
If b(v, h) = 0 Then
x = 0: y = x
For trial = 1 To 9
good = 1
For i = 1 To 9
If b(v, i) = trial Or b(i, h) = trial Then good = 0
Next i
If good Then
v1 = Int((v - 1) / 3) * 3 + 1
h1 = Int((h - 1) / 3) * 3 + 1
For i = v1 To v1 + 2
For j = h1 To h1 + 2
If b(i, j) = trial Then good = 0
Next
Next
If good Then
b(v, h) = trial
GoSub incrCoords
b(v, h) = 0
End If
End If
Next trial
Else
GoSub incrCoords
End If
Exit Sub
incrCoords:
hnew = h + 1
If hnew > 9 Then
vnew = v + 1: hnew = 1
Else
vnew = v
End If
If vnew > 9 Then
For i = 1 To 9
For j = 1 To 9
Print #2, b(i, j);
Next
Print #2,
Next
solCt = solCt + 1
Print Timer, solCt
Else
find vnew, hnew
End If
Return
End Sub
|
Posted by Charlie
on 2005-05-16 14:50:46 |