-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSudoku.java
148 lines (134 loc) · 3.7 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.lang.StringBuilder;
import java.util.ArrayList;
import java.util.List;
public class Sudoku {
private final int[][] board;
public Sudoku(int[][] board) {
this.board = board;
}
public static Sudoku fromString(String puzzle) {
int[][] board = new int[9][9];
for (int r = 0; r < 9; r++) {
for (int c = 0; c < 9; c++) {
board[r][c] = puzzle.charAt(r*9+c) == '.' ? 0 : Character.getNumericValue(puzzle.charAt(r*9+c));
}
}
return new Sudoku(board);
}
private boolean isValid() {
// rows
for (int r = 0; r < 9; r++) {
int[] seen = new int[10];
for (int c = 0; c < 9; c++) {
if (board[r][c] > 0 && ++seen[board[r][c]] > 1) {
return false;
}
}
}
// cols
for (int c = 0; c < 9; c++) {
int[] seen = new int[10];
for (int r = 0; r < 9; r++) {
if (board[r][c] > 0 && ++seen[board[r][c]] > 1) {
return false;
}
}
}
// boxes
for (int b = 0; b < 9; b++) {
int[] seen = new int[10];
int boxRow = b / 3;
int boxCol = b % 3;
for (int r = boxRow * 3; r < boxRow * 3 + 3; ++r) {
for (int c = boxCol * 3; c < boxCol * 3 + 3; ++c) {
if (board[r][c] > 0 && ++seen[board[r][c]] > 1) {
return false;
}
}
}
}
return true;
}
private int nextEmpty() {
for (int r = 0; r < 9; r++) {
for (int c = 0; c < 9; c++) {
if (board[r][c] == 0) {
return r * 9 + c;
}
}
}
return -1;
}
public boolean solve() {
if (!this.isValid()) {
return false;
}
int nextEmpty = this.nextEmpty();
// complete
if (nextEmpty < 0) {
return true;
}
int r = nextEmpty / 9;
int c = nextEmpty % 9;
for (int v = 1; v <= 9; ++v) {
this.board[r][c] = v;
boolean isDone = this.solve();
if (isDone) {
return true;
}
this.board[r][c] = 0;
}
return false;
}
public String toString() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
sb.append(board[i][j]);
}
}
return sb.toString();
}
public static List<String> readFile(String filename) throws FileNotFoundException, IOException {
List<String> lines = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new FileReader(filename))) {
for (String line; (line = br.readLine()) != null; ) {
String cleaned = line.strip();
if (!cleaned.equals("")) {
lines.add(cleaned);
}
}
}
return lines;
}
public static void solve(List<String> lines) {
for (int i = 0; i < lines.size(); i += 2) {
String input = lines.get(i);
String expected = lines.get(i + 1);
long start = System.nanoTime();
Sudoku s = Sudoku.fromString(input);
s.solve();
String output = s.toString();
long end = System.nanoTime();
if (output.equals(expected)) {
System.out.println(String.format("Solved sudoku %s in %f ms", input, (end - start) / 1000000.0));
} else {
System.out.println(String.format("Failed to solve sudoku %s. Expected %s, got %s", input, expected, output));
System.exit(1);
}
}
}
public static void main(String[] args) throws FileNotFoundException, IOException {
if (args.length < 1) {
System.out.println("Please provide the name of an input file as an argument.");
System.exit(1);
}
String filename = args[0];
List<String> lines = readFile(filename);
solve(lines);
}
}