-
Notifications
You must be signed in to change notification settings - Fork 481
/
0827.cpp
46 lines (43 loc) · 1.39 KB
/
0827.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class Solution
{
public:
int largestIsland(vector<vector<int>>& g)
{
vector<int> data = {0, 0};
for (auto i = 0; i < g.size(); ++i)
for (auto j = 0; j < g[i].size(); ++j)
if (g[i][j] == 1) data.push_back(dfs(i, j, data.size(), g));
int res = 0;
for (auto i = 0; i < g.size(); ++i)
{
for (auto j = 0; j < g[i].size(); ++j)
{
if (g[i][j] == 0)
{
unordered_set<int> s = {check(i + 1, j, g), check(i - 1, j, g), check(i, j + 1, g), check(i, j - 1, g)};
res = max(res, 1 + accumulate(begin(s), end(s), 0, [&](int a, int b) {
return a + data[b];
}));
}
}
}
return res == 0 ? g.size() * g[0].size() : res;
}
private:
int dire[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int check(int i, int j, vector<vector<int>>& g)
{
return (i < 0 || j < 0 || i >= g.size() || j >= g[0].size()) ? 0 : g[i][j];
}
int dfs(int i, int j, int color, vector<vector<int>>& g)
{
if (check(i, j, g) != 1) return 0;
g[i][j] = color;
int res = 1;
for (int x = 0; x < 4; x++)
{
res += dfs(dire[x][0] + i, dire[x][1] + j, color, g);
}
return res;
}
};