79. Word Search

题目描述

79. Word Search

回溯

代码一,使用used数组

cpp 复制代码
class Solution {
    vector<pair<int,int>> directions{{0,1},{0,-1},{1,0},{-1,0}};
    vector<vector<bool>> used;
public:
    bool exist(vector<vector<char>>& board, string word) {
        used.resize(board.size(),vector<bool>(board[0].size(),false));
        for(int i = 0;i < board.size();i++){
            for(int j = 0;j < board[i].size();j++){
                if(board[i][j] != word[0] || used[i][j] == true)
                    continue;
                used[i][j] = true;
                if(backtrack(board,word,1,i,j))
                    return true;
                used[i][j] = false;
            }
        }
        return false;
    }

    bool backtrack(vector<vector<char>>& board, string &word,int idx,int row,int col){
        if(idx == word.size())
            return true;

        for(const auto& dir: directions){
            int newrow = row+dir.first;
            int newcol = col+dir.second;
            if(newrow<0 || newrow>=board.size() || newcol<0 || newcol>= board[0].size())
                continue;
            if(used[newrow][newcol])
                continue;
            if(board[newrow][newcol] == word[idx]){
                used[newrow][newcol] = true;
                if(backtrack(board,word,idx+1,newrow,newcol))
                    return true;
                used[newrow][newcol] = false;
            }
        }
        return false;
    }
};

代码二,不使用used数组

cpp 复制代码
class Solution {
    vector<pair<int,int>> directions{{0,1},{0,-1},{1,0},{-1,0}};
public:
    bool exist(vector<vector<char>>& board, string word) {
        for(int i = 0;i < board.size();i++){
            for(int j = 0;j < board[i].size();j++){
                if(board[i][j] != word[0])
                    continue;
                board[i][j] = '#';//word仅由大小写英文字母组成,将board[i][j]标记为#表示board[i][j]已经被使用
                if(backtrack(board,word,1,i,j))
                    return true;
                board[i][j] = word[0];//恢复原字符
            }
        }
        return false;
    }

    bool backtrack(vector<vector<char>>& board, string &word,int idx,int row,int col){
        if(idx == word.size())
            return true;

        for(const auto& dir: directions){
            int newrow = row+dir.first;
            int newcol = col+dir.second;
            if(newrow<0 || newrow>=board.size() || newcol<0 || newcol>= board[0].size())
                continue;
            if(board[newrow][newcol] == word[idx]){
                board[newrow][newcol] = '#';
                if(backtrack(board,word,idx+1,newrow,newcol))
                    return true;
                board[newrow][newcol] = word[idx];//恢复原字符
            }
        }
        return false;
    }
};
相关推荐
hanlin031 小时前
刷题笔记:力扣第287题-寻找重复数
笔记·算法·leetcode
橘子汽水1681 小时前
Leetcode 322 279 零钱兑换,完全平方数
算法·leetcode
土司大王2 小时前
LeetCode hot100——51.N 皇后:Java 回溯模板、列与对角线剪枝、O(n!) 复杂度分析
java·leetcode·剪枝
木井巳3 小时前
【记忆化搜索】斐波那契数
java·算法·leetcode·深度优先·剪枝·推荐算法
Navigator_Z18 小时前
LeetCode //C - 1240. Tiling a Rectangle with the Fewest Squares
c语言·算法·leetcode
稻米哟18 小时前
力扣100——双指针
算法·leetcode
橘子汽水1681 天前
Leetcode 198,118打家劫舍,杨辉三角
算法·leetcode
alphaTao1 天前
LeetCode 每日一题 2026/9/7-2026/9/13
算法·leetcode
土司大王1 天前
LeetCode 79 单词搜索:Java 回溯模板、网格 DFS 与剪枝优化
java·算法·leetcode·深度优先