Skip to content

Commit 7d00ba9

Browse files
committed
feat: add solutions to lc problem: No.0749
No.0749.Contain Virus
1 parent 5870d78 commit 7d00ba9

File tree

5 files changed

+389
-73
lines changed

5 files changed

+389
-73
lines changed

solution/0700-0799/0749.Contain Virus/README.md

Lines changed: 132 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
<strong>输出:</strong> 10
2626
<strong>解释:</strong>一共有两块被病毒感染的区域。
2727
在第一天,添加 5 墙隔离病毒区域的左侧。病毒传播后的状态是:
28-
<img src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0700-0799/0749.Contain%20Virus/images/653" />
28+
<img src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0700-0799/0749.Contain%20Virus/images/virus12edited-grid.jpg" />
2929
第二天,在右侧添加 5 个墙来隔离病毒区域。此时病毒已经被完全控制住了。
3030
<img src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0700-0799/0749.Contain%20Virus/images/virus13edited-grid.jpg" style="height: 261px; width: 500px;" />
3131
</pre>
@@ -86,8 +86,8 @@ class Solution:
8686
def containVirus(self, isInfected: List[List[int]]) -> int:
8787
def dfs(i, j):
8888
vis[i][j] = True
89-
areas[-1].add((i, j))
90-
for a, b in [[0, -1], [0, 1], [1, 0], [-1, 0]]:
89+
areas[-1].append((i, j))
90+
for a, b in [[0, -1], [0, 1], [-1, 0], [1, 0]]:
9191
x, y = i + a, j + b
9292
if 0 <= x < m and 0 <= y < n:
9393
if isInfected[x][y] == 1 and not vis[x][y]:
@@ -106,7 +106,7 @@ class Solution:
106106
for i, row in enumerate(isInfected):
107107
for j, v in enumerate(row):
108108
if v == 1 and not vis[i][j]:
109-
areas.append(set())
109+
areas.append([])
110110
boundaries.append(set())
111111
c.append(0)
112112
dfs(i, j)
@@ -120,7 +120,7 @@ class Solution:
120120
isInfected[i][j] = -1
121121
else:
122122
for i, j in area:
123-
for a, b in [[0, -1], [0, 1], [1, 0], [-1, 0]]:
123+
for a, b in [[0, -1], [0, 1], [-1, 0], [1, 0]]:
124124
x, y = i + a, j + b
125125
if 0 <= x < m and 0 <= y < n and isInfected[x][y] == 0:
126126
isInfected[x][y] = 1
@@ -135,33 +135,37 @@ class Solution:
135135
class Solution {
136136
private static final int[] DIRS = {-1, 0, 1, 0, -1};
137137
private List<Integer> c = new ArrayList<>();
138-
private List<Set<Integer>> areas = new ArrayList<>();
138+
private List<List<Integer>> areas = new ArrayList<>();
139139
private List<Set<Integer>> boundaries = new ArrayList<>();
140-
private int[][] isInfected;
140+
private int[][] infected;
141+
private boolean[][] vis;
141142
private int m;
142143
private int n;
143144

144145
public int containVirus(int[][] isInfected) {
145-
m = isInfected.length;
146-
n = isInfected[0].length;
147-
this.isInfected = isInfected;
146+
infected = isInfected;
147+
m = infected.length;
148+
n = infected[0].length;
149+
vis = new boolean[m][n];
148150
int ans = 0;
149151
while (true) {
150-
boolean[][] vis = new boolean[m][n];
151-
areas.clear();
152+
for (boolean[] row : vis) {
153+
Arrays.fill(row, false);
154+
}
152155
c.clear();
156+
areas.clear();
153157
boundaries.clear();
154158
for (int i = 0; i < m; ++i) {
155159
for (int j = 0; j < n; ++j) {
156-
if (isInfected[i][j] == 1 && !vis[i][j]) {
157-
areas.add(new HashSet<>());
158-
boundaries.add(new HashSet<>());
160+
if (infected[i][j] == 1 && !vis[i][j]) {
159161
c.add(0);
160-
dfs(i, j, vis);
162+
areas.add(new ArrayList<>());
163+
boundaries.add(new HashSet<>());
164+
dfs(i, j);
161165
}
162166
}
163167
}
164-
if (boundaries.isEmpty()) {
168+
if (areas.isEmpty()) {
165169
break;
166170
}
167171
int idx = max(boundaries);
@@ -170,21 +174,20 @@ class Solution {
170174
if (t == idx) {
171175
for (int v : areas.get(t)) {
172176
int i = v / n, j = v % n;
173-
isInfected[i][j] = -1;
177+
infected[i][j] = -1;
174178
}
175179
} else {
176180
for (int v : areas.get(t)) {
177181
int i = v / n, j = v % n;
178182
for (int k = 0; k < 4; ++k) {
179183
int x = i + DIRS[k], y = j + DIRS[k + 1];
180-
if (x >= 0 && x < m && y >= 0 && y < n && isInfected[x][y] == 0) {
181-
isInfected[x][y] = 1;
184+
if (x >= 0 && x < m && y >= 0 && y < n && infected[x][y] == 0) {
185+
infected[x][y] = 1;
182186
}
183187
}
184188
}
185189
}
186190
}
187-
188191
}
189192
return ans;
190193
}
@@ -202,16 +205,16 @@ class Solution {
202205
return idx;
203206
}
204207

205-
private void dfs(int i, int j, boolean[][] vis) {
208+
private void dfs(int i, int j) {
206209
vis[i][j] = true;
207210
int idx = areas.size() - 1;
208211
areas.get(idx).add(i * n + j);
209212
for (int k = 0; k < 4; ++k) {
210213
int x = i + DIRS[k], y = j + DIRS[k + 1];
211214
if (x >= 0 && x < m && y >= 0 && y < n) {
212-
if (isInfected[x][y] == 1 && !vis[x][y]) {
213-
dfs(x, y, vis);
214-
} else if (isInfected[x][y] == 0) {
215+
if (infected[x][y] == 1 && !vis[x][y]) {
216+
dfs(x, y);
217+
} else if (infected[x][y] == 0) {
215218
c.set(idx, c.get(idx) + 1);
216219
boundaries.get(idx).add(x * n + y);
217220
}
@@ -221,6 +224,110 @@ class Solution {
221224
}
222225
```
223226

227+
### **C++**
228+
229+
```cpp
230+
class Solution {
231+
public:
232+
const vector<int> dirs = {-1, 0, 1, 0, -1};
233+
vector<int> c;
234+
vector<vector<int>> areas;
235+
vector<unordered_set<int>> boundaries;
236+
vector<vector<int>> infected;
237+
vector<vector<bool>> vis;
238+
int m;
239+
int n;
240+
241+
int containVirus(vector<vector<int>>& isInfected) {
242+
infected = isInfected;
243+
m = infected.size();
244+
n = infected[0].size();
245+
vis.assign(m, vector<bool>(n));
246+
int ans = 0;
247+
while (1)
248+
{
249+
for (int i = 0; i < m; ++i) for (int j = 0; j < n; ++j) vis[i][j] = false;
250+
c.clear();
251+
areas.clear();
252+
boundaries.clear();
253+
for (int i = 0; i < m; ++i)
254+
{
255+
for (int j = 0; j < n; ++j)
256+
{
257+
if (infected[i][j] == 1 && !vis[i][j])
258+
{
259+
c.push_back(0);
260+
areas.push_back({});
261+
boundaries.push_back({});
262+
dfs(i, j);
263+
}
264+
}
265+
}
266+
if (areas.empty()) break;
267+
int idx = getMax();
268+
ans += c[idx];
269+
for (int t = 0; t < areas.size(); ++t)
270+
{
271+
if (t == idx)
272+
{
273+
for (int v : areas[t])
274+
{
275+
int i = v / n, j = v % n;
276+
infected[i][j] = -1;
277+
}
278+
}
279+
else
280+
{
281+
for (int v : areas[t])
282+
{
283+
int i = v / n, j = v % n;
284+
for (int k = 0; k < 4; ++k)
285+
{
286+
int x = i + dirs[k], y = j + dirs[k + 1];
287+
if (x >= 0 && x < m && y >= 0 && y < n && infected[x][y] == 0) infected[x][y] = 1;
288+
}
289+
}
290+
}
291+
}
292+
}
293+
return ans;
294+
}
295+
296+
int getMax() {
297+
int idx = 0;
298+
int mx = boundaries[0].size();
299+
for (int i = 1; i < boundaries.size(); ++i)
300+
{
301+
int t = boundaries[i].size();
302+
if (mx < t)
303+
{
304+
mx = t;
305+
idx = i;
306+
}
307+
}
308+
return idx;
309+
}
310+
311+
void dfs(int i, int j) {
312+
vis[i][j] = true;
313+
areas.back().push_back(i * n + j);
314+
for (int k = 0; k < 4; ++k)
315+
{
316+
int x = i + dirs[k], y = j + dirs[k + 1];
317+
if (x >= 0 && x < m && y >= 0 && y < n)
318+
{
319+
if (infected[x][y] == 1 && !vis[x][y]) dfs(x, y);
320+
else if (infected[x][y] == 0)
321+
{
322+
c.back() += 1;
323+
boundaries.back().insert(x * n + y);
324+
}
325+
}
326+
}
327+
}
328+
};
329+
```
330+
224331
### **Go**
225332

226333
```go

0 commit comments

Comments
 (0)