public static void main(String[] args) {
int[][] matrix = parseProblem(args);
writeMatrix(matrix);
if (solve(0,0,matrix)) // solves in place
writeMatrix(matrix);
else
System.out.println("NONE");
}
static boolean solve(int i, int j, int[][] cells) {
if (i == 9) {
i = 0;
if (++j == 9)
return true;
}
if (cells[i][j] != 0) // skip filled cells
return solve(i+1,j,cells);
for (int val = 1; val <= 9; ++val) {
if (legal(i,j,val,cells)) {
cells[i][j] = val;
if (solve(i+1,j,cells))
return true;
}
}
cells[i][j] = 0; // reset on backtrack
return false;
}
static boolean legal(int i, int j, int val, int[][] cells) {
for (int k = 0; k < 9; ++k) // row
if (val == cells[k][j])
return false;
for (int k = 0; k < 9; ++k) // col
if (val == cells[i][k])
return false;
int boxRowOffset = (i / 3)*3;
int boxColOffset = (j / 3)*3;
for (int k = 0; k < 3; ++k) // box
for (int m = 0; m < 3; ++m)
if (val == cells[boxRowOffset+k][boxColOffset+m])
return false;
return true; // no violations, so it's legal
}
static int[][] parseProblem(String[] args) {
int[][] problem = new int[9][9]; // default 0 vals
for (int n = 0; n < args.length; ++n) {
int i = Integer.parseInt(args[n].substring(0,1));
int j = Integer.parseInt(args[n].substring(1,2));
int val = Integer.parseInt(args[n].substring(2,3));
problem[i][j] = val;
}
return problem;
}
static void writeMatrix(int[][] solution) {
for (int i = 0; i < 9; ++i) {
if (i % 3 == 0)
System.out.println(" -----------------------");
for (int j = 0; j < 9; ++j) {
if (j % 3 == 0) System.out.print("| ");
System.out.print(solution[i][j] == 0
? " "
: Integer.toString(solution[i][j]));
System.out.print(' ');
}
System.out.println("|");
}
System.out.println(" -----------------------");
}
}
Tag : .NET, Ms SQL Server 2005, VS 2005 (.NET 2.x)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For i As Integer = 0 To 8
For j As Integer = 0 To 8
txtTemp(i, j) = New TextBox()
Me.Controls.Add(txtTemp(i, j))
txtTemp(i, j).Width = 20
txtTemp(i, j).Location = New Point(j * 30, i * 30)
Next
Next
End Sub
Public Class Form1
Dim txtTemp(9, 9) As TextBox
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For i As Integer = 0 To 8
For j As Integer = 0 To 8
txtTemp(i, j) = New TextBox()
Me.Controls.Add(txtTemp(i, j))
txtTemp(i, j).Width = 20
txtTemp(i, j).Location = New Point(j * 30, i * 30)
Next
Next
End Sub
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btAnswer.Click, btAnswer.Click
End Sub
Private Function solve(ByVal i As Integer, ByVal j As Integer, ByVal cells(,) As Integer) As Boolean
If i = 9 Then
i = 0
If (++j = 9) Then
Return True
End If
End If
If cells(i, j) <> 0 Then
Return solve(i + 1, j, cells)
End If
For Val As Integer = 1 To Val <= 9
If legal(i, j, Val, cells) Then
cells(i, j) = Val
If (solve(i + 1, j, cells)) Then
Return True
End If
End If
Next
End Function
Private Function legal(ByVal i As Integer, ByVal j As Integer, ByVal Val As Integer, ByVal cells(,) As Integer) As Boolean
Dim k, m As Integer
For k = 0 To 8
If Val = cells(k, j) Then
Return False
End If
Next
For k = 0 To 8
If Val = cells(i, k) Then
Return False
End If
Next
Dim boxRowOffset As Integer = (i / 3) * 3
Dim boxColOffset As Integer = (j / 3) * 3
For k = 0 To 2
For m = 0 To 3
If Val = cells(boxRowOffset + k, boxColOffset + m) Then
Return False
End If
Next
Next
Return True '// no violations, so it's legal
End Function
End Class