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;
    }
};
相关推荐
@蓝莓果粒茶3 小时前
LeetCode第245题_最短单词距离III
c语言·c++·笔记·学习·算法·leetcode·c#
闻闻不会编程3 小时前
704. 二分查找 (力扣)
数据结构·算法·leetcode
技术帮扶户5 小时前
Leetcode-7 寻找用户推荐人
算法·leetcode·职场和发展
全栈凯哥6 小时前
Java详解LeetCode 热题 100(23):LeetCode 206. 反转链表(Reverse Linked List)详解
java·算法·leetcode·链表
kingmax542120086 小时前
动态规划十大经典题型状态转移、模版等整理(包括leetcode、洛谷题号)
算法·leetcode·动态规划
GalaxyPokemon7 小时前
LeetCode - 144. 二叉树的前序遍历
算法·leetcode·职场和发展
编程绿豆侠17 小时前
力扣HOT100之多维动态规划:62. 不同路径
算法·leetcode·动态规划
晨曦学习日记18 小时前
力扣题解654:最大二叉树
数据结构·算法·leetcode
一只鱼^_19 小时前
力扣第452场周赛
数据结构·c++·算法·leetcode·贪心算法·动态规划·剪枝
GEEK零零七20 小时前
Leetcode 2123. 使矩阵中的 1 互不相邻的最小操作数
算法·leetcode··二分图·匈牙利算法·hopcroft-karp算法