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;
    }
};
相关推荐
想吃火锅10055 天前
【leetcode】121.买卖股票的最佳时机js/c++
算法·leetcode·职场和发展
凌波粒5 天前
LeetCode--491.递增子序列(回溯算法)
数据结构·算法·leetcode
退休倒计时5 天前
【每日一题】LeetCode 146. LRU 缓存 TypeScript
算法·leetcode·缓存·typescript
小欣加油5 天前
leetcode3612 用特殊操作处理字符串I
数据结构·c++·算法·leetcode·职场和发展
凌波粒5 天前
LeetCode--90.子集II(回溯算法)
数据结构·算法·leetcode
凌波粒5 天前
LeetCode--46.全排列(回溯算法)
数据结构·算法·leetcode
吃着火锅x唱着歌5 天前
LeetCode 2530.执行K次操作后的最大分数
数据结构·算法·leetcode
sheeta19985 天前
LeetCode 每日一题笔记 日期:2026.06.16 题目:3612. 字符串特殊符号处理
笔记·算法·leetcode
CoderYanger5 天前
A.每日一题:2095. 删除链表的中间节点
java·数据结构·程序人生·leetcode·链表·面试·职场和发展
青山木5 天前
Hot 100 --- 矩阵置零
线性代数·算法·leetcode·矩阵·哈希算法