aboutsummaryrefslogtreecommitdiff
path: root/game_of_life.cpp
blob: 86c9d5307a2efe74f5fd71c822db29cbbd005817 (plain)
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
#include <bits/stdc++.h>
using namespace std;

class Solution {
public:
  void gameOfLife(vector<vector<int>> &board) {
    vector<int> waysX = {0, 0, 1, 1, 1, -1, -1, -1};
    vector<int> waysY = {1, -1, 1, -1, 0, 0, 1, -1};
    for (int row = 0; row < board.size(); row++) {
      for (int col = 0; col < board[0].size(); col++) {
        int live = 0;
        for (int i = 0; i < 8; i++) {
          int currX = row + waysX[i], currY = col + waysY[i];
          if (isNeighbor(currX, currY, board) && abs(board[currX][currY]) == 1)
            live++;
        }
        if (board[row][col] == 1 && (live < 2 || live > 3))
          board[row][col] = -1;
        if (board[row][col] == 0 && live == 3)
          board[row][col] = 2;
      }
    }
    for (int row = 0; row < board.size(); row++)
      for (int col = 0; col < board[0].size(); col++)
        if (board[row][col] >= 1)
          board[row][col] = 1;
        else
          board[row][col] = 0;
  }

private:
  bool isNeighbor(int x, int y, vector<vector<int>> &board) {
    return (x >= 0 && x < board.size() && y >= 0 && y < board[0].size());
  }
};

int main() {}