1
+ class Solution {
2
+ int dx[4 ] = {-1 , 0 , 1 , 0 };
3
+ int dy[4 ] = {0 , 1 , 0 , -1 };
4
+ int totalBricksAttached (int x, int y, vector<vector<int >>& grid) {
5
+ if (grid[x][y] != 1 ) {
6
+ return 0 ;
7
+ }
8
+ grid[x][y] = 2 ;
9
+ int count = 0 ;
10
+ for (int i = 0 ; i < sizeof (dx) / sizeof (dx[0 ]); i++) {
11
+ int neighX = x + dx[i];
12
+ int neighY = y + dy[i];
13
+ if (neighX >= 0 and neighY >= 0 and neighX < grid.size () and neighY < grid[0 ].size ()
14
+ and grid[neighX][neighY] == 1 ) {
15
+ count += totalBricksAttached (neighX, neighY, grid);
16
+ }
17
+ }
18
+ return 1 + count;
19
+ }
20
+
21
+ bool isConnectedToTop (int x, int y, vector<vector<int >>& grid) {
22
+ if (x == 0 ) {
23
+ return true ;
24
+ }
25
+ for (int i = 0 ; i < sizeof (dx) / sizeof (dx[0 ]); i++) {
26
+ int neighX = x + dx[i];
27
+ int neighY = y + dy[i];
28
+ if (neighX >= 0 and neighY >= 0 and neighX < grid.size () and neighY < grid[0 ].size ()
29
+ and grid[neighX][neighY] == 2 ) {
30
+ return true ;
31
+ }
32
+ }
33
+ return false ;
34
+ }
35
+
36
+ vector<int > searchFallingBricks (vector<vector<int >>& grid, vector<vector<int >>& hits) {
37
+ vector<int > result (hits.size (), 0 );
38
+ for (int i = hits.size () - 1 ; i >= 0 ; i--) {
39
+ vector<int >& hit = hits[i];
40
+ int x = hit[0 ], y = hit[1 ];
41
+ if (grid[x][y] == 0 ) {
42
+ grid[x][y] = 1 ;
43
+ if (isConnectedToTop (x, y, grid)) {
44
+ result[i] = totalBricksAttached (x, y, grid) - 1 ;
45
+ }
46
+ }
47
+ }
48
+ return result;
49
+ }
50
+
51
+ void removeHits (vector<vector<int >>& grid, vector<vector<int >>& hits) {
52
+ for (vector<int > hit : hits) {
53
+ int x = hit[0 ], y = hit[1 ];
54
+ grid[x][y] = grid[x][y] - 1 ;
55
+ }
56
+ }
57
+
58
+ void markBricks (vector<vector<int >>& grid) {
59
+ for (int i = 0 ; i < grid[0 ].size (); i++) {
60
+ totalBricksAttached (0 , i, grid);
61
+ }
62
+ }
63
+ public:
64
+ vector<int > hitBricks (vector<vector<int >>& grid, vector<vector<int >>& hits) {
65
+ removeHits (grid, hits);
66
+ markBricks (grid);
67
+ return searchFallingBricks (grid, hits);
68
+ }
69
+ };
70
+
71
+ // TLE
72
+ class Solution {
73
+ int dx[4 ] = {-1 , 0 , 1 , 0 };
74
+ int dy[4 ] = {0 , 1 , 0 , -1 };
75
+ int totalBricksAttached (vector<vector<int >>& grid, vector<vector<bool >>& isBrick, int x, int y) {
76
+ isBrick[x][y] = true ;
77
+ int count = 0 ;
78
+ for (int i = 0 ; i < sizeof (dx) / sizeof (dx[0 ]); i++) {
79
+ int neighX = x + dx[i];
80
+ int neighY = y + dy[i];
81
+ if (neighX >= 0 and neighY >= 0 and neighX < grid.size () and neighY < grid[0 ].size ()
82
+ and grid[neighX][neighY] and !isBrick[neighX][neighY]) {
83
+ isBrick[neighX][neighY] = true ;
84
+ count += totalBricksAttached (grid, isBrick, neighX, neighY);
85
+ }
86
+ }
87
+ return 1 + count;
88
+ }
89
+
90
+ int totalBricksAttached (vector<vector<int >>& grid, vector<vector<bool >>& isBrick) {
91
+ int totalBricks = 0 ;
92
+ for (int i = 0 ; i < grid[0 ].size (); i++) {
93
+ if (grid[0 ][i] and !isBrick[0 ][i]) {
94
+ totalBricks += totalBricksAttached (grid, isBrick, 0 , i);
95
+ }
96
+ }
97
+ return totalBricks;
98
+ }
99
+ public:
100
+ vector<int > hitBricks (vector<vector<int >>& grid, vector<vector<int >>& hits) {
101
+ vector<vector<bool >> isBrick;
102
+
103
+ isBrick = vector<vector<bool >>(grid.size (), vector<bool > (grid[0 ].size (), false ));
104
+ int totalBricks = totalBricksAttached (grid, isBrick);
105
+
106
+ vector<int > result;
107
+ for (vector<int > hit : hits) {
108
+ int x = hit[0 ], y = hit[1 ];
109
+ if (!isBrick[x][y]) {
110
+ result.push_back (0 );
111
+ continue ;
112
+ }
113
+ grid[x][y] = 0 ;
114
+ totalBricks--;
115
+ isBrick = vector<vector<bool >>(grid.size (), vector<bool > (grid[0 ].size (), false ));
116
+ int fallenBricks = totalBricks - totalBricksAttached (grid, isBrick);
117
+ result.push_back (fallenBricks);
118
+ totalBricks -= fallenBricks;
119
+ }
120
+
121
+ return result;
122
+ }
123
+ };
0 commit comments