Leetcode 79 Word search

题意:给定一个m*n的矩阵,求在矩阵中是否能搜索到这个单词

https://leetcode.com/problems/word-search/description/

Input: board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"

Output: true

解答:遇到第一个匹配的开头(比如例子中的A),开始搜索,用dfs来搜索四个方向,并用vis数组确保已经访问过的节点不会被访问,用一个变量来记录我当前搜索到哪一个位置了

cpp 复制代码
class Solution {
public:
    bool ret;
    bool exist(vector<vector<char>>& board, string word) {
        if(!word.size()) {
            return true;
        }
        int m = board.size();
        int n = board[0].size();
        for(int i = 0; i < m; i++) {
            for(int j = 0; j < n; j++) {
                if(board[i][j] == word[0]) {
                    vector<vector<int>> vis(m, vector<int>(n,0));
                    vis[i][j] = 1;
                    dfs(board, 1, i, j, vis, word);
                    if(ret) {
                        return true;
                        }
                    }
                }
            }
            return false;
        }

    void dfs(vector<vector<char>>& board, int st, int x, int y, vector<vector<int>>& vis, string word) {
        int m = board.size();
        int n = board[0].size();
        if(st == word.size()) {
            ret = true;
            return;
        }
        
        int dk[] = {-1, 0, 1, 0, -1};
        for(int i = 0; i < 4; i++) {
            int dx = x + dk[i];
            int dy = y + dk[i+1];
            if(dx >= 0 && dx < m && dy >= 0 && dy < n && !vis[dx][dy] && board[dx][dy] == word[st]) {
                vis[dx][dy] = 1;
                dfs(board, st+1, dx, dy, vis, word);
                vis[dx][dy] = 0;
            }
        }
    }
};

question:为什么下面的代码过不了?以及有哪些不好的地方?

cpp 复制代码
class Solution {
public:
    bool ret;
    bool exist(vector<vector<char>>& board, string word) {
        if(!word.size()) {
            return true;
        }
        int m = board.size();
        int n = board[0].size();
        for(int i = 0; i < m; i++) {
            for(int j = 0; j < n; j++) {
                if(board[i][j] == word[0]) {
                    vector<vector<int>> vis(m, vector<int>(n,0));
                    vis[i][j] = 1;
                    dfs(board, 1, i, j, vis, word);
                    if(ret) {
                        return true;
                        }
                    }
                }
            }
            return false;
        }

    void dfs(vector<vector<char>>& board, int st, int x, int y, vector<vector<int>> vis, string word) {
        int m = board.size();
        int n = board[0].size();
        if(st == word.size()) {
            ret = true;
            return;
        }
        
        int dk[] = {-1, 0, 1, 0, -1};
        for(int i = 0; i < 4; i++) {
            int dx = x + dk[i];
            int dy = y + dk[i+1];
            if(dx >= 0 && dx < m && dy >= 0 && dy < n && !vis[dx][dy] && board[dx][dy] == word[st]) {
                vis[dx][dy] = 1;
                dfs(board, st+1, dx, dy, vis, word);
                vis[dx][dy] = 0;
            }
        }
    }
};

为什么过不了?因为vis数组产生了copy,这个开销是MN的,你的答案的算法复杂度会变成M 2N 2

并且这个ret完全没必要,你完全可以通过dfs的返回值消掉这个ret

而且vis数组其实可以写在里面的

question:为什么这个会有问题?因为board[dx][dy] == word[st+1] 会有越界问题的,要想输入dfs(board, 0, i, j, vis, word,我必须每次进入的时候判定,而不是说每一次for循环中去判定

cpp 复制代码
class Solution {
public:
    bool exist(vector<vector<char>>& board, string word) {
        if(!word.size()) {
            return true;
        }
        int m = board.size();
        int n = board[0].size();
        for(int i = 0; i < m; i++) {
            for(int j = 0; j < n; j++) {
                if(board[i][j] == word[0]) {
                    vector<vector<int>> vis(m, vector<int>(n,0));
                    if(dfs(board, 0, i, j, vis, word)) {
                        return true;
                        }
                    }
                    
                }
            }
            return false;
        }

    bool dfs(vector<vector<char>>& board, int st, int x, int y, vector<vector<int>>& vis, string word) {
        int m = board.size();
        int n = board[0].size();
        vis[x][y] = 1;
        if(st == word.size()) {
            return true;
        }
        int dk[] = {-1, 0, 1, 0, -1};
        for(int i = 0; i < 4; i++) {
            int dx = x + dk[i];
            int dy = y + dk[i+1];
            if(dx >= 0 && dx < m && dy >= 0 && dy < n && !vis[dx][dy] && board[dx][dy] == word[st+1]) {
                if(dfs(board, st+1, dx, dy, vis, word)) {
                    return true;
                }
            }
        }
        vis[x][y] = 0;
        return false;
    }


};

最为简洁的写法,写的时候注意,先判断true,再来false

cpp 复制代码
class Solution {
public:
    bool exist(vector<vector<char>>& board, string word) {
        if(!word.size()) {
            return true;
        }
        int m = board.size();
        int n = board[0].size();
        for(int i = 0; i < m; i++) {
            for(int j = 0; j < n; j++) {
                if(board[i][j] == word[0]) {
                    vector<vector<int>> vis(m, vector<int>(n,0));
                    if(dfs(board, 0, i, j, vis, word)) {
                        return true;
                        }
                    }
                    
                }
            }
            return false;
        }

    bool dfs(vector<vector<char>>& board, int st, int x, int y, vector<vector<int>>& vis, string word) {
        int m = board.size();
        int n = board[0].size();
        if(st == word.size()) {
            return true;
        }
        if(x < 0 || x >= m || y < 0 || y >= n) {
            return false;
        }
        if(vis[x][y] || board[x][y] != word[st]) {
            return false;
        }
        vis[x][y] = 1;
        int dk[] = {-1, 0, 1, 0, -1};
        for(int i = 0; i < 4; i++) {
            int dx = x + dk[i];
            int dy = y + dk[i+1];
                if(dfs(board, st+1, dx, dy, vis, word)) {
                    return true;
                }
            }
        vis[x][y] = 0;
        return false;
        }

};

算法复杂度(MN 3^L)

MN是因为第一个大循环,第二个dfs相当于搜索长度为L的字符串,有3个方向可以搜索(去除了回退哪一个)

相关推荐
戊子仲秋2 小时前
【LeetCode】每日一题 2024_11_21 矩阵中的蛇(模拟)
算法·leetcode·矩阵
wang_changyue4 小时前
CSP-X2024解题报告(T3)
数据结构·算法·leetcode
Tisfy8 小时前
LeetCode 3240.最少翻转次数使二进制矩阵回文 II:分类讨论
算法·leetcode·矩阵·题解·回文·分类讨论
Tisfy9 小时前
LeetCode 3244.新增道路查询后的最短距离 II:贪心(跃迁合并)-9行py(O(n))
算法·leetcode·题解·贪心·思维
DdddJMs__1359 小时前
C语言 | Leetcode C语言题解之第564题寻找最近的回文数
c语言·leetcode·题解
xxxmmc12 小时前
Leetcode 3355 Zero Array Transformation
算法·leetcode·差分
程序garbage12 小时前
搜索插入位置-力扣
数据结构·算法·leetcode
来知晓12 小时前
Python世界:力扣题解875,珂珂爱吃香蕉,中等
linux·python·leetcode
xiaoshiguang312 小时前
LeetCode 209 长度最小的子数组(滑动窗口)
java·算法·leetcode
m0_6759882312 小时前
Leetcode3244:新增道路查询后的最短距离 II(C++)
c++·算法·leetcode