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;
    }
};
相关推荐
smj2302_7968265211 小时前
解决leetcode第3753题范围内总波动值II
python·算法·leetcode
leoufung14 小时前
LeetCode 92 反转链表 II 全流程详解
算法·leetcode·链表
im_AMBER16 小时前
Leetcode 59 二分搜索
数据结构·笔记·学习·算法·leetcode
leoufung16 小时前
LeetCode 61. 旋转链表(Rotate List)题解与思路详解
leetcode·链表·list
leoufung21 小时前
逆波兰表达式 LeetCode 题解及相关思路笔记
linux·笔记·leetcode
Aspect of twilight1 天前
LeetCode华为大模型岗刷题
python·leetcode·华为·力扣·算法题
2301_807997381 天前
代码随想录-day47
数据结构·c++·算法·leetcode
Elias不吃糖1 天前
LeetCode每日一练(3)
c++·算法·leetcode
小年糕是糕手1 天前
【C++】类和对象(二) -- 构造函数、析构函数
java·c语言·开发语言·数据结构·c++·算法·leetcode
sheeta19981 天前
LeetCode 每日一题笔记 日期:2025.11.24 题目:1018. 可被5整除的二进制前缀
笔记·算法·leetcode