-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSudoku.java
80 lines (64 loc) · 2.14 KB
/
Sudoku.java
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
62
63
64
65
66
67
68
69
70
71
72
73
74
import java.util.ArrayList;
public class Sudoku {
// Given a partially filled two-dimensional array, fill all the unfilled cells such
// that each row, each column and each 3 x 3 subgrid (as highlighted below by bolder lines) has every digit from 1 to 9 exactly once.
// Unfilled cells have a value of 0 on the given board.
public static ArrayList<ArrayList<Integer>> solve_sudoku_puzzle(ArrayList<ArrayList<Integer>> board) {
helper(board);
return board;
}
private static boolean helper(ArrayList<ArrayList<Integer>> board) {
int row = 0;
int col = 0;
boolean unfilledCell = false;
for (int i = 0; i < board.size(); i++) {
for (int j = 0; j < board.get(0).size(); j++) {
if (board.get(i).get(j) == 0) {
row = i;
col = j;
unfilledCell = true;
break;
}
if (unfilledCell) {
break;
}
}
}
if (!unfilledCell) {
return true;
}
for (int i = 1; i <= 9; i++) {
if (isSafe(board, row, col, i)) {
board.get(row).set(col, i);
if (helper(board)) {
return true;
} else {
board.get(row).set(col, 0);
}
}
}
return false;
}
static boolean isSafe(ArrayList<ArrayList<Integer>> board, int row, int col, int val) {
for (int i = 0; i < 9; i++) {
if (board.get(row).get(i) == val) {
return false;
}
}
for (int i = 0; i < 9; i++) {
if (board.get(i).get(col) == val) {
return false;
}
}
int rowStart = row - row % 3;
int colStart = col - col % 3;
for (int i = rowStart; i < rowStart + 3; i++) {
for (int j = colStart; j < colStart + 3; j++) {
if (board.get(i).get(j) == val) {
return false;
}
}
}
return true;
}
}