• 【Leetcode】79. Word Search


    思路:

       遍历矩阵,结合dfs解即可。

       

    #include <iostream>
    #include <vector>
    using namespace std;
    
    class Solution {
    public:
    
        Solution() {}
    
        bool exist(vector<vector<char>>& board, string word)
        {
            //初始化都没有被访问过
            this->rowCount = board.size();
            if (rowCount == 0) {
                return false;
            }
            this->colCount = board[0].size();
            for (int i = 0; i < rowCount; i++) {
                vector<int> v(colCount, 0);
                visit.push_back(v);
            }
    
    
            this->word = word;
            this->board = board;
    
            for (int i = 0; i < rowCount; i++){
                for (int j = 0; j< colCount; j++) {
                    if (board[i][j] == word[0] && dfs(i, j, 0)) {
                        return true;
                    }
                }
            }
    
            return false;
        }
    
        /**
         *@param row 当前字符所在行
         *@param col 当前字符所在列
         *@param index word当前被扫描的位置
         */
        bool dfs(int row, int col, int index)
        {
            if (index == word.size() - 1) {
                return true;
            }
            //当前位置标志为被访问
            visit[row][col] = 1;
            //up
            if (row - 1 >= 0 && !visit[row - 1][col] && board[row - 1][col] == word[index + 1] ) {
                if (dfs(row - 1, col, index + 1)) {
                    return true;
                }
            }
            //down
            if (row + 1 < this->rowCount && !visit[row + 1][col] && board[row + 1][col] == word[index + 1]) {
                if (dfs(row + 1, col, index + 1)) {
                    return true;
                }
            }
            //left
            if (col - 1 >= 0 && !visit[row][col - 1] && board[row][col - 1] == word[index + 1]) {
                if (dfs(row, col - 1, index + 1)) {
                    return true;
                }
            }
            //right
            if (col + 1 < this->colCount && !visit[row][col + 1] && board[row][col + 1] == word[index + 1]) {
                if (dfs(row, col + 1, index + 1)) {
                    return true;
                }
            }
            //不满足,置为0
            visit[row][col] = 0;
            return false;
        }
    
    private:
        vector<vector<char>> board;
        string word;
        vector<vector<int>> visit;
        int rowCount;
        int colCount;
    };
    
    int main(int argc, char *argv[])
    {
        vector<vector<char>> board =  {
                {'a','b'}};
        Solution s;
        string word = "ba";
        bool ret = s.exist(board, word);
        cout<<ret<<endl;
        return 0;
    }
    

      

  • 相关阅读:
    opencv 遍历Mat对象中数据方法-转
    JVM OutOfMemoryError 分析
    hibernate validation HV000030: No validator could be found for constraint
    通过aop实现rpc统一参数校验&异常捕捉
    java8 lambda groupingby 分组保持原来顺序
    递归判断素组是否有序
    dubbo 直连
    Linux 删除openjdk
    telnet命令调用远程dubbo 接口
    git submodule ssh key
  • 原文地址:https://www.cnblogs.com/AndrewGhost/p/6926040.html
Copyright © 2020-2023  润新知