Skip to content

Instantly share code, notes, and snippets.

@jmrchelani
Last active May 25, 2021 18:16
Show Gist options
  • Save jmrchelani/63588e439b246265de2fad5b5b69b0ec to your computer and use it in GitHub Desktop.
Save jmrchelani/63588e439b246265de2fad5b5b69b0ec to your computer and use it in GitHub Desktop.
static void solver(BoggleBoard board) {
for (int i = 0; i < board.getRows(); i++) {
for (int j = 0; j < board.getColumns(); j++) {
solver(board, board.getCharAt(i,j) + "", i, j);
}
}
}
static private void solver(
BoggleBoard board,
String word,
int x, int y)
{
if (trie.containsWord(word) && !answer.contains(word)) {
answer.add(word);
counter++;
}
if (!trie.containsPrefix(word)) { return; }
//upper left
if (0 <= x - 1 && 0 <= y - 1 && !board.isVisited(x - 1,y - 1)){
solver(board, word+board.getCharAt(x-1,y-1), x-1, y-1);
}
//up
if (0 <= y - 1 && !board.isVisited(x,y - 1)){
solver(board, word + board.getCharAt(x,y-1), x, y-1);
}
//upper right
if (x + 1 < board.getRows() && 0 <= y - 1 && !board.isVisited(x + 1,y - 1)){
solver(board, word+board.getCharAt(x+1,y-1), x+1, y-1);
}
//right
if (x + 1 < board.getRows() && !board.isVisited(x + 1,y)){
solver(board, word + board.getCharAt(x+1,y), x+1, y);
}
//lower right
if (x+1 < board.getRows() && y+1 < board.getColumns() && !board.isVisited(x+1,y+1)){
solver(board, word+board.getCharAt(x+1,y+1), x+1, y+1);
}
//down
if (y + 1 < board.getColumns() && !board.isVisited(x,y + 1)){
solver(board, word + board.getCharAt(x,y+1), x, y+1);
}
//lower left
if (0 <= x - 1 && y + 1 < board.getColumns() && !board.isVisited(x - 1,y + 1)){
solver(board, word+board.getCharAt(x-1,y+1), x-1, y+1);
}
//left
if (0 <= x - 1 && !board.isVisited(x - 1,y)){
solver(board, word + board.getCharAt(x-1,y), x-1, y);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment