示例#1
0
 // Returns if the word is in the trie.
 bool search(string word) {
     TrieNode* itr = root;
     for (int i = 0; itr != NULL && i < word.length(); ++i) {
         itr = itr->locateCh(word[i]);
     }
     return (itr != NULL && itr->isWordEnd());
 }
    void searchWords(vector<vector<char>>& board, int i, int j, TrieNode* node, string pre, vector<string>& res) {
        if (i < 0 || i >= (int)board.size() || j < 0 || j >= (int)board[0].size())
            return;

        char c = board[i][j];
        TrieNode* cur = node->getTrieNode(c);
        if (cur == NULL)
            return;

        pre += c;
        if (cur->isWordEnd()) {
            if (find(res.begin(), res.end(), pre) == res.end()) // No dup
                res.push_back(pre);
        }

        board[i][j] = '*'; // mark as visited
        searchWords(board, i - 1, j, cur, pre, res);
        searchWords(board, i + 1, j, cur, pre, res);
        searchWords(board, i, j - 1, cur, pre, res);
        searchWords(board, i, j + 1, cur, pre, res);
        board[i][j] = c;
    }