-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoggleSolver.java
147 lines (124 loc) · 4.14 KB
/
BoggleSolver.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
public class BoggleSolver
{
private TrieST2<Integer> st;
private int col;
private int row;
private SET<String> set;
// 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)
{
st = new TrieST2<Integer>();
//for (int i=0;i<dictionary.length;i++) {
// if (dictionary[i].contains("QU")) st.put(dictionary[i], i);
// else if (!dictionary[i].contains("Q")) st.put(dictionary[i], i);
//}
for (String s:dictionary) {
st.put(s, 1);
}
}
// Returns the set of all valid words in the given Boggle board, as an Iterable.
public Iterable<String> getAllValidWords(BoggleBoard board)
{
this.set = new SET<String>();
for (int i=0;i<board.rows();i++) {
for (int j=0;j<board.cols();j++) {
DFS(board, i, j);
}
}
return set;
}
// 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 (st.contains(word)) {
//if (word.contains("QU")) word.replace("Q", "QU");
/*if (word.length()<=2 && word.length()>=0)
score = 0;
else if (word.length()>=3 && word.length() <=4)
score = 1;
else if (word.length() == 5)
score = 2;
else if (word.length() == 6)
score = 3;
else if (word.length() == 7)
score = 5;
else if (word.length() >= 8)
score = 11;*/
switch (word.length()) {
case 0:
case 1:
case 2:
return 0;
case 3:
case 4:
return 1;
case 5:
return 2;
case 6:
return 3;
case 7:
return 5;
default:
return 11;
}
} else {
return 0;
}
}
private boolean[][] marked;
private BoggleBoard b;
private void DFS(BoggleBoard board, int i, int j)
{
this.row = board.rows();
this.col = board.cols();
this.marked = new boolean[row][col];
this.b = board;
String prefix = "";
dfs(prefix, i, j);
}
private void dfs (String prefix, int i, int j)
{
if ((i<0 || i>=this.row) ||
(j<0 || j>=this.col) ||
marked[i][j] ||
!st.isPrefix(prefix) )
return;
marked[i][j] = true;
char letter = b.getLetter(i, j);
prefix = prefix + (letter == 'Q' ? "QU" : letter);
// prefix = prefix + letter;
// String prefix1 = prefix.replace("Q", "QU");
if (st.contains(prefix)&prefix.length()>2) set.add(prefix);
for (int ii = -1; ii <= 1; ii++)
for (int jj = -1; jj <= 1; jj++)
dfs(prefix, i + ii, j + jj);
marked[i][j] = false;
}
public static void main(String[] args)
{
long startTime = System.currentTimeMillis();
In in = new In("dictionary-yawl.txt");
// In in = new In("dictionary-algs4.txt");
// In in = new In("dictionary-16q.txt");
String[] dictionary = in.readAllStrings();
BoggleSolver solver = new BoggleSolver(dictionary);
BoggleBoard board = new BoggleBoard("board-q.txt");
System.out.println(board.toString());
//System.out.println(solver.getAllValidWords(board));
int score = 0;
int count = 0;
for (String word : solver.getAllValidWords(board))
{
StdOut.println(word);
score += solver.scoreOf(word);
count += 1;
}
StdOut.println("Score = " + score);
StdOut.println("Count = " + count);
long endTime = System.currentTimeMillis();
long totalTime = endTime - startTime;
System.out.println(totalTime);
}
}