当前位置:网站首页>leetcode1020. 飞地的数量(中等)
leetcode1020. 飞地的数量(中等)
2022-07-06 06:55:00 【重you小垃】
思路:dfs/bfs模板题
跟 leetcode130.被围绕的区域(中等) 类似
https://blog.csdn.net/zhangjiaji111/article/details/123668205
class Solution {
public:
int flag[505][505] = {
0};
int xy[4][2] = {
0, 1, 0, -1, 1, 0, -1, 0};
void dfs(vector<vector<int>>& grid, int x, int y) {
int n = grid.size(), m = grid[0].size();
flag[x][y] = 1;
for (int i = 0; i < 4; ++i) {
int xx = x + xy[i][0];
int yy = y + xy[i][1];
if (xx < 0 || xx > n - 1 || yy < 0 || yy > m - 1) continue;
if (grid[xx][yy] && !flag[xx][yy]) dfs(grid, xx, yy);
}
}
int numEnclaves(vector<vector<int>>& grid) {
int n = grid.size(), m = grid[0].size();
for (int j = 0; j < m; ++j) {
if (grid[0][j] && !flag[0][j]) dfs(grid, 0, j);
if (grid[n - 1][j] && !flag[n - 1][j]) dfs(grid, n - 1, j);
}
for (int i = 1; i < n - 1; ++i) {
if (grid[i][0] && !flag[i][0]) dfs(grid, i, 0);
if (grid[i][m - 1] && !flag[i][m - 1]) dfs(grid, i, m - 1);
}
int ans = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (grid[i][j] && !flag[i][j]) ans++;
}
}
return ans;
}
};
思路三:并查集
具体思路:设置一个虚拟的陆地:n*m,初始化时边缘的陆地与该单元格连通。然后将图中的1构造若干个连通分量。
ans:单元格为1且与n*m的findfather()相等的个数。
class Solution {
public:
vector<int> father;
void unite (int u, int v) {
int fau = findfather(u);
int fav = findfather(v);
if (fau != fav) father[fau] = fav;
}
int findfather(int m) {
if (father[m] == m) return m;
return father[m] = findfather(father[m]);
}
int numEnclaves(vector<vector<int>>& grid) {
int n = grid.size(), m = grid[0].size();
father.resize(n * m + 1);
for (int i = 0; i < m * n + 1; ++i) {
father[i] = i;
}
auto getindex = [&](int x, int y) {
return x * m + y;
};
for (int j = 0; j < m; ++j) {
if (grid[0][j]) unite(getindex(0, j), n * m);
if (grid[n - 1][j]) unite(getindex(n - 1, j), n * m);
}
for (int i = 1; i < n - 1; ++i) {
if (grid[i][0]) unite(getindex(i, 0), n * m);
if (grid[i][m - 1]) unite(getindex(i, m - 1), n * m);
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (grid[i][j]) {
if (i - 1 >= 0 && grid[i - 1][j]) unite(getindex(i - 1, j), getindex(i, j));
if (j - 1 >= 0 && grid[i][j - 1]) unite(getindex(i, j - 1), getindex(i, j));
}
}
}
int ans = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (grid[i][j] && findfather(getindex(i, j)) != findfather(n * m)) ans++;
}
}
return ans;
}
};
边栏推荐
- 【服务器数据恢复】IBM服务器raid5两块硬盘离线数据恢复案例
- 自动化测试环境配置
- Do you really know the use of idea?
- (practice C language every day) reverse linked list II
- ML之shap:基于adult人口普查收入二分类预测数据集(预测年收入是否超过50k)利用Shap值对XGBoost模型实现可解释性案例之详细攻略
- 机器学习植物叶片识别
- Refer to how customer push e-commerce does content operation
- UWA Pipeline 2.2.1 版本更新说明
- AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models. common‘ from ‘/home/yolov5/models/comm
- Blue Bridge Cup zero Foundation National Championship - day 20
猜你喜欢
After sharing the clone remote project, NPM install reports an error - CB () never called! This is an error with npm itself.
AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models.common‘ from ‘/home/yolov5/models/comm
BUU的MISC(不定时更新)
What is the biggest problem that fresh e-commerce is difficult to do now
万丈高楼平地起,每个API皆根基
18. Multi level page table and fast table
漏了监控:Zabbix对Eureka instance状态监控
After working for 10 years, I changed to a programmer. Now I'm 35 + years old and I'm not anxious
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
How to find a medical software testing institution? First flight software evaluation is an expert
随机推荐
Suspended else
GET 和 POST 请求类型的区别
基于购买行为数据对超市顾客进行市场细分(RFM模型)
UDP攻击是什么意思?UDP攻击防范措施
AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models. common‘ from ‘/home/yolov5/models/comm
自动化测试环境配置
ML之shap:基于adult人口普查收入二分类预测数据集(预测年收入是否超过50k)利用Shap值对XGBoost模型实现可解释性案例之详细攻略
librosa音频处理教程
Misc of BUU (update from time to time)
【刷题】怎么样才能正确的迎接面试?
After working for 10 years, I changed to a programmer. Now I'm 35 + years old and I'm not anxious
BUU的MISC(不定时更新)
BIO模型实现多人聊天
[Yu Yue education] Dunhuang Literature and art reference materials of Zhejiang Normal University
AI on the cloud makes earth science research easier
Leetcode daily question (1997. first day where you have been in all the rooms)
Day 239/300 注册密码长度为8~14个字母数字以及标点符号至少包含2种校验
How to find a medical software testing institution? First flight software evaluation is an expert
指尖上的 NFT|在 G2 上评价 Ambire,有机会获得限量版收藏品
18. Multi level page table and fast table