class Solution {
public:
int life(vector<vector<int>>& board, int x, int y, int r, int c) {
int count = 0; // Initialize count to 0
if (x < 0 || x >= r || y < 0 || y >= c) return 0;
if (x + 1 < r && board[x + 1][y] == 1) count++;
if (x + 1 < r && y - 1 >= 0 && board[x + 1][y - 1] == 1) count++;
if (x + 1 < r && y + 1 < c && board[x + 1][y + 1] == 1) count++;
if (x - 1 >= 0 && board[x - 1][y] == 1) count++;
if (x - 1 >= 0 && y + 1 < c && board[x - 1][y + 1] == 1) count++;
if (x - 1 >= 0 && y - 1 >= 0 && board[x - 1][y - 1] == 1) count++;
if (y + 1 < c && board[x][y + 1] == 1) count++;
if (y - 1 >= 0 && board[x][y - 1] == 1) count++;
return count;
}
void gameOfLife(vector<vector<int>>& board) {
int r = board.size(), c = board[0].size();
vector<vector<int>> dum = board;
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
int nbr = life(board, i, j, r, c);
if (board[i][j] == 1) { // Cell is currently alive
if (nbr < 2 || nbr > 3) dum[i][j] = 0; // Cell dies
else dum[i][j] = 1; // Cell survives
} else { // Cell is currently dead
if (nbr == 3) dum[i][j] = 1; // Cell becomes alive
}
}
}
board = dum; // Update the original board with the new state
}
};