LC.P749[隔离病毒]

方法一:BFS

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
class Solution {

int[][] isInfected;
int m, n, ans;
static int[][] dirs = new int[][]{{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
boolean[][] visited;

public int containVirus(int[][] isInfected) {
this.isInfected = isInfected;
m = isInfected.length;
n = isInfected[0].length;
while (true) {
int cnt = getCnt();
if (cnt == 0) break;
ans += cnt;
}
return ans;
}

private int getCnt() {
visited = new boolean[m][n];
int max = 0, ans = 0;
// list1:每个连通块的点集 list2:每个连通块的候选感染点集
List<Set<Integer>> list1 = new ArrayList<>(), list2 = new ArrayList<>();
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (isInfected[i][j] == 1 && !visited[i][j]) {
// set1:当前连通块的点集 set2:当前连通块的候选感染点集
Set<Integer> set1 = new HashSet<>(), set2 = new HashSet<>();
int b = search(i, j, set1, set2), a = set2.size();
if (a > max) {
max = a;
ans = b;
}
list1.add(set1);
list2.add(set2);
}
}
}
for (int i = 0; i < list2.size(); ++i) {
for (int loc : list2.get(i).size() == max ? list1.get(i) : list2.get(i)) {
int x = loc / n, y = loc % n;
isInfected[x][y] = list2.get(i).size() == max ? -1 : 1;
}
}
return ans;
}

private int search(int i, int j, Set<Integer> set1, Set<Integer> set2) {
int ans = 0;
Deque<int[]> queue = new ArrayDeque<>();
visited[i][j] = true;
queue.offer(new int[]{i, j});
set1.add(i * n + j);
while (!queue.isEmpty()) {
int[] cur = queue.poll();
int x = cur[0], y = cur[1];
for (int[] dir : dirs) {
int nx = x + dir[0], ny = y + dir[1], loc = nx * n + ny;
if (nx < 0 || nx >= m || ny < 0 || ny >= n || visited[nx][ny]) continue;
if (isInfected[nx][ny] == 1) {
set1.add(loc);
visited[nx][ny] = true;
queue.offer(new int[]{nx, ny});
} else if (isInfected[nx][ny] == 0) {
set2.add(loc);
++ans;
}
}
}
return ans;
}
}
  • 时间复杂度:$O((m + n) \times mn)$
  • 空间复杂度:$(mn)$