-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameofLife.cc
More file actions
52 lines (45 loc) · 1.16 KB
/
Copy pathGameofLife.cc
File metadata and controls
52 lines (45 loc) · 1.16 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <sstream>
#include <iostream>
#include <vector>
using namespace std;
class Solution {
int getLiveNum(vector<vector<int> >& board, int x, int y) {
int c = 0;
for (int i = x - 1; i <= x + 1; i++) {
for (int j = y - 1; j <= y + 1; j++) {
if (i < 0 || j < 0 || i > board.size() - 1 || j > board[0].size() - 1 ||
(i == x && j == y))
continue;
if (board[i][j] % 10 == 1) c++;
}
}
return c;
}
public:
void gameOfLife(vector<vector<int> >& board) {
// check input
if ( board.size() == 0) return;
if ( board[0].size() == 0) return;
int m = board.size();
int n = board[0].size();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
int x = getLiveNum(board, i, j);
if (board[i][j] == 0) {
if (x == 3) board[i][j] += 10;
} else {
if (x == 2 || x == 3) board[i][j] += 10;
}
}
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
board[i][j] /= 10;
}
}
}
};
int main(int argc, char const* argv[]) { return 0; }