Skip to content
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

Add solution and test-cases for problem 2257 #1039

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 19 additions & 14 deletions leetcode/2201-2300/2257.Count-Unguarded-Cells-in-the-Grid/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
# [2257.Count Unguarded Cells in the Grid][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
You are given two integers m and n representing a **0-indexed** `m x n` grid. You are also given two 2D integer arrays `guards` and `walls` where `guards[i] = [rowi, coli]` and `walls[j] = [rowj, colj]` represent the positions of the `ith` guard and `jth` wall respectively.

A guard can see **every** cell in the four cardinal directions (north, east, south, or west) starting from their position unless **obstructed** by a wall or another guard. A cell is **guarded** if there is **at least** one guard that can see it.

Return the number of unoccupied cells that are **not guarded**.

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

![1](./1.png)

```
Input: a = "11", b = "1"
Output: "100"
Input: m = 4, n = 6, guards = [[0,0],[1,1],[2,3]], walls = [[0,1],[2,2],[1,4]]
Output: 7
Explanation: The guarded and unguarded cells are shown in red and green respectively in the above diagram.
There are a total of 7 unguarded cells, so we return 7.
```

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

## 题解
![2](./2.png)

### 思路1
> ...
Count Unguarded Cells in the Grid
```go
```

Input: m = 3, n = 3, guards = [[1,1]], walls = [[0,1],[1,0],[2,1],[1,2]]
Output: 4
Explanation: The unguarded cells are shown in green in the above diagram.
There are a total of 4 unguarded cells, so we return 4.
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,78 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(m int, n int, guards [][]int, walls [][]int) int {
count := m * n
count -= len(guards)
count -= len(walls)
wm := make(map[int]map[int]struct{})
for _, w := range walls {
if _, ok := wm[w[0]]; !ok {
wm[w[0]] = make(map[int]struct{})
}
wm[w[0]][w[1]] = struct{}{}
}
gm := make(map[int]map[int]struct{})
for _, g := range guards {
if _, ok := gm[g[0]]; !ok {
gm[g[0]] = make(map[int]struct{})
}
gm[g[0]][g[1]] = struct{}{}
}

used := make(map[[2]int]struct{})
for _, g := range guards {
x, y := g[0], g[1]
for i := x - 1; i >= 0; i-- {
if v, ok := gm[i]; ok {
if _, ok := v[y]; ok {
break
}
}
if v, ok := wm[i]; ok {
if _, ok := v[y]; ok {
break
}
}
used[[2]int{i, y}] = struct{}{}
}

for i := x + 1; i < m; i++ {
if v, ok := gm[i]; ok {
if _, ok := v[y]; ok {
break
}
}
if v, ok := wm[i]; ok {
if _, ok := v[y]; ok {
break
}
}
used[[2]int{i, y}] = struct{}{}
}

rows := gm[x]
for i := y + 1; i < n; i++ {
if _, ok := rows[i]; ok {
break
}
if v, ok := wm[x]; ok {
if _, ok := v[i]; ok {
break
}
}
used[[2]int{x, i}] = struct{}{}
}
for i := y - 1; i >= 0; i-- {
if _, ok := rows[i]; ok {
break
}
if v, ok := wm[x]; ok {
if _, ok := v[i]; ok {
break
}
}
used[[2]int{x, i}] = struct{}{}
}
}
return count - len(used)
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,31 @@ import (
func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
name string
m, n int
guards, walls [][]int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", 4, 6, [][]int{{0, 0}, {1, 1}, {2, 3}}, [][]int{{0, 1}, {2, 2}, {1, 4}}, 7},
{"TestCase2", 3, 3, [][]int{{1, 1}}, [][]int{{0, 1}, {1, 0}, {2, 1}, {1, 2}}, 4},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.m, c.n, c.guards, c.walls)
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 %v %v %v",
c.expect, got, c.m, c.n, c.guards, c.walls)
}
})
}
}

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

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading