当前位置:网站首页>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;
}
};
边栏推荐
- 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
- 《从0到1:CTFer成长之路》书籍配套题目(周更)
- After sharing the clone remote project, NPM install reports an error - CB () never called! This is an error with npm itself.
- [English] Grammar remodeling: the core framework of English Learning -- English rabbit learning notes (1)
- On the first day of clock in, click to open a surprise, and the switch statement is explained in detail
- 开源的网易云音乐API项目都是怎么实现的?
- 将ue4程序嵌入qt界面显示
- After working for 10 years, I changed to a programmer. Now I'm 35 + years old and I'm not anxious
- C language_ Double create, pre insert, post insert, traverse, delete
- UWA Pipeline 2.2.1 版本更新说明
猜你喜欢
ROS学习_基础
Office doc add in - Online CS
[brush questions] how can we correctly meet the interview?
Huawei equipment configuration ospf-bgp linkage
AI on the cloud makes earth science research easier
Supporting title of the book from 0 to 1: ctfer's growth road (Zhou Geng)
Is it difficult for girls to learn software testing? The threshold for entry is low, and learning is relatively simple
ROS2安装及基础知识介绍
女生学软件测试难不难 入门门槛低,学起来还是比较简单的
Leetcode daily question (971. flip binary tree to match preorder traversal)
随机推荐
[advanced software testing step 1] basic knowledge of automated testing
Redis Foundation
mysql的基础命令
The registration password of day 239/300 is 8~14 alphanumeric and punctuation, and at least 2 checks are included
软件测试外包到底要不要去?三年真实外包感受告诉你
【Hot100】739. Daily temperature
Delete external table source data
Day 246/300 SSH connection prompt "remote host identification has changed!"
[ 英語 ] 語法重塑 之 動詞分類 —— 英語兔學習筆記(2)
【Hot100】739. 每日温度
(practice C language every day) reverse linked list II
Proteus -- Serial Communication parity flag mode
机器人类专业不同层次院校课程差异性简述-ROS1/ROS2-
Simple use of MySQL database: add, delete, modify and query
将ue4程序嵌入qt界面显示
C language_ Double create, pre insert, post insert, traverse, delete
Attributeerror: can 't get attribute' sppf 'on < module' models. Common 'from' / home / yolov5 / Models / comm
从autojs到冰狐智能辅助的心里历程
Apache dolphin scheduler source code analysis (super detailed)
Cif10 actual combat (resnet18)