这道题见鬼的我连题目都没有读懂。。。郁闷。。。。
这道题我用深度优先遍历坐的,当然超时了。。。
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.
解题思路:
来说一下用并查集怎么做这道题。有哪些砖块会随着某一块砖块(A)的敲击而掉落呢?首先那些砖块得通过A连接到顶层,但是没有跟A连接前其不能连接到顶层。所以先将所有要敲击的砖块都敲落 , 计算连通块。
然后再将砖块依次装回去,看看哪些砖块是本来不能连接到顶层但是通过连接要敲落的砖块连接到顶层的。
这里又出现了一个问题,依次是按照什么样的顺序,如果是从前往后,便不知道砖块是否能通过连接后面要敲落的砖块连接到顶层,所以不予考虑;那从后往前呢?因为前面的砖块已经被敲落,所以不需要考虑砖块是否能通过前面砖块连接到顶层;
class Solution {
public:
vector<int> hitBricks(vector<vector<int>>& grid, vector<vector<int>>& hits)
{
vector<int> res ;
for(int i = 0 ; i < hits.size() ; ++i)
{
res.push_back(hit(hits[i][0] , hits[i][1] , grid)) ;
}
return res ;
}
int hit(int y , int x , vector<vector<int>> &grid )
{
if(grid[y][x] == 0) return 0 ;
int ans = 0 ;
grid[y][x] = 0 ;
for(int i = 0 ; i < 4 ; ++i)
{
vector<vector<int>> visited(grid.size() , vector(grid[0].size() , 0)) ;
vector<vector<int>> clrgrid ;
visited[y][x] = 1 ;
if(!fall(y + dir[i] , x + dir[i + 1] , grid , visited , clrgrid)) continue ;
ans += clrgrid.size() ;
clr(grid , clrgrid) ;
}
return ans ;
}
bool fall(int y , int x , vector<vector<int>> &grid , vector<vector<int>> &visited , vector<vector<int>> &clrgrid)
{
if(y < 0 || y >= grid.size() || x < 0 || x >= grid[0].size()) return true ;
if(visited[y][x] == 1 || grid[y][x] == 0) return true ;
if(y == 0) return false ;
clrgrid.push_back({y , x}) ;
visited[y][x] = 1 ;
for(int i = 0 ; i < 4 ; ++i)
{
if(!fall(y + dir[i] , x + dir[i + 1] , grid , visited , clrgrid)) return false ;
}
return true ;
}
void clr(vector<vector<int>> &grid , vector<vector<int>> &clrgrid)
{
for(int i = 0 ; i < clrgrid.size() ; ++i)
{
int y = clrgrid[i][0] ;
int x = clrgrid[i][1] ;
grid[y][x] = 0 ;
}
}
private :
vector<int> dir = { -1 , 0 , 1 , 0 , -1} ;
};