当前位置:网站首页>leetcode1020. Number of enclaves (medium)
leetcode1020. Number of enclaves (medium)
2022-07-06 07:03:00 【Heavy garbage】



Ideas :dfs/bfs The template questions
Follow leetcode130. The surrounding area ( secondary ) similar
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;
}
};
Train of thought three : Union checking set
Specific ideas : Set up a virtual land :n*m, When initializing, the land on the edge is connected with this cell . Then put the... In the figure 1 Construct several connected components .
ans: Cell is 1 And with the n*m Of findfather() Equal number .
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;
}
};
边栏推荐
- Simple use of MySQL database: add, delete, modify and query
- Day 245/300 JS foreach data cannot be updated to the object after multi-layer nesting
- 【每日一题】729. 我的日程安排表 I
- 【刷题】怎么样才能正确的迎接面试?
- 1189. Maximum number of "balloons"
- Reflex WMS medium level series 3: display shipped replaceable groups
- Short video, more and more boring?
- Fedora/rehl installation semanage
- UniPro甘特图“初体验”:关注细节背后的多场景探索
- Interface automation test framework: pytest+allure+excel
猜你喜欢
![[daily question] 729 My schedule I](/img/6b/a9fef338ac09caafe628023f066e1f.png)
[daily question] 729 My schedule I

Apache dolphin scheduler source code analysis (super detailed)

leetcode704. 二分查找(查找某个元素,简单,不同写法)

漏了监控:Zabbix对Eureka instance状态监控

Database basics exercise part 2

After working for 10 years, I changed to a programmer. Now I'm 35 + years old and I'm not anxious

Reflex WMS中阶系列3:显示已发货可换组

Short video, more and more boring?

微信脑力比拼答题小程序_支持流量主带最新题库文件

Bitcoinwin (BCW): 借贷平台Celsius隐瞒亏损3.5万枚ETH 或资不抵债
随机推荐
Interface automation test framework: pytest+allure+excel
将ue4程序嵌入qt界面显示
Pallet management in SAP SD delivery process
AI on the cloud makes earth science research easier
Embed UE4 program into QT interface display
BIO模型实现多人聊天
编译,连接 -- 笔记 -2
Windows Server 2016 standard installing Oracle
Raspberry pie 3B update VIM
19. Actual memory management of segment page combination
The difference between get and post request types
首发织梦百度推送插件全自动收录优化seo收录模块
Day 245/300 JS foreach data cannot be updated to the object after multi-layer nesting
[brush questions] how can we correctly meet the interview?
PCL实现选框裁剪点云
1189. Maximum number of "balloons"
L'Ia dans les nuages rend la recherche géoscientifique plus facile
Missing monitoring: ZABBIX monitors the status of Eureka instance
【软件测试进阶第1步】自动化测试基础知识
指尖上的 NFT|在 G2 上评价 Ambire,有机会获得限量版收藏品