-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsodukuSolver.java
More file actions
53 lines (44 loc) · 1.22 KB
/
sodukuSolver.java
File metadata and controls
53 lines (44 loc) · 1.22 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
//Solving soduku 9x9 board
public void solveSudoku(char[][] board) {
char[][] fakeboard = new char[9][9];
deepcopy(board, fakeboard);
fillSudoku(fakeboard, 0, board);
}
public void fillSudoku(char[][] board, int c, char[][] realboard){
if(c==81){
deepcopy(board, realboard);
return;
}
int x = c/9;
int y = c%9;
if(board[x][y] != '.')
fillSudoku(board,c+1,realboard);
else{
for(int i=1; i<10; i++)
if(canfill(board, x, y, i)){
board[x][y]=(char)('0'+i);
fillSudoku(board,c+1,realboard);
board[x][y]='.';
}
}
}
public boolean canfill(char[][] board, int x, int y, int t){
char temp = (char)('0'+t);
for(int i=0; i<9; i++){
if(board[x][i] == temp || board[i][y] == temp)
return false;
}
int bx = x/3;
int by = y/3;
for(int p=bx*3; p<bx*3+3; p++)
for(int q=by*3; q<by*3+3; q++){
if(board[p][q]== temp)
return false;
}
return true;
}
public void deepcopy (char[][] arr1, char[][] arr2){
for(int i=0; i<arr1.length; i++)
for(int j=0; j<arr1[0].length; j++)
arr2[i][j] = arr1[i][j];
}