Skip to content

Commit 57d3767

Browse files
authored
Merge pull request #838 from 0xff-dev/289
Add solution and test-cases for problem 289
2 parents ef44940 + 3080df3 commit 57d3767

File tree

5 files changed

+81
-26
lines changed

5 files changed

+81
-26
lines changed

leetcode/201-300/0289.Game-of-Life/README.md

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,34 @@
11
# [289.Game of Life][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
According to [Wikipedia's article](https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life): "The **Game of Life**, also known simply as **Life**, is a cellular automaton devised by the British mathematician John Horton Conway in 1970."
5+
6+
The board is made up of an m x n grid of cells, where each cell has an initial state: **live** (represented by a `1`) or **dead** (represented by a `0`). Each cell interacts with its [eight neighbors](https://en.wikipedia.org/wiki/Moore_neighborhood) (horizontal, vertical, diagonal) using the following four rules (taken from the above Wikipedia article):
7+
8+
- Any live cell with fewer than two live neighbors dies as if caused by under-population.
9+
- Any live cell with two or three live neighbors lives on to the next generation.
10+
- Any live cell with more than three live neighbors dies, as if by over-population.
11+
- Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.
12+
13+
The next state is created by applying the above rules simultaneously to every cell in the current state, where births and deaths occur simultaneously. Given the current state of the `m x n` grid `board`, return the next state.
714

8-
**Example 1:**
15+
**Example 1:**
16+
17+
![1](./grid1.jpeg)
918

1019
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
20+
Input: board = [[0,1,0],[0,0,1],[1,1,1],[0,0,0]]
21+
Output: [[0,0,0],[1,0,1],[0,1,1],[0,1,0]]
1322
```
1423

15-
## 题意
16-
> ...
24+
**Example 2:**
1725

18-
## 题解
26+
![2](./grid2.jpeg)
1927

20-
### 思路1
21-
> ...
22-
Game of Life
23-
```go
2428
```
25-
29+
Input: board = [[1,1],[1,0]]
30+
Output: [[1,1],[1,1]]
31+
```
2632

2733
## 结语
2834

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,51 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(board [][]int) {
4+
rows, cols := len(board), len(board[0])
5+
6+
var ones func(int, int) int
7+
ones = func(x, y int) int {
8+
count := 0
9+
for i := -1; i <= 1; i++ {
10+
for j := -1; j <= 1; j++ {
11+
if i == 0 && j == 0 {
12+
continue
13+
}
14+
15+
a, b := x+i, y+j
16+
// 1说明之前就是就是活着的,2表示之前是活着的,但是因为周围环境自己死亡
17+
if a >= 0 && a < rows && b >= 0 && b < cols && (board[a][b] == 1 || board[a][b] == 2) {
18+
count++
19+
}
20+
}
21+
}
22+
return count
23+
}
24+
for r := 0; r < rows; r++ {
25+
for c := 0; c < cols; c++ {
26+
count := ones(r, c)
27+
if count < 2 || count > 3 {
28+
if board[r][c] == 1 {
29+
// 之前是活着的,但是被迫挂了
30+
board[r][c] = 2
31+
}
32+
}
33+
34+
// 被迫复活
35+
if count == 3 && board[r][c] == 0 {
36+
board[r][c] = 3
37+
}
38+
}
39+
}
40+
for r := 0; r < rows; r++ {
41+
for c := 0; c < cols; c++ {
42+
if board[r][c] == 2 {
43+
board[r][c] = 0
44+
}
45+
if board[r][c] == 3 {
46+
board[r][c] = 1
47+
}
48+
}
49+
}
50+
return
551
}

leetcode/201-300/0289.Game-of-Life/Solution_test.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,33 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs [][]int
14+
expect [][]int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", [][]int{
17+
{0, 1, 0}, {0, 0, 1}, {1, 1, 1}, {0, 0, 0},
18+
}, [][]int{
19+
{0, 0, 0}, {1, 0, 1}, {0, 1, 1}, {0, 1, 0},
20+
}},
21+
{"TestCase2", [][]int{{1, 1}, {1, 0}}, [][]int{{1, 1}, {1, 1}}},
1922
}
2023

2124
// 开始测试
2225
for i, c := range cases {
2326
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
25-
if !reflect.DeepEqual(got, c.expect) {
27+
Solution(c.inputs)
28+
if !reflect.DeepEqual(c.inputs, c.expect) {
2629
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
30+
c.expect, c.inputs, c.inputs)
2831
}
2932
})
3033
}
3134
}
3235

33-
// 压力测试
36+
// 压力测试
3437
func BenchmarkSolution(b *testing.B) {
3538
}
3639

37-
// 使用案列
40+
// 使用案列
3841
func ExampleSolution() {
3942
}
18.6 KB
Loading
6.83 KB
Loading

0 commit comments

Comments
 (0)