Skip to content

Commit 054e426

Browse files
committed
New Problem Solution - "Game of Life"
1 parent 8bc818a commit 054e426

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ LeetCode
148148
|295|[Find Median from Data Stream](https://leetcode.com/problems/find-median-from-data-stream/) | [C++](./algorithms/cpp/findMedianFromDataStream/FindMedianFromDataStream.cpp)|Hard|
149149
|292|[Nim Game](https://leetcode.com/problems/nim-game/) | [C++](./algorithms/cpp/nimGame/nimGame.cpp)|Easy|
150150
|290|[Word Pattern](https://leetcode.com/problems/word-pattern/) | [C++](./algorithms/cpp/wordPattern/WordPattern.cpp)|Easy|
151+
|289|[Game of Life](https://leetcode.com/problems/game-of-life/) | [C++](./algorithms/cpp/gameOfLife/GameOfLife.cpp)|Medium|
151152
|287|[Find the Duplicate Number](https://leetcode.com/problems/find-the-duplicate-number/) | [C++](./algorithms/cpp/findTheDuplicateNumber/findTheDuplicateNumber.cpp), [Python](./algorithms/python/FindTheDuplicateNumber/findDuplicate.py)|Hard|
152153
|285|[Inorder Successor in BST](https://leetcode.com/problems/inorder-successor-in-bst/) ♥ | [Java](./algorithms/java/src/inorderSuccessorInBST/inorderSuccessorInBST.java)|Medium|
153154
|284|[Peeking Iterator](https://leetcode.com/problems/peeking-iterator/) | [C++](./algorithms/cpp/peekingIterator/PeekingIterator.cpp)|Medium|
@@ -408,3 +409,4 @@ LeetCode
408409
|---| ----- | -------- | ---------- |
409410
|1|[Search in a big sorted array](http://www.lintcode.com/en/problem/search-in-a-big-sorted-array/)|[Java](./algorithms/java/src/searchInABigSortedArray/searchInABigSortedArray.java)|Medium|
410411
|2|[Search Range in Binary Search Tree](http://www.lintcode.com/en/problem/search-range-in-binary-search-tree/) | [Java](./algorithms/java/src/searchRangeInBinarySearchTree/searchRangeInBinarySearchTree.java)|Medium|
412+
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// Source : https://leetcode.com/problems/game-of-life/
2+
// Author : Hao Chen
3+
// Date : 2019-03-20
4+
5+
/*****************************************************************************************************
6+
*
7+
* According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a cellular
8+
* automaton devised by the British mathematician John Horton Conway in 1970."
9+
*
10+
* Given a board with m by n cells, each cell has an initial state live (1) or dead (0). Each cell
11+
* interacts with its eight neighbors (horizontal, vertical, diagonal) using the following four rules
12+
* (taken from the above Wikipedia article):
13+
*
14+
* Any live cell with fewer than two live neighbors dies, as if caused by under-population.
15+
* Any live cell with two or three live neighbors lives on to the next generation.
16+
* Any live cell with more than three live neighbors dies, as if by over-population..
17+
* Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.
18+
*
19+
* Write a function to compute the next state (after one update) of the board given its current state.
20+
* The next state is created by applying the above rules simultaneously to every cell in the current
21+
* state, where births and deaths occur simultaneously.
22+
*
23+
* Example:
24+
*
25+
* Input:
26+
* [
27+
* [0,1,0],
28+
* [0,0,1],
29+
* [1,1,1],
30+
* [0,0,0]
31+
* ]
32+
* Output:
33+
* [
34+
* [0,0,0],
35+
* [1,0,1],
36+
* [0,1,1],
37+
* [0,1,0]
38+
* ]
39+
*
40+
* Follow up:
41+
*
42+
* Could you solve it in-place? Remember that the board needs to be updated at the same time:
43+
* You cannot update some cells first and then use their updated values to update other cells.
44+
* In this question, we represent the board using a 2D array. In principle, the board is
45+
* infinite, which would cause problems when the active area encroaches the border of the array. How
46+
* would you address these problems?
47+
*
48+
******************************************************************************************************/
49+
50+
51+
class Solution {
52+
public:
53+
// the problem here is we need store two states in one cell,
54+
// one is the original state, another is the new state
55+
// So, we could store the state into the bit.
56+
// - Old State: the first bit from the right
57+
// - New State: the second bit from the right
58+
void liveCheck(vector<vector<int>>& board, int r, int c) {
59+
int cnt = 0;
60+
for (int i=r-1; i<=r+1; i++) {
61+
if (i < 0 || i>=board.size()) continue;
62+
for (int j=c-1; j<=c+1; j++) {
63+
if (j<0 || j>=board[0].size() || (i==r && j==c)) continue;
64+
if ( board[i][j] & 1 ) cnt++;
65+
}
66+
}
67+
68+
//live -> die
69+
//if (board[r][c]==1 && (cnt < 2 || cnt > 3)) board[r][c] = 1;
70+
71+
//live -> live
72+
if ( board[r][c] == 1 && (cnt == 2 || cnt == 3) ) board[r][c] = 3;
73+
74+
//die -> live
75+
if ( board[r][c] == 0 && cnt == 3 ) board[r][c] = 2;
76+
77+
}
78+
79+
void gameOfLife(vector<vector<int>>& board) {
80+
for (int i=0; i<board.size(); i++) {
81+
for (int j=0; j<board[0].size(); j++) {
82+
liveCheck(board, i, j);
83+
}
84+
}
85+
86+
for (int i=0; i<board.size(); i++) {
87+
for (int j=0; j<board[0].size(); j++) {
88+
board[i][j] >>= 1;
89+
}
90+
91+
}
92+
93+
}
94+
};

0 commit comments

Comments
 (0)