-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSudoku.java
107 lines (79 loc) · 2.23 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
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
public class Sudoku {
private int[][] board;
//constructor
public Sudoku (int[][] board) {
this.board = board;
}
//get board
public int[][] getBoard() {
return board;
}
//solve method helper
public void solve() {
solve(0,0);
}
//solves the next position and returns true if it is possible
private boolean nextPos(int row, int col) {
if (col < 8) {
return solve(row, col + 1);
} else {
return solve(row + 1, 0);
}
}
//solves the sudoku
private boolean solve(int row, int col) {
//base case
if(row > 8) {
return true;
}
//checks for already existing numbers
if(board[row][col] != 0) {
if(check(row, col, board[row][col])) {
return nextPos(row, col);
}
}
//checks each number from 1 to 9
for(int i = 1; i < 10; i++) {
if(check(row, col, i)) {
board[row][col] = i;
if(nextPos(row, col)) {
return true;
}
}
//clears the previous positions
board[row][col] = 0;
}
return false;
}
//checks for each scenario
public boolean check(int row, int col, int x) {
//checks for rows and column
for(int i = 0; i < board.length; i++) {
if(i != row && board[i][col] == x) {
return false;
}
if(i != col && board[row][i] == x) {
return false;
}
}
int squareRow = (row/3) * 3;
int squareCol = (col/3) * 3;
//checks for 3x3
for(int i = squareRow; i < squareRow+3; i++) {
for(int j = squareCol; j < squareCol+3; j++) {
if((j != col || i != row) && board[i][j] == x) {
return false;
}
}
}
return true;
}
//prints the board
public void print() {
for(int r = 0; r < board.length; r++) {
for (int c = 0; c < board[r].length; c++)
System.out.print(board[r][c] + " ");
System.out.println();
}
}
}