Skip to content

Commit

Permalink
Merge pull request #857 from 0xff-dev/529
Browse files Browse the repository at this point in the history
Add solution and test-cases for problem 529
  • Loading branch information
6boris committed Jun 6, 2024
2 parents 6978bf0 + 0e62393 commit fdc40f4
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 26 deletions.
43 changes: 29 additions & 14 deletions leetcode/501-600/0529.Minesweeper/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,43 @@
# [529.Minesweeper][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
Let's play the minesweeper game ([Wikipedia](https://en.wikipedia.org/wiki/Minesweeper_(video_game)), [online game](http://minesweeperonline.com/))!

You are given an `m x n` char matrix `board` representing the game board where:

- `'M'` represents an unrevealed mine,
- `'E'` represents an unrevealed empty square,
- `'B'` represents a revealed blank square that has no adjacent mines (i.e., above, below, left, right, and all 4 diagonals),
- digit (`'1'` to `'8'`) represents how many mines are adjacent to this revealed square, and
- `'X'` represents a revealed mine.

You are also given an integer array `click` where `click = [clickr, clickc]` represents the next click position among all the unrevealed squares (`'M'` or `'E'`).

Return the board after revealing this position according to the following rules:

**Example 1:**
1. If a mine `'M'` is revealed, then the game is over. You should change it to `'X'`.
2. If an empty square `'E'` with no adjacent mines is revealed, then change it to a revealed blank `'B'` and all of its adjacent unrevealed squares should be revealed recursively.
3. If an empty square `'E'` with at least one adjacent mine is revealed, then change it to a digit (`'1'` to `'8'`) representing the number of adjacent mines.
4. Return the board when no more squares will be revealed.


**Example 1:**

![1](./untitled.jpeg)

```
Input: a = "11", b = "1"
Output: "100"
Input: board = [["E","E","E","E","E"],["E","E","M","E","E"],["E","E","E","E","E"],["E","E","E","E","E"]], click = [3,0]
Output: [["B","1","E","1","B"],["B","1","M","1","B"],["B","1","1","1","B"],["B","B","B","B","B"]]
```

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

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

### 思路1
> ...
Minesweeper
```go
```

Input: board = [["B","1","E","1","B"],["B","1","M","1","B"],["B","1","1","1","B"],["B","B","B","B","B"]], click = [1,2]
Output: [["B","1","E","1","B"],["B","1","X","1","B"],["B","1","1","1","B"],["B","B","B","B","B"]]
```

## 结语

Expand Down
56 changes: 54 additions & 2 deletions leetcode/501-600/0529.Minesweeper/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,57 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(board [][]byte, click []int) [][]byte {
if board[click[0]][click[1]] == 'M' {
board[click[0]][click[1]] = 'X'
return board
}
// 啥都不做
if board[click[0]][click[1]] != 'E' {
return board
}
board[click[0]][click[1]] = 'B'
queue := [][2]int{{click[0], click[1]}}
rows, cols := len(board), len(board[0])
for len(queue) > 0 {
// E的时候传播, 其他都静止
nq := make([][2]int, 0)
for _, cur := range queue {
x, y := cur[0], cur[1]
count := byte('0')
for i := -1; i <= 1; i++ {
for j := -1; j <= 1; j++ {
if i == 0 && j == 0 {
continue
}
nx, ny := x+i, y+j
if nx >= 0 && nx < rows && ny >= 0 && ny < cols {
if board[nx][ny] == 'M' {
count++
}
}
}
}
if count != '0' {
board[x][y] = count
} else {
board[x][y] = 'B'
for i := -1; i <= 1; i++ {
for j := -1; j <= 1; j++ {
if i == 0 && j == 0 {
continue
}
nx, ny := x+i, y+j
if nx >= 0 && nx < rows && ny >= 0 && ny < cols {
if board[nx][ny] == 'E' {
board[nx][ny] = 'B'
nq = append(nq, [2]int{nx, ny})
}
}
}
}
}
}
queue = nq
}
return board
}
45 changes: 35 additions & 10 deletions leetcode/501-600/0529.Minesweeper/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,55 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
board [][]byte
click []int
expect [][]byte
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", [][]byte{
{'B', '1', 'E', '1', 'B'},
{'B', '1', 'M', '1', 'B'},
{'B', '1', '1', '1', 'B'},
{'B', 'B', 'B', 'B', 'B'},
}, []int{1, 2}, [][]byte{
{'B', '1', 'E', '1', 'B'},
{'B', '1', 'X', '1', 'B'},
{'B', '1', '1', '1', 'B'},
{'B', 'B', 'B', 'B', 'B'},
}},
{"TestCase2", [][]byte{
{'E', 'E', 'E', 'E', 'E'},
{'E', 'E', 'M', 'E', 'E'},
{'E', 'E', 'E', 'E', 'E'},
{'E', 'E', 'E', 'E', 'E'},
}, []int{3, 0}, [][]byte{
{'B', '1', 'E', '1', 'B'},
{'B', '1', 'M', '1', 'B'},
{'B', '1', '1', '1', 'B'},
{'B', 'B', 'B', 'B', 'B'},
}},
{"TestCase3", [][]byte{
{'E'},
}, []int{0, 0}, [][]byte{
{'B'},
}},
}

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

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

// 使用案列
// 使用案列
func ExampleSolution() {
}
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/501-600/0529.Minesweeper/untitled.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit fdc40f4

Please sign in to comment.