-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoggleSolver.java
More file actions
223 lines (187 loc) · 4.7 KB
/
BoggleSolver.java
File metadata and controls
223 lines (187 loc) · 4.7 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import edu.princeton.cs.algs4.In;
import edu.princeton.cs.algs4.StdOut;
import java.util.HashSet;
public class BoggleSolver {
private Node root;
private static final int R = 26;
private int x[] = {-1,1,0,0,-1,-1,1,1};
private int y[] = {0,0,-1,1,-1,1,-1,1};
private class MyStringBuilder {
char[] value;
private int count;
MyStringBuilder(int capacity) {
value = new char[capacity];
}
public int length() {
return count;
}
public String toString() {
// Create a copy, don't share the array
return new String(value, 0, count);
}
public void append(char c) {
value[count++] = c;
if (c == 'Q') {
value[count++] = 'U';
}
}
public void deleteLast(int len) {
count -= len;
}
}
private class Node {
private Node[] next;
private boolean isString;
public boolean isString() {
return isString;
}
private Node() {
this.next = new Node[R];
}
}
private boolean contains(String key) {
if (key == null) {
throw new IllegalArgumentException("argument to contains() is null");
} else {
Node x = this.get(root, key, 0);
return x == null ? false : x.isString;
}
}
private Node get(Node x, String key, int d) {
if (x == null) {
return null;
} else if (d == key.length()) {
return x;
} else {
int c = key.charAt(d) - 'A';
return this.get(x.next[c], key, d + 1);
}
}
private void add(String key) {
if (key == null) {
throw new IllegalArgumentException("argument to add() is null");
} else {
this.root = this.add(this.root, key, 0);
}
}
private Node add(Node x, String key, int d) {
if (x == null) {
x = new Node();
}
if (d == key.length()) {
x.isString = true;
} else {
int c = key.charAt(d) - 'A';
x.next[c] = this.add(x.next[c], key, d + 1);
}
return x;
}
// Initializes the data structure using the given array of strings as the dictionary.
// (You can assume each word in the dictionary contains only the uppercase letters A through Z.)
public BoggleSolver(String[] dictionary) {
for (int i=0; i<dictionary.length; i++) {
add(dictionary[i]);
}
}
// Returns the set of all valid words in the given Boggle board, as an Iterable.
public Iterable<String> getAllValidWords(BoggleBoard board) {
int row = board.rows();
int col = board.cols();
boolean [][] visited = new boolean[row][col];
char [] b = new char[row*col];
int id=0;
for (int i=0; i<row; i++) {
for (int j=0; j<col; j++) {
b[id++] = board.getLetter(i,j);
}
}
HashSet<String> s = new HashSet<>();
MyStringBuilder word = new MyStringBuilder(row * col * 2);
for (int i=0; i<row; i++) {
for (int j=0; j<col; j++) {
visited[i][j] = true;
dfs(i,j, board, s, visited, word, root);
visited[i][j] = false;
}
}
return s;
}
private boolean check(int i,int j, boolean [][] visited) {
if (i<0 || i >= visited.length) {
return false;
}
if (j<0 || j >= visited[0].length) {
return false;
}
return !visited[i][j];
}
private Node cut(int append, Node node, char c) {
if (append == 1 && node != null) {
return node.next[c-'A'];
}
if (append == 2 && node != null) {
node = node.next['Q'-'A'];
if (node != null) {
return node.next['U'-'A'];
}
}
return null;
}
private void dfs(int i, int j, BoggleBoard board, HashSet<String> s, boolean [][] visited, MyStringBuilder word, Node node) {
char c = board.getLetter(i,j);
int append = 1;
if (c == 'Q') {
append = 2;
}
Node n = cut(append, node,c);
if (n == null) {
return;
}
word.append(c);
if (word.length() >= 3 && n.isString()) {
s.add(word.toString());
}
for (int k=0; k<8; k++) {
int ni=i+x[k];
int nj=j+y[k];
if (check(ni, nj, visited)) {
visited[ni][nj] = true;
dfs(ni, nj, board, s, visited, word, n);
visited[ni][nj] = false;
}
}
word.deleteLast(append);
}
// Returns the score of the given word if it is in the dictionary, zero otherwise.
// (You can assume the word contains only the uppercase letters A through Z.)
public int scoreOf(String word) {
if (!contains(word)) {
return 0;
}
if (word.length() <= 2) {
return 0;
} else if (word.length() <=4) {
return 1;
} else if (word.length() <=5) {
return 2;
} else if (word.length() <=6) {
return 3;
} else if (word.length() <=7) {
return 5;
} else {
return 11;
}
}
public static void main(String[] args) {
In in = new In("dictionary-algs4.txt");
String[] dictionary = in.readAllStrings();
BoggleSolver solver = new BoggleSolver(dictionary);
BoggleBoard board = new BoggleBoard("board-q.txt");
int score = 0;
for (String word : solver.getAllValidWords(board)) {
StdOut.println(word);
score += solver.scoreOf(word);
}
StdOut.println("Score = " + score);
}
}