forked from matthewsamuel95/ACM-ICPC-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sudoku.java
133 lines (100 loc) · 3.56 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
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
public class Sudoku {
public final int SUDOKU_SIZE = 9;
public final int UNASSIGNED = 0;
private int unassignedPositionRow;
private int unassignedPositionColumn;
private int[][] matrix = new int[SUDOKU_SIZE][SUDOKU_SIZE];
public Sudoku() {
clear();
}
public void clear() {
for(int i = 0; i < SUDOKU_SIZE; i++)
for(int j = 0; j < SUDOKU_SIZE; j++)
matrix[i][j] = UNASSIGNED;
}
public void insertRandomNumbers(int n) {
for(int i = 0; i < n; i++) {
int randomRow = (int)(Math.random() * (SUDOKU_SIZE - 1));
int randomColumn = (int)(Math.random() * (SUDOKU_SIZE - 1));
int randomNumber = (int)(Math.random() * (SUDOKU_SIZE - 1) + 1);
matrix[randomRow][randomColumn] = randomNumber;
}
}
public boolean solve() {
if(!findUnassignedPosition())
return true;
for(int n = 1; n <= SUDOKU_SIZE; n++) {
if(canBePlaced(unassignedPositionRow, unassignedPositionColumn, n)) {
final int i = unassignedPositionRow;
final int j = unassignedPositionColumn;
matrix[i][j] = n;
if(solve())
return true;
matrix[i][j] = UNASSIGNED;
}
}
return false;
}
private boolean findUnassignedPosition() {
for(int i = 0; i < SUDOKU_SIZE; i++) {
for(int j = 0; j < SUDOKU_SIZE; j++) {
if(matrix[i][j] == UNASSIGNED) {
unassignedPositionRow = i;
unassignedPositionColumn = j;
return true;
}
}
}
return false;
}
private boolean canBePlaced(int row, int column, int number) {
return !isUsedInRow(row, number) && !isUsedInColumn(column, number) && !isUsedInBox(row, column, number);
}
private boolean isUsedInRow(int row, int number) {
for(int i = 0; i < SUDOKU_SIZE; i++)
if(matrix[row][i] == number)
return true;
return false;
}
private boolean isUsedInColumn(int column, int number) {
for(int i = 0; i < SUDOKU_SIZE; i++)
if(matrix[i][column] == number)
return true;
return false;
}
private boolean isUsedInBox(int row, int column, int number) {
int startRow = row - row % 3;
int startColumn = column - column % 3;
for(int i = 0; i < 3; i++)
for(int j = 0; j < 3; j++)
if(matrix[startRow + i][startColumn + j] == number)
return true;
return false;
}
public void print() {
System.out.println("-------------------------");
for(int i = 0; i < SUDOKU_SIZE; i++) {
for (int j = 0; j < SUDOKU_SIZE; j++) {
if (j == 0)
System.out.print("| ");
System.out.print(matrix[i][j] + " ");
if (j % 3 == 2)
System.out.print("| ");
}
System.out.println();
if(i % 3 == 2)
System.out.println("-------------------------");
}
}
public static void main(String[] args) {
Sudoku sudoku = new Sudoku();
sudoku.insertRandomNumbers(3);
System.out.println("Current sudoku state:");
sudoku.print();
System.out.println("Solving...");
if(sudoku.solve())
sudoku.print();
else
System.out.println("No solution found! :(");
}
}