-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path803-BricksFallingWhenHit.go
More file actions
140 lines (130 loc) · 4.82 KB
/
803-BricksFallingWhenHit.go
File metadata and controls
140 lines (130 loc) · 4.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package main
// 803. Bricks Falling When Hit
// You are given an m x n binary grid, where each 1 represents a brick and 0 represents an empty space.
// A brick is stable if:
// It is directly connected to the top of the grid, or
// At least one other brick in its four adjacent cells is stable.
// You are also given an array hits, which is a sequence of erasures we want to apply.
// Each time we want to erase the brick at the location hits[i] = (rowi, coli). The brick on that location (if it exists) will disappear.
// Some other bricks may no longer be stable because of that erasure and will fall.
// Once a brick falls, it is immediately erased from the grid (i.e., it does not land on other stable bricks).
// Return an array result, where each result[i] is the number of bricks that will fall after the ith erasure is applied.
// Note that an erasure may refer to a location with no brick, and if it does, no bricks drop.
// Example 1:
// Input: grid = [[1,0,0,0],[1,1,1,0]], hits = [[1,0]]
// Output: [2]
// Explanation: Starting with the grid:
// [[1,0,0,0],
// [1,1,1,0]]
// We erase the underlined brick at (1,0), resulting in the grid:
// [[1,0,0,0],
// [0,1,1,0]]
// The two underlined bricks are no longer stable as they are no longer connected to the top nor adjacent to another stable brick, so they will fall. The resulting grid is:
// [[1,0,0,0],
// [0,0,0,0]]
// Hence the result is [2].
// Example 2:
// Input: grid = [[1,0,0,0],[1,1,0,0]], hits = [[1,1],[1,0]]
// Output: [0,0]
// Explanation: Starting with the grid:
// [[1,0,0,0],
// [1,1,0,0]]
// We erase the underlined brick at (1,1), resulting in the grid:
// [[1,0,0,0],
// [1,0,0,0]]
// All remaining bricks are still stable, so no bricks fall. The grid remains the same:
// [[1,0,0,0],
// [1,0,0,0]]
// Next, we erase the underlined brick at (1,0), resulting in the grid:
// [[1,0,0,0],
// [0,0,0,0]]
// Once again, all remaining bricks are still stable, so no bricks fall.
// Hence the result is [0,0].
// Constraints:
// m == grid.length
// n == grid[i].length
// 1 <= m, n <= 200
// grid[i][j] is 0 or 1.
// 1 <= hits.length <= 4 * 10^4
// hits[i].length == 2
// 0 <= xi <= m - 1
// 0 <= yi <= n - 1
// All (xi, yi) are unique.
import "fmt"
func hitBricks(grid [][]int, hits [][]int) []int {
n, m, res := len(grid), len(grid[0]), make([]int, len(hits))
for _, hit := range hits {
grid[hit[0]][hit[1]] *= -1
}
var dfs func(grid [][]int, x, y int) int
dfs = func(grid [][]int, x, y int) int {
if x < 0 || y < 0 || x >= len(grid) || y >= len(grid[0]) || grid[x][y] != 1 { return 0 }
grid[x][y] = 2
return 1 + dfs(grid, x+1, y) + dfs(grid, x, y+1) + dfs(grid, x-1, y) + dfs(grid, x, y-1)
}
for i := 0; i < m; i++ {
dfs(grid, 0, i)
}
connected := func(grid [][]int, x, y, n, m int) bool {
if x == 0 { return true }
if x+1 < n && grid[x+1][y] == 2 { return true }
if x-1 >= 0 && grid[x-1][y] == 2 { return true }
if y+1 < m && grid[x][y+1] == 2 { return true }
if y-1 >= 0 && grid[x][y-1] == 2 { return true }
return false
}
for i := len(hits) - 1; i >= 0; i-- {
x := hits[i][0]
y := hits[i][1]
if grid[x][y] == 0 {
continue
}
grid[x][y] = 1
if connected(grid, x, y, n, m) {
res[i] = dfs(grid, x, y) - 1
}
}
return res
}
func main() {
// Example 1:
// Input: grid = [[1,0,0,0],[1,1,1,0]], hits = [[1,0]]
// Output: [2]
// Explanation: Starting with the grid:
// [[1,0,0,0],
// [1,1,1,0]]
// We erase the underlined brick at (1,0), resulting in the grid:
// [[1,0,0,0],
// [0,1,1,0]]
// The two underlined bricks are no longer stable as they are no longer connected to the top nor adjacent to another stable brick, so they will fall. The resulting grid is:
// [[1,0,0,0],
// [0,0,0,0]]
// Hence the result is [2].
grid1 := [][]int{
{1,0,0,0},
{1,1,1,0},
}
fmt.Println(hitBricks(grid1, [][]int{{1,0}})) // [2]
// Example 2:
// Input: grid = [[1,0,0,0],[1,1,0,0]], hits = [[1,1],[1,0]]
// Output: [0,0]
// Explanation: Starting with the grid:
// [[1,0,0,0],
// [1,1,0,0]]
// We erase the underlined brick at (1,1), resulting in the grid:
// [[1,0,0,0],
// [1,0,0,0]]
// All remaining bricks are still stable, so no bricks fall. The grid remains the same:
// [[1,0,0,0],
// [1,0,0,0]]
// Next, we erase the underlined brick at (1,0), resulting in the grid:
// [[1,0,0,0],
// [0,0,0,0]]
// Once again, all remaining bricks are still stable, so no bricks fall.
// Hence the result is [0,0].
grid2 := [][]int{
{1,0,0,0},
{1,1,0,0},
}
fmt.Println(hitBricks(grid2, [][]int{{1,1},{1,0}})) // [0,0]
}