-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.java
More file actions
159 lines (124 loc) · 2.72 KB
/
Copy pathBoard.java
File metadata and controls
159 lines (124 loc) · 2.72 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
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
import java.util.HashSet;
import java.util.Set;
/*
* Sudoku board class for a 9x9 sudoku
*/
class Board {
byte[][] board;
int N = 9;
public Board() {
board = new byte[N][N];
}
public Board(byte[][] preset) {
board = preset;
}
public void set(int idx, int val) {
int row = idx / N;
int col = idx % N;
board[row][col] = (byte) val;
}
public int get(int idx) {
int row = idx / N;
int col = idx % N;
return board[row][col];
}
public Set<Integer> getPossibleCol(int col) {
boolean[] used = new boolean[10];
for (int i = 0; i < N; i++)
used[board[i][col]] = true;
Set<Integer> ans = new HashSet<Integer>();
for (int i = 1; i < 10; i++) {
if (!used[i])
ans.add(i);
}
return ans;
}
public Set<Integer> getPossibleSq(int s) {
// Check all squares
int sqrow = (s / 3) * 3;
int sqcol = (s % 3) * 3;
boolean[] seen = new boolean[N + 1];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
int cur = board[sqrow + i][sqcol + j];
seen[cur] = true;
}
}
Set<Integer> ans = new HashSet<Integer>();
for (int i = 1; i < 10; i++) {
if (!seen[i])
ans.add(i);
}
return ans;
}
public Set<Integer> getPossibleRow(int row) {
boolean[] used = new boolean[10];
for (int j = 0; j < N; j++) {
used[board[row][j]] = true;
}
Set<Integer> ans = new HashSet<Integer>();
for (int i = 1; i < 10; i++) {
if (!used[i])
ans.add(i);
}
return ans;
}
public boolean invalidState() {
// Check all rows
for (int i = 0; i < N; i++) {
boolean[] seen = new boolean[N + 1];
for (int j = 0; j < N; j++) {
int cur = board[i][j];
if (cur != 0 && seen[cur])
return true;
seen[cur] = true;
}
}
// Check all columns
for (int j = 0; j < N; j++) {
boolean[] seen = new boolean[N + 1];
for (int i = 0; i < N; i++) {
int cur = board[i][j];
if (cur != 0 && seen[cur])
return true;
seen[cur] = true;
}
}
// Check all squares
for (int s = 0; s < N; s++) {
int sqrow = (s / 3) * 3;
int sqcol = (s % 3) * 3;
boolean[] seen = new boolean[N + 1];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
int cur = board[sqrow + i][sqcol + j];
if (cur != 0 && seen[cur]) {
return true;
}
seen[cur] = true;
}
}
}
return false;
}
@Override
public String toString() {
StringBuilder b = new StringBuilder();
for (int i = 0; i < N * N; i++) {
if (i % N == 0)
b.append("\n");
b.append(get(i) + " ");
}
return b.toString();
}
/* Creates a deep clone */
public Board clone() {
byte[][] clone = new byte[N][N];
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
clone[i][j] = board[i][j];
}
}
return new Board(clone);
}
}