leetcode803

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.
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.BFS。O(n^2 * k)
从顶部的砖向下BFS,找所有连得到顶部的砖,就是不会掉的了。反过来把没有visited到的也就是要掉的砖去掉,并统计,即可完成每次hit要做的事。遍历每次hit即可。会TLE。
 
2.反向union find
先假设都敲掉了,然后从后往前把砖一块块加进去,如果让roof的那一个union数量增加了,(增加的数量-1)就是这块砖敲掉锁导致的掉砖数。 

实现1。BFS

class Solution {
    private class Pair {
        public int x;
        public int y;
        public Pair(int x, int y) {
            this.x = x;
            this.y = y;
        }
    }
    public int[] hitBricks(int[][] grid, int[][] hits) {
        int[] ans = new int[hits.length];
        for (int i = 0; i < hits.length; i++) {
            grid[hits[i][0]][hits[i][1]] = 0;
            ans[i] = checkBoard(grid);
        }
        return ans;
    }
    
    private int checkBoard(int[][] grid) {
        int ans = 0;
        int[] dx = {-1, 1, 0, 0};
        int[] dy = {0, 0, -1, 1};
        Queue<Pair> q = new LinkedList<>();
        boolean[][] isVisited = new boolean[grid.length][grid[0].length];
        
        for (int j = 0; j < grid[0].length; j++) {
            if (grid[0][j] == 1) {
                q.offer(new Pair(0, j));
                isVisited[0][j] = true;
            }
        }
        
        while (!q.isEmpty()) {
            Pair p = q.poll();
            for (int i = 0; i < 4; i++) {
                int nx = p.x + dx[i];
                int ny = p.y + dy[i];
                if (!isValid(grid, nx, ny) || isVisited[nx][ny] || grid[nx][ny] != 1) {
                    continue;
                }
                q.offer(new Pair(nx, ny));
                isVisited[nx][ny] = true;
            }
        }
        
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                if (grid[i][j] == 1 && !isVisited[i][j]) {
                    ans++;
                    grid[i][j] = 0;
                }
            }
        }
        return ans;
    }
    
    private boolean isValid(int[][] grid, int x, int y) {
        return x >= 0 && x < grid.length && y >= 0 && y < grid[0].length;
    }
}
原文地址:https://www.cnblogs.com/jasminemzy/p/9672491.html