0
点赞
收藏
分享

微信扫一扫

利用 Scapy 库编写 ARP 缓存中毒攻击脚本

数数扁桃 03-29 15:30 阅读 1

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

第一题:螺旋矩阵

. - 力扣(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改为对应的值即可。

举报

相关推荐

ARP欺骗攻击

arp内网攻击

ARP协议以及攻击

渗透技术——ARP攻击

什么是 ARP 攻击?

如何防止Arp攻击

Python库scapy

ARP伪造网关攻击学习

0 条评论