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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
|
/* vim: set expandtab tabstop=4 shiftwidth=4 : */
import java.util.Set;
import java.util.HashSet;
import java.util.Arrays;
public class BoggleSolverDicho
{
private class BoggleDictState
{
private String s;
private int idx, from, to;
private boolean isWord;
public BoggleDictState(int from, int to)
{
this.s = "";
this.idx = -1;
this.from = from;
this.to = to;
this.isWord = false;
}
public BoggleDictState(String s, int idx, int from, int to,
boolean isWord)
{
this.s = s;
this.idx = idx;
this.from = from;
this.to = to;
this.isWord = isWord;
}
public String s() { return s; }
public int idx() { return idx; }
public int from() { return from; }
public int to() { return to; }
public boolean isWord() { return isWord; }
public int n() { return (to - from + 1); }
public String toString(String[] words)
{
String ret = s + "(" + idx + ") " + isWord + " "
+ " [" + from + ";" + to + "](" + n()+") ";
if (from > 0) ret += words[from - 1];
ret += " ["+words[from]+"..."+words[to]+"] ";
if (to < (words.length - 1)) ret += words[to + 1];
return ret;
}
}
private class BoggleDict
{
private String[] words;
public BoggleDict(String[] words)
{
this.words = Arrays.copyOf(words, words.length);
}
public int size()
{
return words.length;
}
public boolean exists(String word) {
if (word.isEmpty()) return false;
BoggleDictState st = new BoggleDictState(0, (size() - 1));
for (int i = 0; i < word.length(); i++) {
st = filter(word.charAt(i), st);
if (st == null) return false;
}
return st.isWord();
}
private BoggleDictState filter(char c, BoggleDictState st)
{
int idx = st.idx() + 1;
int from = st.from();
int to = st.to();
int n = st.n();
if (words[from].length() <= idx) {
if (n == 1) return null;
from++;
}
boolean fromOk = (words[from].charAt(idx) == c);
boolean toOk = (words[to].charAt(idx) == c);
if (!fromOk && (n == 1)) return null;
// if (n < 200) {
// // lower bound
// if (!fromOk) {
// for (int i = from; i <= to; i++) {
// if (words[i].charAt(idx) == c) {
// fromOk = true;
// from = i;
// break;
// }
// }
// if (!fromOk) return null;
// }
// // upper bound
// if (!toOk) {
// for (int i = to; i >= from; i--) {
// if (words[i].charAt(idx) == c) {
// toOk = true;
// to = i;
// break;
// }
// }
// if (!toOk) return null;
// }
// }
// lower bound
if (!fromOk) {
int tob = to;
int mid = (from + tob) / 2;
while (mid != from) {
int diff = words[mid].charAt(idx) - c;
if (diff > 0) {
to = mid;
tob = mid;
} else if (diff < 0)
from = mid;
else
tob = mid;
mid = (tob + from) / 2;
}
if (!(words[from].charAt(idx) == c)) {
if (words[tob].charAt(idx) == c) from = tob;
else return null;
}
}
// upper bound
if (!toOk) {
int fromb = from;
int mid = (fromb + to) / 2;
while (mid != fromb) {
int diff = words[mid].charAt(idx) - c;
if (diff <= 0)
fromb = mid;
else
to = mid;
mid = (fromb + to) / 2;
}
if (!(words[to].charAt(idx) == c)) {
if (words[fromb].charAt(idx) == c) to = fromb;
else return null;
}
}
boolean isWord = ((idx > 1) && ((words[from].length() - 1) == idx));
return new BoggleDictState((st.s() + c), idx, from, to, isWord);
}
}
private int cols, rows;
private BoggleDict dict;
private BoggleBoard board;
private boolean[] visited;
private Set<String> result;
// assume each word in the dictionary contains only [A-Z]
public BoggleSolverDicho(String[] dictionary)
{
dict = new BoggleDict(dictionary);
}
// Returns the set of all valid words in the given Boggle board
public Iterable<String> getAllValidWords(BoggleBoard b)
{
this.board = b;
this.cols = board.cols();
this.rows = board.rows();
this.result = new HashSet<String>();
BoggleDictState emptyState = new BoggleDictState(0, (dict.size() - 1));
for (int r = 0; r < this.rows; r++) {
for (int c = 0; c < this.cols; c++) {
this.visited = new boolean[this.rows * this.cols];
explore(r, c, emptyState);
}
}
return result;
}
//
private void explore(int r, int c, BoggleDictState current)
{
if ((r < 0) || (r >= rows) || (c < 0) || (c >= cols)) return;
int visitedIdx = (r * this.cols) + c;
if (visited[visitedIdx]) return;
char ch = board.getLetter(r, c);
BoggleDictState next = dict.filter(ch, current);
if (next == null) return;
if (ch == 'Q') {
next = dict.filter('U', next);
if (next == null) return;
}
if (next.isWord()) result.add(next.s());
visited[visitedIdx] = true;
explore((r - 1), (c - 1), next);
explore((r - 1), c, next);
explore((r - 1), (c + 1), next);
explore(r, (c - 1), next);
explore(r, (c + 1), next);
explore((r + 1), (c - 1), next);
explore((r + 1), c, next);
explore((r + 1), (c + 1), next);
visited[visitedIdx] = false;
}
// Returns the score if it is in the dictionary, zero otherwise.
// assume the word contains only [A-Z]
public int scoreOf(String word)
{
if (!dict.exists(word)) return 0;
int l = word.length();
if (l < 3) {
return 0;
} else if (l < 5) {
return 1;
} else if (l < 6) {
return 2;
} else if (l < 7) {
return 3;
} else if (l < 8) {
return 5;
} else {
return 11;
}
}
public static void main(String[] args)
{
if (args[0].equals("timing")) {
In in = new In("./data/dictionary-yawl.txt");
String[] dictionary = in.readAllStrings();
Stopwatch w = new Stopwatch();
BoggleSolver solver = new BoggleSolver(dictionary);
double dt = +w.elapsedTime();
System.out.println("construction : "+dt);
String s = "board-pneumonoultramicroscopicsilicovolcanoconiosis.txt";
BoggleBoard []boards = new BoggleBoard[5];
boards[0] = new BoggleBoard("./data/board-points4527.txt");
boards[1] = new BoggleBoard("./data/board-points13464.txt");
boards[2] = new BoggleBoard("./data/board-points4410.txt");
boards[3] = new BoggleBoard("./data/board-points1250.txt");
boards[4] = new BoggleBoard("./data/board-points1500.txt");
// boards[1] = new BoggleBoard("./data/board-rotavator.txt");
// boards[2] = new BoggleBoard("./data/board-16q.txt");
// boards[4] = new BoggleBoard("./data/" + s);
for (int t = 0; t < 5; t++) {
w = new Stopwatch();
for (int i = 0; i < 3000; i++) {
solver.getAllValidWords(boards[i % 5]);
}
dt = +w.elapsedTime();
System.out.println("search time : "+dt);
}
} else {
In in = new In(args[0]);
String[] dictionary = in.readAllStrings();
BoggleSolverDicho solver = new BoggleSolverDicho(dictionary);
BoggleBoard board = new BoggleBoard(args[1]);
int score = 0;
for (String word : solver.getAllValidWords(board))
{
StdOut.println(word);
score += solver.scoreOf(word);
}
StdOut.println("Score = " + score);
StdOut.println("Score = " + solver.scoreOf("REQUEST"));
}
}
}
|