classSolution { public: voidgameOfLife(vector<vector<int>>& board){ int m = board.size(), n = board[0].size(); int dx[8] = {-1, -1, -1, 0, 0, 1, 1, 1}; int dy[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { int live = 0; for (int k = 0; k < 8; k++) { int x = i + dx[k], y = j + dy[k]; if (x >= 0 && x < m && y >= 0 && y < n && board[x][y] & 1) live++; } if (board[i][j] & 1) { if (live == 2 || live == 3) board[i][j] |= 2; } else { if (live == 3) board[i][j] |= 2; } } }
for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) board[i][j] >>= 1; } };
2. In this question, we represent the board using a 2D array. In principle, the board is infinite, which would cause problems when the active area encroaches the border of the array. How would you address these problems?