算法——矩阵

矩阵的遍历,尤其是需要获得每个格子的周围信息时,多种条件判断显得尤其麻烦。

第一题:螺旋矩阵

. - 力扣(LeetCode)

引用大佬代码。对于每个圆圈,提前设定上下左右边界,并且每次处理完一条边后更改边界。

复制代码
class Solution {
public:
    vector<int> spiralOrder(vector<vector<int>>& matrix) {
        vector <int> ans;
        if(matrix.empty()) return ans; //若数组为空,直接返回答案
        int u = 0; //赋值上下左右边界
        int d = matrix.size() - 1;
        int l = 0;
        int r = matrix[0].size() - 1;
        while(true)
        {
            for(int i = l; i <= r; ++i) ans.push_back(matrix[u][i]); //向右移动直到最右
            if(++ u > d) break; //重新设定上边界,若上边界大于下边界,则遍历遍历完成,下同
            for(int i = u; i <= d; ++i) ans.push_back(matrix[i][r]); //向下
            if(-- r < l) break; //重新设定有边界
            for(int i = r; i >= l; --i) ans.push_back(matrix[d][i]); //向左
            if(-- d < u) break; //重新设定下边界
            for(int i = d; i >= u; --i) ans.push_back(matrix[i][l]); //向上
            if(++ l > r) break; //重新设定左边界
        }
        return ans;
    }
};

第二题:生命游戏

. - 力扣(LeetCode)

复制代码
// class Solution {
// public:
//     void gameOfLife(vector<vector<int>>& board) {
//         if (board.size() == 0 || board[0].size() == 0)return;
//         vector<vector<int>> arr(board);
//         for (int i = 0; i < arr.size(); i++)
//         {
//             for (int j = 0; j < arr[0].size(); j++)
//             {
//                 int temp = getalive(board, i, j);
//                 if (board[i][j])
//                 {
//                     if (temp < 2 || temp>3)
//                         arr[i][j] = 0;
//                 }
//                 else if (board[i][j] == 0 && temp == 3)
//                     arr[i][j] = 1;
//             }
//         }
//         board = arr;
//     }

//     int getalive(vector<vector<int>>& board, int row, int col)
//     {
//         int res = 0;
//         int up, down, left, right;
//         if (row == 0 || row == board.size() - 1 || col == 0 || col == board[0].size() - 1)
//         {
//             //bianjie
//             if (row == 0)up = 0;
//             else up = row - 1;
//             if (col == 0)left = 0;
//             else left = col - 1;
//             if (row == board.size() - 1)down = row;
//             else down = row + 1;
//             if (col == board[0].size()-1)right = col;
//             else right = col+1;
//         }
//         else
//         {
//             left = col - 1;
//             right = col + 1;
//             up = row - 1;
//             down = row + 1;
//         }
//         for (int i = up; i <= down; i++)
//         {
//             for (int j = left; j <= right; j++)
//                 if (board[i][j] == 1)res++;
//         }
//         if (board[row][col] == 1)res--;
//         return res;
//     }
// };



class Solution {
public:
    void gameOfLife(vector<vector<int>>& board) {
        int neighbors[3] = {0, 1, -1};

        int rows = board.size();
        int cols = board[0].size();

        // 创建复制数组 copyBoard
        vector<vector<int> >copyBoard(rows, vector<int>(cols, 0));

        // 从原数组复制一份到 copyBoard 中
        for (int row = 0; row < rows; row++) {
            for (int col = 0; col < cols; col++) {
                copyBoard[row][col] = board[row][col];
            }
        }

        // 遍历面板每一个格子里的细胞
        for (int row = 0; row < rows; row++) {
            for (int col = 0; col < cols; col++) {

                // 对于每一个细胞统计其八个相邻位置里的活细胞数量
                int liveNeighbors = 0;

                for (int i = 0; i < 3; i++) {
                    for (int j = 0; j < 3; j++) {

                        //二者不同时为0,即不为当前中心节点
                        if (!(neighbors[i] == 0 && neighbors[j] == 0)) {
                            int r = (row + neighbors[i]);
                            int c = (col + neighbors[j]);

                            // 查看相邻的细胞是否是活细胞
                            if ((r < rows && r >= 0) && (c < cols && c >= 0) && (copyBoard[r][c] == 1)) {
                                liveNeighbors += 1;
                            }
                        }
                    }
                }

                // 规则 1 或规则 3      
                if ((copyBoard[row][col] == 1) && (liveNeighbors < 2 || liveNeighbors > 3)) {
                    board[row][col] = 0;
                }
                // 规则 4
                if (copyBoard[row][col] == 0 && liveNeighbors == 3) {
                    board[row][col] = 1;
                }
            }
        }
    }
};

注释部分为自己写的代码,使用前一种方式,使用边界,对于边界点和非边界点使用不同的边界处理逻辑。但是显然此方法在此题较为繁琐,不过也不失为一种解决办法。对于矩阵的题均可使用上述第一种方法,对于不同的点使用不同的边界。

上述第二种方法使用了一个数组0,1,-1;具体可看代码。很巧妙。代码整体编写难度也较小,容易记住。

上述第二题还有一种方法,即使用不同的数字记录状态。如果从死变为或使用2,从活变为死使用1,在判断时如果周围节点等于1或-1即满足+1条件。最后遍历一遍矩阵把2,-1改为对应的值即可。

相关推荐
BothSavage3 小时前
Trae远程开发中DeepSeek自定义模型4054错误的排查与修复
算法
小林ixn3 小时前
从暴力到KMP:一道题彻底搞懂字符串匹配的前世今生
算法
烬羽4 小时前
字符串算法入门:从反转字符串到回文判断,面试不再慌
算法·面试
先吃饱再说20 小时前
判断回文字符串,从一行代码到双指针优化
算法
黄敬峰1 天前
深入理解算法核心:从递归思想、数组扁平化到快速排序
算法
得物技术1 天前
从狂野代码到按目标生产:得物推荐 AI Harness 的工程化实践|AICon 演讲整理
人工智能·算法·架构
AI小老六1 天前
SkillOpt 架构拆解:把 Skill 文本当参数,用执行轨迹训练 Agent
后端·算法·ai编程
胡萝卜术1 天前
从“分数打架”到“排名投票”:为什么你的ChatBI必须用RRF?
算法·设计模式·面试
Asize1 天前
初识DFS 与 BFS:递归、队列与图遍历
算法