| 24 | } |
| 25 | |
| 26 | public void dfs( |
| 27 | int r, |
| 28 | int c, |
| 29 | Trie node, |
| 30 | String word, |
| 31 | HashSet<String> res, |
| 32 | HashSet<String> visit, |
| 33 | char[][] board, |
| 34 | Trie root |
| 35 | ) { |
| 36 | if ( |
| 37 | r < 0 || |
| 38 | c < 0 || |
| 39 | r == ROWS || |
| 40 | c == COLS || |
| 41 | !node.children.containsKey(board[r][c]) || |
| 42 | node.children.get(board[r][c]).refs < 1 || |
| 43 | visit.contains(r + "-" + c) |
| 44 | ) { |
| 45 | return; |
| 46 | } |
| 47 | |
| 48 | visit.add(r + "-" + c); |
| 49 | node = node.children.get(board[r][c]); |
| 50 | word += board[r][c]; |
| 51 | if (node.isWord) { |
| 52 | node.isWord = false; |
| 53 | res.add(word); |
| 54 | root.removeWord(word); |
| 55 | } |
| 56 | |
| 57 | dfs(r + 1, c, node, word, res, visit, board, root); |
| 58 | dfs(r - 1, c, node, word, res, visit, board, root); |
| 59 | dfs(r, c + 1, node, word, res, visit, board, root); |
| 60 | dfs(r, c - 1, node, word, res, visit, board, root); |
| 61 | visit.remove(r + "-" + c); |
| 62 | } |
| 63 | |
| 64 | class Trie { |
| 65 | |