Re: [闲聊] 每日leetcode

楼主: oin1104 (是oin的说)   2024-08-28 11:30:57
※ 引述 《argorok (死肥肥社管)》 之铭言:
:  
: 1905. Count Sub Islands
:  
: 给两个grid 求grid2的islands中是grid1的sub island的数量
sub island :
当前island
全部的格子都在grid1的那个island里面
只要超出去一个就不算
思路 :
一边遍历所有grid2的island
顺便看当前island
有没有全部跟grid1的island 重叠
然后加起来就好了
```cpp
class Solution {
public:
vector<vector<int>> paper;
void go(vector<vector<int>>& grid , int i , int j , int &jiwp)
{
if(i<0 || i>=grid.size() || j<0 || j>=grid[0].size())return;
if(grid[i][j]==0)return;
grid[i][j]=0;
if(paper[i][j]==0)jiwp = 0;
go(grid,i-1,j,jiwp);
go(grid,i+1,j,jiwp);
go(grid,i,j-1,jiwp);
go(grid,i,j+1,jiwp);
}
int countSubIslands(vector<vector<int>>& grid1, vector<vector<int>>& grid2)
{
paper = grid1;
int n = grid2.size();
int m = grid2[0].size();
int res = 0;
for(int i = 0 ; i < n ; i ++)
{
for(int j = 0 ; j < m ; j++)
{
if(grid2[i][j]==0)continue;
int jiwp = 1;
go(grid2,i,j,jiwp);
if(jiwp)res++;
}
}
return res;
}
};
```
作者: PogChampLUL (火车站肥宅)   2024-08-28 11:33:00
为啥有jiwp
楼主: oin1104 (是oin的说)   2024-08-28 11:33:00
因为他可以01自由切换 很好用
作者: PogChampLUL (火车站肥宅)   2024-08-28 11:41:00
你真的很恶
楼主: oin1104 (是oin的说)   2024-08-28 11:43:00
.

Links booklink

Contact Us: admin [ a t ] ucptt.com