-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSudokuSolver.java
247 lines (212 loc) · 6.87 KB
/
SudokuSolver.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*https://practice.geeksforgeeks.org/problems/solve-the-sudoku-1587115621/1*/
/*https://leetcode.com/problems/sudoku-solver/*/
class Solution
{
static boolean isSafe(int[][] b, int row, int col, int key)
{
//the row should not contain duplicates
for (int i = 0; i < 9; ++i)
if (b[row][i] == key)
return false;
//the column should not contain duplicates
for (int i = 0; i < 9; ++i)
if (b[i][col] == key)
return false;
//the 3x3 box should not contain duplicates
int sqrt = (int)Math.sqrt(b.length);
int boxRowStart = row - row % sqrt;
int boxColStart = col - col % sqrt;
for (int r = boxRowStart; r < boxRowStart + sqrt; r++)
for (int d = boxColStart; d < boxColStart + sqrt; d++)
if (b[r][d] == key)
return false;
return true;
}
static boolean SolveSudoku(int grid[][])
{
int row = -1;
int col = -1;
//check if the sudoku is already blank and mark it
boolean isEmpty = true;
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
if (grid[i][j] == 0)
{
row = i;
col = j;
isEmpty = false;
break;
}
}
if (!isEmpty)
break;
}
//if blank then there always exists a solution
if (isEmpty)
return true;
for (int num = 1; num <= 9; num++) //for each number from 1 to 9
{
if (isSafe(grid, row, col, num)) //if putting any of them is safe at current cell
{
grid[row][col] = num; //backtracking step - 1
if (SolveSudoku(grid)) //backtracking step - 2
return true;
grid[row][col] = 0; //backtracking step - 3
}
}
return false;
}
//Function to print grids of the Sudoku.
static void printGrid (int grid[][])
{
// add your code here
for (int i = 0; i < 9; ++i)
for (int j = 0; j < 9; ++j)
System.out.print(grid[i][j]+" ");
}
}
class Solution {
char[][] currBoard;
public static final int N = 9;
public static final int boxSize = 3;
public void solveSudoku(char[][] board) {
currBoard = null;
fill(board, 0, 0);
board = currBoard;
}
public void fill(char[][] board, int r, int c)
{
if (r == 9 && c == 0)
{
if (currBoard == null && isFeasible(board))
{
currBoard = new char[N][N];
for (int i = 0; i < N; ++i)
for (int j = 0; j < N; ++j)
currBoard[i][j] = board[i][j];
}
return;
}
if (currBoard != null) return;
int nextR = r, nextC = c;
++nextC; // move to the next cell on right
if (nextC >= 9) // if no such cell exists
{
nextC = 0; // move to the first cell
++nextR; // of the next row
}
if (board[r][c] == '.')
{
for (int i = 1; i <= 9; ++i)
{
board[r][c] = (char)('0'+i);
if (isFeasible(board)) fill(board, nextR, nextC);
if (currBoard != null) return;
board[r][c] = '.';
}
}
else
fill(board, nextR, nextC);
}
public boolean isFeasible(char[][] board)
{
int[] hash = new int[10];
// check for each row
for (int r = 0; r < 9; ++r)
{
for (int c = 0; c < 9; ++c)
if (board[r][c] != '.')
++hash[board[r][c]-'0'];
for (int i = 1; i <= 9; ++i)
if (hash[i] > 1)
return false;
hash = new int[10];
}
// check for each column
for (int c = 0; c < 9; ++c)
{
for (int r = 0; r < 9; ++r)
if (board[r][c] != '.')
++hash[board[r][c]-'0'];
for (int i = 1; i <= 9; ++i)
if (hash[i] > 1)
return false;
hash = new int[10];
}
// check for each box
for (int rGap = 0; rGap < N/boxSize; ++rGap)
{
for (int cGap = 0; cGap < N/boxSize; ++cGap)
{
int R = rGap*boxSize;
int C = cGap*boxSize;
for (int r = R; r < R+boxSize; ++r)
for (int c = C; c < C+boxSize; ++c)
if (board[r][c] != '.')
++hash[board[r][c]-'0'];
for (int i = 1; i <= 9; ++i)
if (hash[i] > 1)
return false;
hash = new int[10];
}
}
return true;
}
}
//Graph approach
class Solution {
int n;
public void solveSudoku(char[][] board) {
n = board.length;
boolean[][] row = new boolean[n][n + 1];
boolean[][] col = new boolean[n][n + 1];
boolean[][] block = new boolean[n][n + 1];
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
if(board[i][j] == '.') {
continue;
}
int val = Character.getNumericValue(board[i][j]);
row[i][val] = true;
col[j][val] = true;
block[3 * (i/3) + j/3][val] = true;
}
}
dfs(0, 0, row, col, block, board);
}
public boolean dfs(int i,
int j,
boolean[][] row,
boolean[][] col,
boolean[][] block,
char[][] board) {
if(i == board.length) {
return true;
}
if(j == n) {
return dfs(i + 1, 0, row, col, block, board);
}
if(board[i][j] != '.') {
return dfs(i ,j + 1, row, col, block, board);
}
for(int k = 1; k <= 9; k++) {
if(row[i][k] || col[j][k] || block[3 * (i / 3) + j / 3][k]) {
continue;
}
row[i][k] = true;
col[j][k] = true;
block[3 * (i / 3) + j / 3][k] = true;
board[i][j] = (char) (k + '0');
if(dfs(i, j + 1, row, col, block, board)) {
return true;
}
row[i][k] = false;
col[j][k] = false;
block[3 * (i / 3) + j / 3][k] = false;
board[i][j] = '.';
}
return false;
}
}