(Node x, BoggleBoard board, int i, int j, StringBuilder sb, boolean[][] visited,
HashSet<String> set)
| 63 | } |
| 64 | |
| 65 | private void dfs(Node x, BoggleBoard board, int i, int j, StringBuilder sb, boolean[][] visited, |
| 66 | HashSet<String> set) { |
| 67 | char c = board.getLetter(i, j); |
| 68 | Node next = x.children[c - 'A']; |
| 69 | if (next == null) return; |
| 70 | |
| 71 | // deal with Qu |
| 72 | if (c == 'Q') { |
| 73 | next = next.children['U' - 'A']; |
| 74 | if (next == null) return; |
| 75 | } |
| 76 | |
| 77 | sb.append(c); |
| 78 | if (c == 'Q') sb.append('U'); // deal with Qu |
| 79 | visited[i][j] = true; |
| 80 | |
| 81 | if (sb.length() >= 3 && next.isTernimal) |
| 82 | set.add(sb.toString()); |
| 83 | |
| 84 | for (int[] dir : dirs) { |
| 85 | int ii = i + dir[0]; |
| 86 | int jj = j + dir[1]; |
| 87 | if (isValidGrid(board, ii, jj) && !visited[ii][jj]) |
| 88 | dfs(next, board, ii, jj, sb, visited, set); |
| 89 | } |
| 90 | // backtracking |
| 91 | visited[i][j] = false; |
| 92 | sb.deleteCharAt(sb.length() - 1); |
| 93 | if (c == 'Q') sb.deleteCharAt(sb.length() - 1); // deal with Qu |
| 94 | } |
| 95 | |
| 96 | private boolean isValidGrid(BoggleBoard board, int row, int col) { |
| 97 | return col >= 0 && col < board.cols() && row >= 0 && row < board.rows(); |
no test coverage detected