Bricks Falling When Hit

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.

思路:爲了避免每次去掉一個,做一次全局的dfs,我們倒着算。也就是首先把hits所有點去掉,然後把跟top連接的點全部變成2;然後逆着加入每個hits,如果周圍四個點有2,也就是跟top連接,那麼count dfs = 1的點,減去1,就是會掉落的點個數。

class Solution {
    private int[][] dirs = {{1,0}, {-1,0}, {0,-1}, {0,1}};
    public int[] hitBricks(int[][] grid, int[][] hits) {
        int n = grid.length;
        int m = grid[0].length;
        // remove hits;
        for(int[] hit: hits) {
            int x = hit[0];
            int y = hit[1];
            grid[x][y]--;
        }
        
        // make top connected component to 2;
        for(int j = 0; j < m; j++) {
            if(grid[0][j] == 1) {
                dfs(grid, 0, j);
            }
        }
        
        int[] res = new int[hits.length];
        for(int i = hits.length - 1; i >= 0; i--) {
            int x = hits[i][0]; int y = hits[i][1];
            grid[x][y]++;
            if(grid[x][y] == 0) continue;
            if(!isConnectToTop(grid, x, y)) continue;
            res[i] = dfs(grid, x, y) - 1;
        }
        return res;
    }
    
    private int dfs(int[][] grid, int x, int y) {
        if(x < 0 || x >= grid.length || y < 0 || y >= grid[0].length || grid[x][y] != 1) {
            return 0;
        }
        int count = 1;
        grid[x][y] = 2;
        for(int[] dir: dirs) {
            int nx = x + dir[0];
            int ny = y + dir[1];
            count += dfs(grid, nx, ny);
        }
        return count;
    }
    
    // 判斷周圍四個點是否有2,也就是是否connect to top;因爲top全部是2;
    private boolean isConnectToTop(int[][] grid, int x, int y) {
        if(x == 0) return true;
        for(int[] dir: dirs) {
            int nx = x + dir[0];
            int ny = y + dir[1];
            if(nx < 0 || nx >= grid.length || ny < 0 || ny >= grid[0].length || grid[nx][ny] != 2) {
                continue;
            }
            if(grid[nx][ny] == 2) {
                return true;
            }
        }
        return false;
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章