Skip to content

Add solution and test-cases for problem 289 #838

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 20 additions & 14 deletions leetcode/201-300/0289.Game-of-Life/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,34 @@
# [289.Game of Life][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

## Description
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."

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):

- Any live cell with fewer than two live neighbors dies as if caused by under-population.
- Any live cell with two or three live neighbors lives on to the next generation.
- Any live cell with more than three live neighbors dies, as if by over-population.
- Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.

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.

**Example 1:**
**Example 1:**

![1](./grid1.jpeg)

```
Input: a = "11", b = "1"
Output: "100"
Input: board = [[0,1,0],[0,0,1],[1,1,1],[0,0,0]]
Output: [[0,0,0],[1,0,1],[0,1,1],[0,1,0]]
```

## 题意
> ...
**Example 2:**

## 题解
![2](./grid2.jpeg)

### 思路1
> ...
Game of Life
```go
```

Input: board = [[1,1],[1,0]]
Output: [[1,1],[1,1]]
```

## 结语

Expand Down
50 changes: 48 additions & 2 deletions leetcode/201-300/0289.Game-of-Life/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,51 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(board [][]int) {
rows, cols := len(board), len(board[0])

var ones func(int, int) int
ones = func(x, y int) int {
count := 0
for i := -1; i <= 1; i++ {
for j := -1; j <= 1; j++ {
if i == 0 && j == 0 {
continue
}

a, b := x+i, y+j
// 1说明之前就是就是活着的,2表示之前是活着的,但是因为周围环境自己死亡
if a >= 0 && a < rows && b >= 0 && b < cols && (board[a][b] == 1 || board[a][b] == 2) {
count++
}
}
}
return count
}
for r := 0; r < rows; r++ {
for c := 0; c < cols; c++ {
count := ones(r, c)
if count < 2 || count > 3 {
if board[r][c] == 1 {
// 之前是活着的,但是被迫挂了
board[r][c] = 2
}
}

// 被迫复活
if count == 3 && board[r][c] == 0 {
board[r][c] = 3
}
}
}
for r := 0; r < rows; r++ {
for c := 0; c < cols; c++ {
if board[r][c] == 2 {
board[r][c] = 0
}
if board[r][c] == 3 {
board[r][c] = 1
}
}
}
return
}
23 changes: 13 additions & 10 deletions leetcode/201-300/0289.Game-of-Life/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,33 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs [][]int
expect [][]int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", [][]int{
{0, 1, 0}, {0, 0, 1}, {1, 1, 1}, {0, 0, 0},
}, [][]int{
{0, 0, 0}, {1, 0, 1}, {0, 1, 1}, {0, 1, 0},
}},
{"TestCase2", [][]int{{1, 1}, {1, 0}}, [][]int{{1, 1}, {1, 1}}},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
if !reflect.DeepEqual(got, c.expect) {
Solution(c.inputs)
if !reflect.DeepEqual(c.inputs, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
c.expect, c.inputs, c.inputs)
}
})
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Binary file added leetcode/201-300/0289.Game-of-Life/grid1.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added leetcode/201-300/0289.Game-of-Life/grid2.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.