Compare commits
No commits in common. "56ab2ca98fe78a0c16f935853707590af0243768" and "24463fe9200d8f7729171577bd280c21208fdd96" have entirely different histories.
56ab2ca98f
...
24463fe920
1 changed files with 7 additions and 21 deletions
|
@ -7,16 +7,10 @@ public class Solver implements SudokuSolver {
|
||||||
board = new int[9][9];
|
board = new int[9][9];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritDoc}
|
|
||||||
*/
|
|
||||||
public void setBoard(int[][] board) {
|
public void setBoard(int[][] board) {
|
||||||
this.board = board;
|
this.board = board;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritDoc}
|
|
||||||
*/
|
|
||||||
public int[][] getBoard() {
|
public int[][] getBoard() {
|
||||||
return board;
|
return board;
|
||||||
}
|
}
|
||||||
|
@ -28,9 +22,6 @@ public class Solver implements SudokuSolver {
|
||||||
board = new int[9][9];
|
board = new int[9][9];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritDoc}
|
|
||||||
*/
|
|
||||||
public boolean solve() {
|
public boolean solve() {
|
||||||
return solve(0, 0);
|
return solve(0, 0);
|
||||||
}
|
}
|
||||||
|
@ -47,7 +38,6 @@ public class Solver implements SudokuSolver {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// If row is 9, drop down to the next column, if column is 9, we are done
|
|
||||||
if (row == 9) {
|
if (row == 9) {
|
||||||
row = 0;
|
row = 0;
|
||||||
if (++col == 9) {
|
if (++col == 9) {
|
||||||
|
@ -55,17 +45,13 @@ public class Solver implements SudokuSolver {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// If we have a "number" in the current cell
|
|
||||||
// recursively call solve() on the next cell
|
|
||||||
if (board[row][col] != 0) {
|
if (board[row][col] != 0) {
|
||||||
return solve(row + 1, col);
|
return solve(row + 1, col);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check for legal values in the current cell
|
|
||||||
for (int val = 1; val <= 9; ++val) {
|
for (int val = 1; val <= 9; ++val) {
|
||||||
if (legal(row, col, val)) {
|
if (legal(row, col, val)) {
|
||||||
board[row][col] = val;
|
board[row][col] = val;
|
||||||
// When we find a legal value, recursively call solve() on the next cell
|
|
||||||
if (solve(row + 1, col)) {
|
if (solve(row + 1, col)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -105,7 +91,12 @@ public class Solver implements SudokuSolver {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritDoc}
|
* Checks if val is legal in the given row, column, and 3x3 box
|
||||||
|
*
|
||||||
|
* @param row row to check
|
||||||
|
* @param col column to check
|
||||||
|
* @param val value to check
|
||||||
|
* @return true if val is legal
|
||||||
*/
|
*/
|
||||||
public boolean legal(int row, int col, int val) {
|
public boolean legal(int row, int col, int val) {
|
||||||
if (row < 0 || row >= 9 || col < 0 || col >= 9 || val < 1 || val > 9) {
|
if (row < 0 || row >= 9 || col < 0 || col >= 9 || val < 1 || val > 9) {
|
||||||
|
@ -142,11 +133,6 @@ public class Solver implements SudokuSolver {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Checks if the board is solved
|
|
||||||
*
|
|
||||||
* @return true if solved
|
|
||||||
*/
|
|
||||||
public boolean isSolved() {
|
public boolean isSolved() {
|
||||||
return isSolved(0, 0);
|
return isSolved(0, 0);
|
||||||
}
|
}
|
||||||
|
@ -156,7 +142,7 @@ public class Solver implements SudokuSolver {
|
||||||
*
|
*
|
||||||
* @param row
|
* @param row
|
||||||
* @param col
|
* @param col
|
||||||
* @return true if solved
|
* @return
|
||||||
*/
|
*/
|
||||||
private boolean isSolved(int row, int col) {
|
private boolean isSolved(int row, int col) {
|
||||||
// If we are at the 9th row and 0th column (the last cell), we are done
|
// If we are at the 9th row and 0th column (the last cell), we are done
|
||||||
|
|
Loading…
Add table
Reference in a new issue