当前位置:网站首页>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;
}
};
边栏推荐
- 【刷题】怎么样才能正确的迎接面试?
- Briefly describe the differences between indexes, primary keys, unique indexes, and joint indexes in mysql, and how they affect the performance of the database (in terms of reading and writing)
- 【服务器数据恢复】IBM服务器raid5两块硬盘离线数据恢复案例
- 19. Actual memory management of segment page combination
- [some special grammars about C]
- Uncaught TypeError: Cannot red propertites of undefined(reading ‘beforeEach‘)解决方案
- L'Ia dans les nuages rend la recherche géoscientifique plus facile
- idea控制台彩色日志
- leetcode1020. 飞地的数量(中等)
- 1189. Maximum number of "balloons"
猜你喜欢
Cif10 actual combat (resnet18)
Reflex WMS medium level series 3: display shipped replaceable groups
因高额网络费用,Arbitrum 奥德赛活动暂停,Nitro 发行迫在眉睫
AI on the cloud makes earth science research easier
AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models.common‘ from ‘/home/yolov5/models/comm
Bitcoinwin (BCW): the lending platform Celsius conceals losses of 35000 eth or insolvency
首发织梦百度推送插件全自动收录优化seo收录模块
Fedora/REHL 安装 semanage
ROS2安装及基础知识介绍
Hydra common commands
随机推荐
同事上了个厕所,我帮产品妹子轻松完成BI数据产品顺便得到奶茶奖励
hydra常用命令
Brief introduction to the curriculum differences of colleges and universities at different levels of machine human major -ros1/ros2-
18. Multi level page table and fast table
Apache DolphinScheduler源码分析(超详细)
The psychological process from autojs to ice fox intelligent assistance
Misc of BUU (update from time to time)
作者已死?AI正用藝術征服人類
UDP攻击是什么意思?UDP攻击防范措施
配置树莓派接入网络
作者已死?AI正用艺术征服人类
UWA pipeline version 2.2.1 update instructions
C language_ Double create, pre insert, post insert, traverse, delete
26岁从财务转行软件测试,4年沉淀我已经是25k的测开工程师...
At the age of 26, I changed my career from finance to software testing. After four years of precipitation, I have been a 25K Test Development Engineer
leetcode1020. 飞地的数量(中等)
idea控制台彩色日志
医疗软件检测机构怎么找,一航软件测评是专家
树莓派3B更新vim
Zhongqing reading news