Skip to content

Commit 8adbcbb

Browse files
authored
Merge pull request #1039 from 0xff-dev/2257
Add solution and test-cases for problem 2257
2 parents 6af411c + 6045992 commit 8adbcbb

File tree

5 files changed

+105
-27
lines changed

5 files changed

+105
-27
lines changed
Loading
Loading

leetcode/2201-2300/2257.Count-Unguarded-Cells-in-the-Grid/README.md

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,33 @@
11
# [2257.Count Unguarded Cells in the Grid][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+
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.
5+
6+
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.
7+
8+
Return the number of unoccupied cells that are **not guarded**.
79

8-
**Example 1:**
10+
**Example 1:**
11+
12+
![1](./1.png)
913

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

15-
## 题意
16-
> ...
21+
**Example 2:**
1722

18-
## 题解
23+
![2](./2.png)
1924

20-
### 思路1
21-
> ...
22-
Count Unguarded Cells in the Grid
23-
```go
2425
```
25-
26+
Input: m = 3, n = 3, guards = [[1,1]], walls = [[0,1],[1,0],[2,1],[1,2]]
27+
Output: 4
28+
Explanation: The unguarded cells are shown in green in the above diagram.
29+
There are a total of 4 unguarded cells, so we return 4.
30+
```
2631

2732
## 结语
2833

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(m int, n int, guards [][]int, walls [][]int) int {
4+
count := m * n
5+
count -= len(guards)
6+
count -= len(walls)
7+
wm := make(map[int]map[int]struct{})
8+
for _, w := range walls {
9+
if _, ok := wm[w[0]]; !ok {
10+
wm[w[0]] = make(map[int]struct{})
11+
}
12+
wm[w[0]][w[1]] = struct{}{}
13+
}
14+
gm := make(map[int]map[int]struct{})
15+
for _, g := range guards {
16+
if _, ok := gm[g[0]]; !ok {
17+
gm[g[0]] = make(map[int]struct{})
18+
}
19+
gm[g[0]][g[1]] = struct{}{}
20+
}
21+
22+
used := make(map[[2]int]struct{})
23+
for _, g := range guards {
24+
x, y := g[0], g[1]
25+
for i := x - 1; i >= 0; i-- {
26+
if v, ok := gm[i]; ok {
27+
if _, ok := v[y]; ok {
28+
break
29+
}
30+
}
31+
if v, ok := wm[i]; ok {
32+
if _, ok := v[y]; ok {
33+
break
34+
}
35+
}
36+
used[[2]int{i, y}] = struct{}{}
37+
}
38+
39+
for i := x + 1; i < m; i++ {
40+
if v, ok := gm[i]; ok {
41+
if _, ok := v[y]; ok {
42+
break
43+
}
44+
}
45+
if v, ok := wm[i]; ok {
46+
if _, ok := v[y]; ok {
47+
break
48+
}
49+
}
50+
used[[2]int{i, y}] = struct{}{}
51+
}
52+
53+
rows := gm[x]
54+
for i := y + 1; i < n; i++ {
55+
if _, ok := rows[i]; ok {
56+
break
57+
}
58+
if v, ok := wm[x]; ok {
59+
if _, ok := v[i]; ok {
60+
break
61+
}
62+
}
63+
used[[2]int{x, i}] = struct{}{}
64+
}
65+
for i := y - 1; i >= 0; i-- {
66+
if _, ok := rows[i]; ok {
67+
break
68+
}
69+
if v, ok := wm[x]; ok {
70+
if _, ok := v[i]; ok {
71+
break
72+
}
73+
}
74+
used[[2]int{x, i}] = struct{}{}
75+
}
76+
}
77+
return count - len(used)
578
}

leetcode/2201-2300/2257.Count-Unguarded-Cells-in-the-Grid/Solution_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,31 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
m, n int
14+
guards, walls [][]int
15+
expect int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", 4, 6, [][]int{{0, 0}, {1, 1}, {2, 3}}, [][]int{{0, 1}, {2, 2}, {1, 4}}, 7},
18+
{"TestCase2", 3, 3, [][]int{{1, 1}}, [][]int{{0, 1}, {1, 0}, {2, 1}, {1, 2}}, 4},
1919
}
2020

2121
// 开始测试
2222
for i, c := range cases {
2323
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
24+
got := Solution(c.m, c.n, c.guards, c.walls)
2525
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v %v",
27+
c.expect, got, c.m, c.n, c.guards, c.walls)
2828
}
2929
})
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)