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;
    }
};
相关推荐
Q741_14739 分钟前
每日一题 力扣 1848. 到目标元素的最小距离 模拟 C++题解
c++·算法·leetcode·模拟
语戚11 小时前
力扣 968. 监控二叉树 —— 贪心 & 树形 DP 双解法递归 + 非递归全解(Java 实现)
java·算法·leetcode·贪心算法·动态规划·力扣·
skywalker_1111 小时前
力扣hot100-7(接雨水),8(无重复字符的最长子串)
算法·leetcode·职场和发展
田梓燊12 小时前
leetcode 160
算法·leetcode·职场和发展
_深海凉_13 小时前
LeetCode热题100-颜色分类
python·算法·leetcode
6Hzlia13 小时前
【Hot 100 刷题计划】 LeetCode 136. 只出现一次的数字 | C++ 哈希表&异或基础解法
c++·算法·leetcode
无限进步_15 小时前
【C++】只出现一次的数字 II:位运算的三种解法深度解析
数据结构·c++·ide·windows·git·算法·leetcode
_深海凉_16 小时前
LeetCode热题100-找到字符串中所有字母异位词
算法·leetcode·职场和发展
木井巳16 小时前
【递归算法】目标和
java·算法·leetcode·决策树·深度优先
旖-旎16 小时前
哈希表(字母异位次分组)(5)
数据结构·c++·算法·leetcode·哈希算法·散列表