• 【牛客刷题】bfs和dfs (二叉树层序遍历、矩阵最长递增路径、被围绕的区域)


    二叉树层序遍历

       vector<vector<int> > levelOrder(TreeNode* root) {
            // write code here
            vector<int> res;
            vector<vector<int>> result;
            if (root == nullptr) return result;
            queue<TreeNode*> que;
            que.push(root);
    
            while (!que.empty()) {
                int size = que.size();
                for (int i = 0; i < size; i++) {
                    TreeNode* temp = que.front();
                    que.pop();
                    res.push_back(temp->val);
                    if (temp->left) que.push(temp->left);
                    if (temp->right) que.push(temp->right);
                }
                result.push_back(res);
                res.clear();
            }
            return result;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    矩阵最长递增路径

    https://www.nowcoder.com/share/jump/9321389651694076681305
    BFS 通常是为了找到最短路径,求最长路径最好用DFS!
    在这里插入图片描述

    拓扑排序(增加inDegrees矩阵)+ BFS

        int dir[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};
        vector<vector<int>> getInDegrees(vector<vector<int> >& grid) {
            int m = grid.size();
            int n = grid[0].size();
            vector<vector<int>> inDegrees(m, vector<int> (n, 0));
            for (int i = 0; i < m; i++) {
                for (int j = 0; j < n; j++) {
                    for (int k = 0; k < 4; k++) {
                        int newX = i + dir[k][0];
                        int newY = j + dir[k][1];
                        if (newX < 0 || newX >= m || newY < 0 || newY >= n) continue;
                        if (grid[i][j] > grid[newX][newY]) {
                            inDegrees[i][j]++;
                        }
                    }
                }
            }
            return inDegrees;
        }
    
        int solve(vector<vector<int> >& matrix) {
            // write code here
            vector<vector<int>> inDegrees = getInDegrees(matrix);
            int maxLen = 0;
            queue<pair<int, int>> que;
    
            for (int i = 0; i < matrix.size(); i++) {
                for (int j = 0; j < matrix[0].size(); j++) {
                    if (inDegrees[i][j] == 0) {
                        que.push({i, j});
                    }
                }
            }
    
            while (!que.empty()) {
                maxLen++;
                int size = que.size();
                for (int i = 0; i < size; i++) {//需要处理每层信息时这样写,类似于二叉树的层序遍历
                    int x = que.front().first;
                    int y = que.front().second;
                    que.pop();
                    for (int k = 0; k < 4; k++) {//遍历方向
                        int newX = x + dir[k][0];
                        int newY = y + dir[k][1];
                        if (newX < 0 || newX >= matrix.size() || newY < 0 ||
                                newY >= matrix[0].size()) continue;
                        if (matrix[x][y] < matrix[newX][newY]) {//保证是递增序列
                            inDegrees[newX][newY]--;//因为已经确保递增了,所以减少newX和newY的一个入度
                            if (inDegrees[newX][newY] == 0) {//当入度全为0,表示条件全满足,所以可以入队
                                que.push({newX, newY});
                            }
                        }
    
                    }
                }
            }
            return maxLen;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59

    单纯的bfs:

        int dir[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};
        int bfs(vector<vector<int>>& matrix, vector<vector<bool>>& visited, int x,
                 int y) {
            queue<pair<int, int>> que;
            que.push({x, y});
            visited[x][y] = true;
            int maxLen = 0;
    
            while (!que.empty()) {
                maxLen++;
                int size = que.size();
                for (int i = 0; i < size; i++) {//层处理
                    int curX = que.front().first;
                    int curY = que.front().second;
                    que.pop();
                    for (int k = 0; k < 4; k++) {//方向处理
                        int newX = curX + dir[k][0];
                        int newY = curY + dir[k][1];
                        if (newX < 0 || newX >= matrix.size() || newY < 0 ||
                                newY >= matrix[0].size()) continue;
                        if (!visited[newX][newY] && matrix[curX][curY] < matrix[newX][newY]) {
                            que.push({newX, newY});
                            visited[newX][newY] = true;
                        }
                    }
                }
            }
            return maxLen;
        }
    
        int solve(vector<vector<int> >& matrix) {
            // write code here
            vector<vector<bool>> visited(matrix.size(), vector<bool>(matrix[0].size(),
                                         false));
            int maxLen = 0;
    
            for (int i = 0; i < matrix.size(); i++) {
                for (int j = 0; j < matrix[0].size(); j++) {
                    maxLen = max(maxLen, bfs(matrix, visited, i, j));
                }
            }
            return maxLen;
        }
        
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45

    dfs+记忆化搜索(memo):

        int dir[4][2] = {0, 1, 1, 0, 0, -1, -1, 0};
        int dfs(vector<vector<int>>& matrix, vector<vector<int>>& memo, int x, int y) {
            if (memo[x][y] != -1) return memo[x][y];//递归终止条件
            int maxLen = 1;
    
            for (int i = 0; i < 4; i++) {//遍历方向
                int newX = x + dir[i][0];
                int newY = y + dir[i][1];
                if (newX < 0 || newX >= matrix.size() || newY < 0 || newY >= matrix[0].size() ||
                        matrix[newX][newY] <= matrix[x][y]) continue;//满足条件才递归
                maxLen = max(maxLen, 1 + dfs(matrix, memo, newX, newY));//表示从(x, y)到(newX, newY)这一步
            }
            memo[x][y] = maxLen;
            return memo[x][y];
        }
    
        int solve(vector<vector<int> >& matrix) {
            vector<vector<int>> memo(matrix.size(), vector<int>(matrix[0].size(), -1));
            int maxLen = 0;
    
            for (int i = 0; i < matrix.size(); i++) {
                for (int j = 0; j < matrix[0].size(); j++) {
                    maxLen = max(maxLen, dfs(matrix, memo, i, j));
                }
            }
            return maxLen;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    被围绕的区域

    https://www.nowcoder.com/share/jump/9321389651694087623428
    在这里插入图片描述
    dfs:

        int dir[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};
        void dfs(vector<vector<char>>& matrix, int x,
                 int y) {
            int m = matrix.size(), n = matrix[0].size();
            matrix[x][y] = 'E';
    
            for (int i = 0; i < 4; i++) {
                int newX = x + dir[i][0];
                int newY = y + dir[i][1];
                if (newX < 0 || newX >= m || newY < 0 || newY >= n ||
                        matrix[newX][newY] != 'O')
                    continue;//(newX,newY)中超出边界的、不是O的不用管
                dfs(matrix, newX, newY);
            }
        }
    
        vector<vector<char> > surroundedArea(vector<vector<char> >& board) {
            // write code here
            int m = board.size(), n = board[0].size();
            //将连接边界的O全部替换
            for (int i = 0; i < m; i++) {
                if (board[i][0] == 'O') dfs(board, i, 0);
                if (board[i][n - 1] == 'O') dfs(board, i, n - 1);
            }
            for (int j = 0; j < n; j++) {
                if (board[0][j] == 'O') dfs(board, 0, j);
                if (board[m - 1][j] == 'O') dfs(board, m - 1, j);
            }
            //又替换回来
            for (int i = 0; i < m; i++) {
                for (int j = 0; j < n; j++) {
                    if (board[i][j] == 'E') board[i][j] = 'O';
                    else board[i][j] = 'X';
                }
            }
            return board;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    bfs:

        int dir[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};
    
        void bfs(vector<vector<char>>& matrix, int x, int y) {
            int m = matrix.size(), n = matrix[0].size();
            queue<pair<int, int>> que;
            que.push({x, y});
            while (!que.empty()) {
                int curX = que.front().first;
                int curY = que.front().second;
                que.pop();
                matrix[curX][curY] = 'E';
                for (int i = 0; i < 4; i++) {
                    int newX = curX + dir[i][0];
                    int newY = curY + dir[i][1];
                    if (newX < 0 || newX >= m || newY < 0 || newY >= n ||
                            matrix[newX][newY] != 'O')
                        continue;//(newX,newY)中超出边界的、不是O的不用管
                    que.push({newX, newY});
                }
            }
        }
    
        vector<vector<char> > surroundedArea(vector<vector<char> >& board) {
            // write code here
            int m = board.size(), n = board[0].size();
            for (int i = 0; i < m; i++) {
                if (board[i][0] == 'O') bfs(board, i, 0);
                if (board[i][n - 1] == 'O') bfs(board, i, n - 1);
            }
            for (int j = 0; j < n; j++) {
                if (board[0][j] == 'O') bfs(board, 0, j);
                if (board[m - 1][j] == 'O') bfs(board, m - 1, j);
            }
    
            for (int i = 0; i < m; i++) {
                for (int j = 0; j < n; j++) {
                    if (board[i][j] == 'E') board[i][j] = 'O';
                    else board[i][j] = 'X';
                }
            }
            return board;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
  • 相关阅读:
    vs studio 安装opencv 环境
    Android如何连接metamask并签名
    8.自定义组件布局和详解Context上下文
    云原生nacos之服务发现SDK
    Qt 之悬浮球菜单
    代码优化~隔离接口实现类
    Linux mkdir命令:创建目录(文件夹)
    剑指offer——JZ82 二叉树中和为某一值的路径(一) 解题思路与具体代码【C++】
    数据流的中位数 [双堆&桶的使用]
    “百度杯”CTF比赛 九月场,Web:SQL
  • 原文地址:https://blog.csdn.net/weixin_43785314/article/details/132734276