LeetCode_Array_79. Word Search (C++)

目录

1,题目描述

2, 思路

我的思路:

大神的思路:

3, 代码【C++】

4,测试结果


1,题目描述

Given a 2D board and a word, find if the word exists in the grid.

The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once.

Example:

board =
[
  ['A','B','C','E'],
  ['S','F','C','S'],
  ['A','D','E','E']
]

Given word = "ABCCED", return true.
Given word = "SEE", return true.
Given word = "ABCB", return false.

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/word-search
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

相当于在这个矩阵中寻找这串字符,字符串中相邻字符在矩阵中必须也是相邻的,且元素不能重复使用(有种贪吃蛇的感觉。。。)

2, 思路

我的思路:

  • 遍历矩阵的每个元素,每个元素均用一遍dfs,一旦成功搜索到字符串就令全局变量为true,就立刻返回true;
  • dfs的实现:

利用depth判断递归的出口;

由于不能重复使用元素,故将访问过的元素设置为特殊字符,若当前路径不是目标路径,则逐个回溯恢复原始数据;

每到达一个点就判断四个方向是否有路,并逐个递归判断;

 

大神的思路:

其实大神的思路进本上是差不多的,只不过有些部分的条件划分比我的更高效(准确说应该是剪枝吧);

  • row + 1 < maxRow && board[row + 1][col] == word[index + 1]:判断该方向是否有路而且此方向与下一个字符符合,再进行递归;
  • 利用返回值来接受判断结果,而我用全局变量(感觉好像影响不大)

 

3, 代码【C++】

我的代码:

class Solution {
public:
    bool tag = false;
    bool exist(vector<vector<char>>& board, string word) {
        for(int i = 0 ; i < board.size(); i++){
            for(int j = 0 ; j < board[0].size() ; j++){
                dfs(board, 0, i, j, word);
                if(tag == true) return true;
            }
        }
        return false;
    }

    void dfs(vector<vector<char>>& board, int depth, int x, int y, string word){
        if(depth >= word.size()) {
            tag = true;
            return;
        }
        else if(board[x][y] == word[depth]) {
            depth += 1;
            if(depth >= word.size()) {
                tag = true;
                return;
            }
            char temp = board[x][y];
            board[x][y] = '#';  //表示遍历过
        
            if(tag == false && x - 1 >= 0){ 
                dfs(board, depth, x-1, y, word);//上
            }
            if(tag == false && y + 1 <=board[0].size()-1){   
                dfs(board, depth, x, y+1, word);//右
            }
            if(tag == false && x + 1 <= board.size()-1){
                dfs(board, depth, x+1, y, word);//下
            }
            if(tag == false && y - 1 >= 0){
                dfs(board, depth, x, y-1, word);//左
            }
            board[x][y] = temp;
            return;
        }
        else return;
    }
};

大神的代码:

class Solution {
public:
    bool dfs(vector<vector<char>> & board, int row, int col, string & word, int index) {
        int maxRow = board.size();
        int maxCol = board[0].size();

        if(index == word.length() - 1) {
            return true;
        }

        //board[row][col] = tolower(board[row][col]);
        board[row][col] = '\n';
        bool rc = false;
        if(row + 1 < maxRow && board[row + 1][col] == word[index + 1]) {
            rc = dfs(board, row + 1, col, word, index + 1);
            if(rc) return true;
        }

        if(row - 1 >= 0 && board[row - 1][col] == word[index + 1]) {
            rc = dfs(board, row - 1, col, word, index + 1);
            if(rc) return true;
        }
        
        if(col + 1 < maxCol && board[row][col + 1] == word[index + 1]) {
            rc = dfs(board, row, col + 1, word, index + 1);
            if(rc) return true;
        }
        
        if(col - 1 >= 0 && board[row][col - 1] == word[index + 1]) {
            rc = dfs(board, row, col - 1, word, index + 1);
            if(rc) return true;
        }
        board[row][col] = word[index];

        return false;
    }

    bool exist(vector<vector<char>>& board, string word) {
        int row = board.size();
        if(!row) return false;
        int col = board[0].size();

        bool res = false;
        for(int i = 0; i < row; i++) {
            for(int j = 0; j < col; j++) {
                if(board[i][j] == word[0]) {
                    res = dfs(board, i, j, word, 0);
                    if(res == true) return true;
                }
            }
        }

        return false;
    }
};

4,测试结果

充分显示了,适当的判断条件划分,可以显著提升代码的效率!!!

我的代码:(LOW爆了。。。)

大神的代码:(差距不是一点点)

发布了45 篇原创文章 · 获赞 5 · 访问量 2200

猜你喜欢

转载自blog.csdn.net/qq_41528502/article/details/104175942