Leetcode-DSF-Trie-Backtrack

本文介绍了一个解决WordSearch II问题的算法实现。该问题要求在一个二维字符矩阵中找出所有由相邻字母组成的字典中的单词。文章详细阐述了算法的思路与实现细节,并通过具体的代码展示了如何进行高效搜索。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Word Search II

Given a 2D board and a list of words from the dictionary, find all words in the board.

Each word must be constructed from letters of sequentially adjacent cell, where “adjacent” cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once in a word.

For example,
Given words = [“oath”,”pea”,”eat”,”rain”] and board =

[
[‘o’,’a’,’a’,’n’],
[‘e’,’t’,’a’,’e’],
[‘i’,’h’,’k’,’r’],
[‘i’,’f’,’l’,’v’]
]
Return [“eat”,”oath”].
Note:
You may assume that all inputs are consist of lowercase letters a-z.

typedef pair<int, int> pint;
auto isLeftOK = [](const size_t &L, const size_t &R, char c, vector<vector<char>>& board, vector<vector<bool>> &vvb)->bool{
        if (R < 1) return false;
        else if ( !vvb[L][R-1] && c == board[L][R-1]) return true;
        else return false;
    };
    auto isRightOK = [](const size_t &L, const size_t &R, char c, vector<vector<char>>& board, vector<vector<bool>> &vvb)->bool{
        if (R+1 >= board[0].size()) return false;
        else if ( !vvb[L][R+1] && c == board[L][R+1]) return true;
        else return false;
    };
    auto isUpOK = [](const size_t &L, const size_t &R, char c, vector<vector<char>>& board, vector<vector<bool>> &vvb)->bool{
        if (L < 1) return false;
        else if ( !vvb[L-1][R] && c == board[L-1][R]) return true;
        else return false;
    };
    auto isDownOK = [](const size_t &L, const size_t &R, char c, vector<vector<char>>& board, vector<vector<bool>> &vvb)->bool{
        if (L+1 >= board.size()) return false;
        else if ( !vvb[L+1][R] && c == board[L+1][R]) return true;
        else return false;
    };
class Solution {
public:
    unordered_multimap<char, pair<int, int>> mmps;
    typedef pair<int,int> pint;
    bool isIn(vector<vector<char>>& board, string &word, size_t index, size_t L, size_t R, vector<vector<bool>> &vvb) {
        if (index >= word.size()) return true;
        if (isLeftOK(L, R, word[index], board, vvb)) {
            vvb[L][R-1] = true;
            if( isIn(board, word, index+1, L, R-1, vvb)) 
            {
                vvb[L][R-1] = false;
                return true;
            }
            vvb[L][R-1] = false;
        }
        if (isRightOK(L, R, word[index], board, vvb)) {
            vvb[L][R+1] = true;
            if (isIn(board, word, index+1, L, R+1, vvb)) {
                vvb[L][R+1] = false;
                return true;
            }
            vvb[L][R+1] = false;
        }
        if (isUpOK(L, R, word[index], board, vvb)) {
            vvb[L-1][R] = true;
            if (isIn(board, word, index+1, L-1, R, vvb)) {
                vvb[L-1][R] = false;
                return true;
            }
            vvb[L-1][R] = false;
        }
        if (isDownOK(L, R, word[index], board, vvb)) {
            vvb[L+1][R] = true;
            if (isIn(board, word, index+1, L+1, R, vvb)) {
                vvb[L+1][R] = false;
                return true;
            }
            vvb[L+1][R] = false;
        }
        return false;
    }
    //
    bool isInBoard(vector<vector<char>>& board, string &word) {
    string word0(word.rbegin(), word.rend());
    bool res;
    if (mmps.empty()) {

            for (size_t i = 0; i < board.size(); ++i) {

                for (size_t j = 0; j < board[i].size(); ++j) {
                    mmps.insert(pair<char, pint>(board[i][j], pint(i,j)));
                }

        }
    }
     vector<vector<bool>> vvb(board.size(), vector<bool>(board[0].size(), false));
    // forward
    char c = word[0];
    auto resfs = mmps.equal_range(c);
    for (auto it = resfs.first; it != resfs.second; it++) {
        res = false;
        vvb[it->second.first][it->second.second] = true;
        res = isIn(board, word, 1, it->second.first, it->second.second, vvb);
        vvb[it->second.first][it->second.second] = false;
        if (res) return res;
    }
    // reverse
    c = word0[0];
    resfs = mmps.equal_range(c);
    for (auto it = resfs.first; it != resfs.second; it++) {
        res = false;
        vvb[it->second.first][it->second.second] = true;
        res = isIn(board, word0, 1, it->second.first, it->second.second, vvb);
        vvb[it->second.first][it->second.second] = false;
        if (res) return res;
    }
    return false;

}
    vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
        vector<string> res;
        unordered_set<string> s1(words.begin(), words.end());
        vector<string> words0(s1.begin(), s1.end());
        for (auto &i : words0) {
            if (isInBoard(board, i)) 
                res.push_back(i);
        }
        return res;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值