-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.java
90 lines (87 loc) · 2.9 KB
/
Solution.java
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
class Solution {
private static final int[] DIRS = {-1, 0, 1, 0, -1};
private List<Integer> c = new ArrayList<>();
private List<List<Integer>> areas = new ArrayList<>();
private List<Set<Integer>> boundaries = new ArrayList<>();
private int[][] infected;
private boolean[][] vis;
private int m;
private int n;
public int containVirus(int[][] isInfected) {
infected = isInfected;
m = infected.length;
n = infected[0].length;
vis = new boolean[m][n];
int ans = 0;
while (true) {
for (boolean[] row : vis) {
Arrays.fill(row, false);
}
c.clear();
areas.clear();
boundaries.clear();
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (infected[i][j] == 1 && !vis[i][j]) {
c.add(0);
areas.add(new ArrayList<>());
boundaries.add(new HashSet<>());
dfs(i, j);
}
}
}
if (areas.isEmpty()) {
break;
}
int idx = max(boundaries);
ans += c.get(idx);
for (int t = 0; t < areas.size(); ++t) {
if (t == idx) {
for (int v : areas.get(t)) {
int i = v / n, j = v % n;
infected[i][j] = -1;
}
} else {
for (int v : areas.get(t)) {
int i = v / n, j = v % n;
for (int k = 0; k < 4; ++k) {
int x = i + DIRS[k], y = j + DIRS[k + 1];
if (x >= 0 && x < m && y >= 0 && y < n && infected[x][y] == 0) {
infected[x][y] = 1;
}
}
}
}
}
}
return ans;
}
private int max(List<Set<Integer>> boundaries) {
int idx = 0;
int mx = boundaries.get(0).size();
for (int i = 1; i < boundaries.size(); ++i) {
int t = boundaries.get(i).size();
if (mx < t) {
mx = t;
idx = i;
}
}
return idx;
}
private void dfs(int i, int j) {
vis[i][j] = true;
int idx = areas.size() - 1;
areas.get(idx).add(i * n + j);
for (int k = 0; k < 4; ++k) {
int x = i + DIRS[k], y = j + DIRS[k + 1];
if (x >= 0 && x < m && y >= 0 && y < n) {
if (infected[x][y] == 1 && !vis[x][y]) {
dfs(x, y);
} else if (infected[x][y] == 0) {
c.set(idx, c.get(idx) + 1);
boundaries.get(idx).add(x * n + y);
}
}
}
}
}