diff --git a/leetcode/2201-2300/2257.Count-Unguarded-Cells-in-the-Grid/1.png b/leetcode/2201-2300/2257.Count-Unguarded-Cells-in-the-Grid/1.png new file mode 100644 index 000000000..1ca001435 Binary files /dev/null and b/leetcode/2201-2300/2257.Count-Unguarded-Cells-in-the-Grid/1.png differ diff --git a/leetcode/2201-2300/2257.Count-Unguarded-Cells-in-the-Grid/2.png b/leetcode/2201-2300/2257.Count-Unguarded-Cells-in-the-Grid/2.png new file mode 100644 index 000000000..2683e5e8c Binary files /dev/null and b/leetcode/2201-2300/2257.Count-Unguarded-Cells-in-the-Grid/2.png differ diff --git a/leetcode/2201-2300/2257.Count-Unguarded-Cells-in-the-Grid/README.md b/leetcode/2201-2300/2257.Count-Unguarded-Cells-in-the-Grid/README.md index 0b9064584..284fbc24d 100755 --- a/leetcode/2201-2300/2257.Count-Unguarded-Cells-in-the-Grid/README.md +++ b/leetcode/2201-2300/2257.Count-Unguarded-Cells-in-the-Grid/README.md @@ -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. +``` ## 结语 diff --git a/leetcode/2201-2300/2257.Count-Unguarded-Cells-in-the-Grid/Solution.go b/leetcode/2201-2300/2257.Count-Unguarded-Cells-in-the-Grid/Solution.go index d115ccf5e..bfd5fdbf2 100644 --- a/leetcode/2201-2300/2257.Count-Unguarded-Cells-in-the-Grid/Solution.go +++ b/leetcode/2201-2300/2257.Count-Unguarded-Cells-in-the-Grid/Solution.go @@ -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) } diff --git a/leetcode/2201-2300/2257.Count-Unguarded-Cells-in-the-Grid/Solution_test.go b/leetcode/2201-2300/2257.Count-Unguarded-Cells-in-the-Grid/Solution_test.go index 14ff50eb4..abb129b70 100644 --- a/leetcode/2201-2300/2257.Count-Unguarded-Cells-in-the-Grid/Solution_test.go +++ b/leetcode/2201-2300/2257.Count-Unguarded-Cells-in-the-Grid/Solution_test.go @@ -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() { }