Leetcode 803. Bricks Falling When Hit

Problem:

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.

Solution:

  这道题我尽力尝试过用Union Find的解法,但始终写不出答案,只好用DFS写了。首先遍历hits数组去掉所有打掉的砖块,然后从第一行开始用DFS找出所有不会掉落的砖块放入notDrop集合中,然后倒序遍历hits,从被打掉的砖块开始调用DFS,如果这个砖块周围某个砖块存在于notDrop集合中,则说明该砖块与顶部相连,不会掉落,因此就可以对这个砖块的四周调用DFS,找出所有的联通块插入notDrop中,然后比较插入前后的差值,别忘了减去打掉的这一块砖头不算。这道题算是给了我一个警告,面试中如果没有十足的把握不要用Union Find解题,不然很容易把自己玩死。

Code:

 

 1 class Solution {
 2 public:
 3     void dfs(vector<vector<int>>& grid,int i,int j,unordered_set<int>& notDrop) {
 4         int m = grid.size(), n = grid[0].size();
 5         if (i < 0 || i >= m || j < 0 || j >= n || grid[i][j] != 1 || notDrop.find(i*n+j) != notDrop.end()) return;
 6         notDrop.insert(i * n + j);
 7         dfs(grid, i - 1, j, notDrop);
 8         dfs(grid, i + 1, j, notDrop);
 9         dfs(grid, i, j - 1, notDrop);
10         dfs(grid, i, j + 1, notDrop);
11     }
12     vector<int> hitBricks(vector<vector<int>>& grid, vector<vector<int>>& hits) {
13         int m = grid.size();
14         int n = grid[0].size();
15         vector<int> result(hits.size());
16         unordered_set<int> notDrop;
17         for (int i = 0;i != hits.size();++i) 
18             grid[hits[i][0]][hits[i][1]] -= 1;
19         for (int i = 0;i != n;++i) {
20             if (grid[0][i] == 1) 
21                 dfs(grid,0,i,notDrop);
22         }
23         for (int i = hits.size()-1;i >= 0;--i) {
24             int oldSize = notDrop.size();
25             int x = hits[i][0];
26             int y = hits[i][1];
27             if (++grid[x][y] != 1) continue;
28             if ((x - 1 >= 0 && notDrop.find((x-1)*n+y) != notDrop.end()) 
29                 || (x + 1 < m && notDrop.find((x+1)*n+y) != notDrop.end())
30                 || (y - 1 >= 0 && notDrop.find(x*n+y-1) != notDrop.end())
31                 || (y + 1 < n && notDrop.find(x*n+y+1) != notDrop.end())
32                 || x == 0) {
33                 dfs(grid,x,y,notDrop);
34                 result[i] = notDrop.size()-oldSize-1;
35             }
36         }
37         return result;
38     }
39 
40 };

 

posted on 2019-01-16 16:01  周浩炜  阅读(255)  评论(0编辑  收藏  举报

导航