I wrote this java Class to solve a standard 9x9 sudoku board. It uses backtracking to solve each field of the board.
I am really interested in feedback for the "isValid" and "isBlockValid" Methods, because they are redundant.
Here is my code on github.
Also here is the code:
public class SudokuSolver {
private boolean solve(int[][] board, int counter){
int col = (int) counter / board.length;
int row = counter % board.length;
if (col >= board.length){
printBoard(board);
return true;
}
if (board[row][col] == 0) {
for (int n = 1; n <= board.length; n++) {
if (isValid(n,row,col, board)){
board[row][col] = n;
if (solve(board,counter+1)){
return true;
}
}
board[row][col] = 0;
}
}else{
if (solve(board,counter+1)){
return true;
}
}
return false;
}
public void startSolving(int[][] board){
if(!solve(board, 0)){
System.out.println("no solution");
}
}
private boolean isValid(int n, int row, int col, int[][] board){
int i;
int j;
for (i = 0; i < board.length; i++) {
if(board[row][i] == n){
return false;
}
}
for (i = 0; i < board.length; i++) {
if(board[i][col] == n){
return false;
}
}
//check if block is valid - do not know any other way for solving this
// check block 1
if (row >= 0 && col >= 0 && row <= 2 && col <= 2){
if(!isBlockValid(n, row, col, board, 0, 2, 0, 2)){
return false;
}
}
// check block 2
if (row >= 0 && col >= 3 && row <= 2 && col <= 5){
if(!isBlockValid(n, row, col, board, 0, 2, 3, 5)){
return false;
}
}
// check block 3
if (row >= 0 && col >= 6 && row <= 2 && col <= 8){
if(!isBlockValid(n, row, col, board, 0, 2, 6, 8)){
return false;
}
}
// check block 4
if (row >= 3 && col >= 0 && row <= 5 && col <= 2){
if(!isBlockValid(n, row, col, board, 3, 5, 0, 2)){
return false;
}
}
// check block 5
if (row >= 3 && col >= 3 && row <= 5 && col <= 5){
if(!isBlockValid(n, row, col, board, 3, 5, 3, 5)){
return false;
}
}
// check block 6
if (row >= 3 && col >= 6 && row <= 5 && col <= 8){
if(!isBlockValid(n, row, col, board, 3, 5, 6, 8)){
return false;
}
}
// check block 7
if (row >= 6 && col >= 0 && row <= 8 && col <= 2){
if(!isBlockValid(n, row, col, board, 6, 8, 0, 2)){
return false;
}
}
// check block 8
if (row >= 6 && col >= 3 && row <= 8 && col <= 5){
if(!isBlockValid(n, row, col, board, 6, 8, 3, 5)){
return false;
}
}
// check block 9
if (row >= 6 && col >= 6 && row <= 8 && col <= 8){
if(!isBlockValid(n, row, col, board, 6, 8, 6, 8)){
return false;
}
}
return true;
}
private boolean isBlockValid(int n, int row, int col, int[][] board, int starti, int stopi, int startj, int stopj){
for (int i = starti; i <= stopi; i++) {
for (int j = startj; j <= stopj; j++) {
if (board[i][j] == n) {
return false;
}
}
}
return true;
}
private void printBoard(int[][] board){
System.out.println();
for (int[] row : board){
System.out.print("|");
for (int col : row){
System.out.print(col);
System.out.print("|");
}
System.out.println();
}
System.out.println();
}
public static void main(String[] args) {
int[][] board =
{{2,0,5,0,0,0,0,0,0},
{3,0,8,6,0,0,9,0,0},
{0,0,0,1,0,0,4,0,0},
{0,0,0,0,5,0,0,1,0},
{0,0,0,0,9,0,0,2,0},
{8,7,0,0,2,0,0,0,0},
{0,0,0,0,8,9,0,0,3},
{0,0,6,0,0,3,0,0,5},
{5,0,4,0,0,0,0,0,1}};
SudokuSolver sudokuSolver = new SudokuSolver();
sudokuSolver.startSolving(board);
}
}
```