forked from sachuverma/DataStructures-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01. Making A Large Island.cpp
154 lines (126 loc) · 3.45 KB
/
01. Making A Large Island.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
Making A Large Island
=====================
You are given an n x n binary matrix grid. You are allowed to change at most one 0 to be 1.
Return the size of the largest island in grid after applying this operation.
An island is a 4-directionally connected group of 1s.
Example 1:
Input: grid = [[1,0],[0,1]]
Output: 3
Explanation: Change one 0 to 1 and connect two 1s, then we get an island with area = 3.
Example 2:
Input: grid = [[1,1],[1,0]]
Output: 4
Explanation: Change the 0 to 1 and make the island bigger, only one island with area = 4.
Example 3:
Input: grid = [[1,1],[1,1]]
Output: 4
Explanation: Can't change any 0 to 1, only one island with area = 4.
Constraints:
n == grid.length
n == grid[i].length
1 <= n <= 500
grid[i][j] is either 0 or 1.
*/
class Solution
{
public:
int dirs[4][2] = {
{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int bfs(vector<vector<int>> &grid, vector<vector<pair<int, int>>> &vis, int r, int c, int id)
{
int n = grid.size(), m = grid[0].size();
int ans = 0;
queue<pair<int, int>> q;
q.push({r, c});
vis[r][c].first = -1;
while (q.size())
{
auto curr = q.front();
q.pop();
int x = curr.first, y = curr.second;
ans++;
for (auto dir : dirs)
{
int nx = x + dir[0], ny = y + dir[1];
if (nx >= 0 && ny >= 0 && nx < n && ny < m && grid[nx][ny] == 1 && vis[nx][ny].first == 0)
{
vis[nx][ny].first = -1;
q.push({nx, ny});
}
}
}
q.push({r, c});
vis[r][c].first = ans;
vis[r][c].second = id;
while (q.size())
{
auto curr = q.front();
q.pop();
int x = curr.first, y = curr.second;
for (auto dir : dirs)
{
int nx = x + dir[0], ny = y + dir[1];
if (nx >= 0 && ny >= 0 && nx < n && ny < m && grid[nx][ny] == 1 && vis[nx][ny].first == -1)
{
vis[nx][ny].first = ans;
vis[nx][ny].second = id;
q.push({nx, ny});
}
}
}
return ans;
}
int largestIsland(vector<vector<int>> &grid)
{
int n = grid.size(), m = grid[0].size();
int ans = 0;
// { island_size, id }
vector<vector<pair<int, int>>> vis(n, vector<pair<int, int>>(m, {0, 0}));
int index_counter = 0;
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < m; ++j)
{
if (grid[i][j] == 1 && vis[i][j].second == 0)
{
index_counter++;
ans = max(ans, bfs(grid, vis, i, j, index_counter));
}
}
}
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < m; ++j)
{
if (grid[i][j] == 0)
{
int curr = 1;
unordered_set<int> taken_id;
if (i - 1 >= 0 && !taken_id.count(vis[i - 1][j].second))
{
taken_id.insert(vis[i - 1][j].second);
curr += vis[i - 1][j].first;
}
if (i + 1 < n && !taken_id.count(vis[i + 1][j].second))
{
taken_id.insert(vis[i + 1][j].second);
curr += vis[i + 1][j].first;
}
if (j - 1 >= 0 && !taken_id.count(vis[i][j - 1].second))
{
taken_id.insert(vis[i][j - 1].second);
curr += vis[i][j - 1].first;
}
if (j + 1 < m && !taken_id.count(vis[i][j + 1].second))
{
taken_id.insert(vis[i][j + 1].second);
curr += vis[i][j + 1].first;
}
ans = max(ans, curr);
}
}
}
return ans;
}
};