当前位置:网站首页>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;
}
};
边栏推荐
- Entity Developer数据库应用程序的开发
- Compile, connect -- notes-2
- 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
- Map of mL: Based on the adult census income two classification prediction data set (whether the predicted annual income exceeds 50K), use the map value to realize the interpretable case of xgboost mod
- 数据仓库建设思维导图
- Cif10 actual combat (resnet18)
- 万丈高楼平地起,每个API皆根基
- Leetcode - 152 product maximum subarray
- 【Hot100】739. 每日温度
- Oracle数据库11gr2使用tde透明数据加密报错ora28353,如果运行关闭wallet会报错ora28365,运行打开wallet就报错ora28353无法打开wallet
猜你喜欢

Windows Server 2016 standard installing Oracle

LeetCode 78:子集

18.多级页表与快表

Chapter 7 - thread pool of shared model

BUU的MISC(不定时更新)

Simple use of MySQL database: add, delete, modify and query

How to find a medical software testing institution? First flight software evaluation is an expert

前缀和数组系列

Database basics exercise part 2

19. Actual memory management of segment page combination
随机推荐
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
Latex文字加颜色的三种办法
Setting and using richview trvstyle template style
前缀和数组系列
【软件测试进阶第1步】自动化测试基础知识
Uncaught typeerror: cannot red properties of undefined (reading 'beforeeach') solution
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
开源的网易云音乐API项目都是怎么实现的?
Oracle数据库11gr2使用tde透明数据加密报错ora28353,如果运行关闭wallet会报错ora28365,运行打开wallet就报错ora28353无法打开wallet
万丈高楼平地起,每个API皆根基
升级版手机检测微信工具小程序源码-支持多种流量主模式
NFT on fingertips | evaluate ambire on G2, and have the opportunity to obtain limited edition collections
Raspberry pie serial port login and SSH login methods
LeetCode 78:子集
Arduino tutorial - Simon games
Bitcoinwin (BCW): 借贷平台Celsius隐瞒亏损3.5万枚ETH 或资不抵债
【Hot100】739. 每日温度
接口自动化测试框架:Pytest+Allure+Excel
Development of entity developer database application
UNIPRO Gantt chart "first experience": multi scene exploration behind attention to details