25
25
<strong >输出:</strong > 10
26
26
<strong >解释:</strong >一共有两块被病毒感染的区域。
27
27
在第一天,添加 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 " />
29
29
第二天,在右侧添加 5 个墙来隔离病毒区域。此时病毒已经被完全控制住了。
30
30
<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 ;" />
31
31
</pre >
@@ -86,8 +86,8 @@ class Solution:
86
86
def containVirus (self , isInfected : List[List[int ]]) -> int :
87
87
def dfs (i , j ):
88
88
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 ]]:
91
91
x, y = i + a, j + b
92
92
if 0 <= x < m and 0 <= y < n:
93
93
if isInfected[x][y] == 1 and not vis[x][y]:
@@ -106,7 +106,7 @@ class Solution:
106
106
for i, row in enumerate (isInfected):
107
107
for j, v in enumerate (row):
108
108
if v == 1 and not vis[i][j]:
109
- areas.append(set () )
109
+ areas.append([] )
110
110
boundaries.append(set ())
111
111
c.append(0 )
112
112
dfs(i, j)
@@ -120,7 +120,7 @@ class Solution:
120
120
isInfected[i][j] = - 1
121
121
else :
122
122
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 ]]:
124
124
x, y = i + a, j + b
125
125
if 0 <= x < m and 0 <= y < n and isInfected[x][y] == 0 :
126
126
isInfected[x][y] = 1
@@ -135,33 +135,37 @@ class Solution:
135
135
class Solution {
136
136
private static final int [] DIRS = {- 1 , 0 , 1 , 0 , - 1 };
137
137
private List<Integer > c = new ArrayList<> ();
138
- private List<Set <Integer > > areas = new ArrayList<> ();
138
+ private List<List <Integer > > areas = new ArrayList<> ();
139
139
private List<Set<Integer > > boundaries = new ArrayList<> ();
140
- private int [][] isInfected;
140
+ private int [][] infected;
141
+ private boolean [][] vis;
141
142
private int m;
142
143
private int n;
143
144
144
145
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];
148
150
int ans = 0 ;
149
151
while (true ) {
150
- boolean [][] vis = new boolean [m][n];
151
- areas. clear();
152
+ for (boolean [] row : vis) {
153
+ Arrays . fill(row, false );
154
+ }
152
155
c. clear();
156
+ areas. clear();
153
157
boundaries. clear();
154
158
for (int i = 0 ; i < m; ++ i) {
155
159
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]) {
159
161
c. add(0 );
160
- dfs(i, j, vis);
162
+ areas. add(new ArrayList<> ());
163
+ boundaries. add(new HashSet<> ());
164
+ dfs(i, j);
161
165
}
162
166
}
163
167
}
164
- if (boundaries . isEmpty()) {
168
+ if (areas . isEmpty()) {
165
169
break ;
166
170
}
167
171
int idx = max(boundaries);
@@ -170,21 +174,20 @@ class Solution {
170
174
if (t == idx) {
171
175
for (int v : areas. get(t)) {
172
176
int i = v / n, j = v % n;
173
- isInfected [i][j] = - 1 ;
177
+ infected [i][j] = - 1 ;
174
178
}
175
179
} else {
176
180
for (int v : areas. get(t)) {
177
181
int i = v / n, j = v % n;
178
182
for (int k = 0 ; k < 4 ; ++ k) {
179
183
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 ;
182
186
}
183
187
}
184
188
}
185
189
}
186
190
}
187
-
188
191
}
189
192
return ans;
190
193
}
@@ -202,16 +205,16 @@ class Solution {
202
205
return idx;
203
206
}
204
207
205
- private void dfs (int i , int j , boolean [][] vis ) {
208
+ private void dfs (int i , int j ) {
206
209
vis[i][j] = true ;
207
210
int idx = areas. size() - 1 ;
208
211
areas. get(idx). add(i * n + j);
209
212
for (int k = 0 ; k < 4 ; ++ k) {
210
213
int x = i + DIRS [k], y = j + DIRS [k + 1 ];
211
214
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 ) {
215
218
c. set(idx, c. get(idx) + 1 );
216
219
boundaries. get(idx). add(x * n + y);
217
220
}
@@ -221,6 +224,110 @@ class Solution {
221
224
}
222
225
```
223
226
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
+
224
331
### ** Go**
225
332
226
333
``` go
0 commit comments