803. Bricks Falling When Hit
Hard
We have a grid of 1s and 0s; the 1s in a cell represent bricks. A brick will not drop if and only if it is directly connected to the top of the grid, or at least one of its (4-way) adjacent bricks will not drop.
We will do some erasures sequentially. Each time we want to do the erasure at the location (i, j), the brick (if it exists) on that location will disappear, and then some other bricks may drop because of that erasure.
Return an array representing the number of bricks that will drop after each erasure in sequence.
Example 1: Input: grid = [[1,0,0,0],[1,1,1,0]] hits = [[1,0]] Output: [2] Explanation: If we erase the brick at (1, 0), the brick at (1, 1) and (1, 2) will drop. So we should return 2.
Example 2: Input: grid = [[1,0,0,0],[1,1,0,0]] hits = [[1,1],[1,0]] Output: [0,0] Explanation: When we erase the brick at (1, 0), the brick at (1, 1) has already disappeared due to the last move. So each erasure will cause no bricks dropping. Note that the erased brick (1, 0) will not be counted as a dropped brick.
Note:
- The number of rows and columns in the grid will be in the range [1, 200].
- The number of erasures will not exceed the area of the grid.
- It is guaranteed that each erasure will be different from any other erasure, and located inside the grid.
- An erasure may refer to a location with no brick - if it does, no bricks drop.
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 | class Solution { vector<vector<int>> dir; public: void dfs1(int i, int j, vector<vector<int>> &grid, int m, int n, int &count) { grid[i][j] = 2; for(auto d:dir) { int ii = i+d[0], jj = j+d[1]; if (ii >= 0 && ii < m && jj >= 0 && jj < n && grid[ii][jj] == 1) { count++; dfs1(ii, jj, grid, m, n, count); } } } vector<int> hitBricks(vector<vector<int>> &grid, vector<vector<int>> &hits) { int m = grid.size(), n = grid[0].size(), cnt = hits.size(), count = 0; dir.push_back(vector<int>{-1, 0}); dir.push_back(vector<int>{1, 0}); dir.push_back(vector<int>{0, -1}); dir.push_back(vector<int>{0, 1}); vector<int> ret; //remove hit bricks by turning 1 to 0 and 0 to -1 for (auto h : hits) { grid[h[0]][h[1]]--; } //do dfs on each top-edge bricks and note them by 2 //those bricks noted by 2 are non-dropping bricks for (int j = 0; j < n; ++j) { if(grid[0][j]!=1) continue; dfs1(0, j, grid, m, n, count); } //add the hit bricks back for (int c = cnt - 1; c >= 0; --c) { count = 0; int i = hits[c][0], j = hits[c][1]; if (grid[i][j] == -1) continue; grid[i][j] = 1; bool isNonDropping = i==0; for (auto d : dir) { if(isNonDropping) break; int ii = i + d[0], jj = j + d[1]; if (ii >= 0 && ii < m && jj >= 0 && jj < n && grid[ii][jj]==2){ isNonDropping = true; } } if(isNonDropping) { dfs1(i, j, grid, m, n, count); } ret.push_back(count); } reverse(ret.begin(), ret.end()); return ret; } }; |
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 | class Solution { public int[] hitBricks(int[][] grid, int[][] hits) { int r[] = new int[hits.length], d[] = {-1, 0, 1, 0, -1}; for (int[] h : hits) grid[h[0]][h[1]] -= 1; for (int i = 0; i < grid[0].length; i++) dfs(0, i, grid); for (int k = hits.length - 1; k >= 0; k--) { int h[] = hits[k], i = h[0], j = h[1]; grid[i][j] += 1; if (grid[i][j] == 1 && isConnected(i, j, grid, d)) r[k] = dfs(i, j, grid) - 1; } return r; } int dfs(int i, int j, int[][] g) { if (i < 0 || i >= g.length || j < 0 || j >= g[0].length || g[i][j] != 1) return 0; g[i][j] = 2; return 1 + dfs(i + 1, j, g) + dfs(i - 1, j, g) + dfs(i, j + 1, g) + dfs(i, j - 1, g); } boolean isConnected(int i, int j, int[][] g, int[] d) { if (i == 0) return true; for (int k = 1; k < d.length; k++) { int r = i + d[k - 1], c = j + d[k]; if (0 <= r && r < g.length && 0 <= c && c < g[0].length && g[r][c] == 2) return true; } return false; } } |
No comments:
Post a Comment