diff --git a/leetcode/201-300/0289.Game-of-Life/README.md b/leetcode/201-300/0289.Game-of-Life/README.md index 3d5b93634..641c0f37a 100644 --- a/leetcode/201-300/0289.Game-of-Life/README.md +++ b/leetcode/201-300/0289.Game-of-Life/README.md @@ -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]] +``` ## 结语 diff --git a/leetcode/201-300/0289.Game-of-Life/Solution.go b/leetcode/201-300/0289.Game-of-Life/Solution.go index d115ccf5e..bf06ec271 100644 --- a/leetcode/201-300/0289.Game-of-Life/Solution.go +++ b/leetcode/201-300/0289.Game-of-Life/Solution.go @@ -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 } diff --git a/leetcode/201-300/0289.Game-of-Life/Solution_test.go b/leetcode/201-300/0289.Game-of-Life/Solution_test.go index 14ff50eb4..cccee3274 100644 --- a/leetcode/201-300/0289.Game-of-Life/Solution_test.go +++ b/leetcode/201-300/0289.Game-of-Life/Solution_test.go @@ -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() { } diff --git a/leetcode/201-300/0289.Game-of-Life/grid1.jpeg b/leetcode/201-300/0289.Game-of-Life/grid1.jpeg new file mode 100644 index 000000000..55bc9afbb Binary files /dev/null and b/leetcode/201-300/0289.Game-of-Life/grid1.jpeg differ diff --git a/leetcode/201-300/0289.Game-of-Life/grid2.jpeg b/leetcode/201-300/0289.Game-of-Life/grid2.jpeg new file mode 100644 index 000000000..58864658c Binary files /dev/null and b/leetcode/201-300/0289.Game-of-Life/grid2.jpeg differ