LeetCode系列79—单词搜索

题意

79. 单词搜索

题解

DFS+回溯

class Solution {
    
    
public:
    bool check(vector<vector<char>>& board, vector<vector<int>>& visited, int i, int j, string& s, int k) {
    
    
        if (board[i][j] != s[k]) {
    
    
            return false;
        } else if (k == s.length() - 1) {
    
    
            return true;
        }
        visited[i][j] = true;
        vector<pair<int, int>> directions{
    
    {
    
    0, 1}, {
    
    0, -1}, {
    
    1, 0}, {
    
    -1, 0}};
        bool result = false;
        for (const auto& dir: directions) {
    
    
            int newi = i + dir.first, newj = j + dir.second;
            if (newi >= 0 && newi < board.size() && newj >= 0 && newj < board[0].size()) {
    
    
                if (!visited[newi][newj]) {
    
    
                    bool flag = check(board, visited, newi, newj, s, k + 1);
                    if (flag) {
    
    
                        result = true;
                        break;
                    }
                }
            }
        }
        visited[i][j] = false;
        return result;
    }

    bool exist(vector<vector<char>>& board, string word) {
    
    
        int h = board.size(), w = board[0].size();
        vector<vector<int>> visited(h, vector<int>(w));
        for (int i = 0; i < h; i++) {
    
    
            for (int j = 0; j < w; j++) {
    
    
                bool flag = check(board, visited, i, j, word, 0);
                if (flag) {
    
    
                    return true;
                }
            }
        }
        return false;
    }
};

猜你喜欢

转载自blog.csdn.net/younothings/article/details/120099141