LeetCode803. Bricks Falling When Hit——洪水填充

文章目录

一、题目

You are given an m x n binary grid, where each 1 represents a brick and 0 represents an empty space. A brick is stable if:

It is directly connected to the top of the grid, or
At least one other brick in its four adjacent cells is stable.
You are also given an array hits, which is a sequence of erasures we want to apply. Each time we want to erase the brick at the location hits[i] = (rowi, coli). The brick on that location (if it exists) will disappear. Some other bricks may no longer be stable because of that erasure and will fall. Once a brick falls, it is immediately erased from the grid (i.e., it does not land on other stable bricks).

Return an array result, where each result[i] is the number of bricks that will fall after the ith erasure is applied.

Note that an erasure may refer to a location with no brick, and if it does, no bricks drop.

Example 1:

Input: grid = [[1,0,0,0],[1,1,1,0]], hits = [[1,0]]
Output: [2]
Explanation: Starting with the grid:
[[1,0,0,0],
[1,1,1,0]]
We erase the underlined brick at (1,0), resulting in the grid:
[[1,0,0,0],
[0,1,1,0]]
The two underlined bricks are no longer stable as they are no longer connected to the top nor adjacent to another stable brick, so they will fall. The resulting grid is:
[[1,0,0,0],
[0,0,0,0]]
Hence the result is [2].
Example 2:

Input: grid = [[1,0,0,0],[1,1,0,0]], hits = [[1,1],[1,0]]
Output: [0,0]
Explanation: Starting with the grid:
[[1,0,0,0],
[1,1,0,0]]
We erase the underlined brick at (1,1), resulting in the grid:
[[1,0,0,0],
[1,0,0,0]]
All remaining bricks are still stable, so no bricks fall. The grid remains the same:
[[1,0,0,0],
[1,0,0,0]]
Next, we erase the underlined brick at (1,0), resulting in the grid:
[[1,0,0,0],
[0,0,0,0]]
Once again, all remaining bricks are still stable, so no bricks fall.
Hence the result is [0,0].

Constraints:

m == grid.length
n == grid[i].length
1 <= m, n <= 200
grid[i][j] is 0 or 1.
1 <= hits.length <= 4 * 104
hits[i].length == 2
0 <= xi <= m - 1
0 <= yi <= n - 1
All (xi, yi) are unique.

二、题解

class Solution {
   
public:
    int n,m;
    vector<int> hitBricks(vector<vector<int>>& grid, vector<vector<int>>& hits) {
   
        n = grid.size(),m = grid[0].size();
        vector<int> res(hits.size(),0);
        if(n == 1) return res;
        for(auto hit:hits){
   
            grid[hit[0]][hit[1]]--;
        }
        for(int j = 0;j < m;j++){
   
            dfs(grid,0,j);
        }
        for(int i = hits.size() - 1;i >= 0;i--){
   
            int r = hits[i][0],c = hits[i][1];
            grid[r][c]++;
            if(worth(grid,r,c)){
   
                res[i] = dfs(grid,r,c) - 1;
            }
        }
        return res;
    }
    int dfs(vector<vector<int>>& grid,int i,int j){
   
        if(i < 0 || i == n || j < 0 || j == m || grid[i][j] != 1) return 0;
        grid[i][j] = 2;
        return 1 + dfs(grid,i-1,j) + dfs(grid,i,j-1) + dfs(grid,i+1,j) + dfs(grid,i,j+1);
    }
    bool worth(vector<vector<int>>& grid,int i,int j){
   
        return grid[i][j] == 1 &&(i == 0
				|| (i > 0 && grid[i - 1][j] == 2)
				|| (i < n - 1 && grid[i + 1][j] == 2)
				|| (j > 0 && grid[i][j - 1] == 2)
				|| (j < m - 1 && grid[i][j + 1] == 2));
    }
};

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-04 16:02:01       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-04 16:02:01       106 阅读
  3. 在Django里面运行非项目文件

    2024-02-04 16:02:01       87 阅读
  4. Python语言-面向对象

    2024-02-04 16:02:01       96 阅读

热门阅读

  1. vue中$refs的用法及作用详解

    2024-02-04 16:02:01       45 阅读
  2. C# 从“byte[]”转换为“BitmapImage”

    2024-02-04 16:02:01       58 阅读
  3. openEuler 22.03 docker安装elasticsearch 7.17.17

    2024-02-04 16:02:01       41 阅读
  4. 美敦力呼吸机PB560硬件分析

    2024-02-04 16:02:01       56 阅读
  5. [C语言]结构体初识

    2024-02-04 16:02:01       56 阅读
  6. Large Language Models on Graphs: A Comprehensive Survey

    2024-02-04 16:02:01       60 阅读
  7. 在 Qt 中如何将数字转换为QString

    2024-02-04 16:02:01       52 阅读
  8. 栈和队列-删除字符串中的所有相邻重复项

    2024-02-04 16:02:01       55 阅读
  9. 蓝桥杯题目-四个瓷瓶的神秘游戏

    2024-02-04 16:02:01       58 阅读
  10. 音频处理库和工具,以及相关开源

    2024-02-04 16:02:01       46 阅读
  11. 什么是ISO21434网络安全中的汽车网络?-亚远景

    2024-02-04 16:02:01       61 阅读