zoukankan      html  css  js  c++  java
  • 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;
        }
    }
  • 相关阅读:
    css中盒模型的理解与整理
    Java从入门到精通——数据库篇Mongo DB GridFS文件系统
    Java从入门到精通——数据库篇Mongo DB 导出,导入,备份
    Java从入门到精通——数据库篇Mongo DB 安装启动及配置详解
    MySQL基于mysqldump快速搭建从库
    Linux下C语言操作MySQL数据库
    html dl dt dd 标签语法与使用
    人的一切痛苦,本质上都是对自己的无能的愤怒
    游泳健康好处多
    从疲劳到猝死只需6步!
  • 原文地址:https://www.cnblogs.com/jasminemzy/p/9672491.html
Copyright © 2011-2022 走看看