-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSudokuSolver.java
More file actions
61 lines (50 loc) · 1.33 KB
/
SudokuSolver.java
File metadata and controls
61 lines (50 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
public class SudokuSolver {
static boolean solver(int board[][], int row, int col) {
// Move to the next row
if(col == board[0].length) {
col = 0;
row += 1;
}
// check if board if filled
if(row == board.length) {
return true;
}
// check if there is zero or any other number
if(board[row][col] != 0) {
return solver(board, row, col+1);
}
for(int i = 1; i <= 9; i++) {
if(isSafe(row, col, i, board)) {
board[row][col] = i;
}
}
}
static boolean isPresentInRow(int row, int value, int [][]board) {
return false;
}
static boolean isPresentInCol(int col, int value, int [][]board) {
return false;
}
static boolean isPresentInGrid(int row, int col, int value, int[][] board) {
return false;
}
static boolean isSafe(int row, int col, int value, int[][] board) {
return !isPresentInRow(row, value, board) &&
!isPresentInCol(col, value, board) &&
!isPresentInGrid(row, col, value, board);
}
public static void main(String[] args) {
int board[][] = {
{5,3,0,0,7,0,0,0,0},
{6,0,0,1,9,5,0,0,0},
{0,9,8,0,0,0,0,6,0},
{8,0,0,0,6,0,0,0,3},
{4,0,0,8,0,3,0,0,1},
{7,0,0,0,2,0,0,0,6},
{0,6,0,0,0,0,2,8,0},
{0,0,0,4,1,9,0,0,5},
{0,0,0,0,8,0,0,7,9}
};
solver(board,0,0);
}
}