[LeetCode] Contain Virus

749. Contain Virus

A virus is spreading rapidly, and your task is to quarantine the infected area by installing walls.

The world is modeled as an m x n binary grid isInfected, where isInfected[i][j] == 0 represents uninfected cells, and isInfected[i][j] == 1 represents cells contaminated with the virus. A wall (and only one wall) can be installed between any two 4-directionally adjacent cells, on the shared boundary.

Every night, the virus spreads to all neighboring cells in all four directions unless blocked by a wall. Resources are limited. Each day, you can install walls around only one region (i.e., the affected area (continuous block of infected cells) that threatens the most uninfected cells the following night). There will never be a tie.

Return the number of walls used to quarantine all the infected regions. If the world will become fully infected, return the number of walls used.

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
class Solution {
int n, m;
int dy[4]{-1,0,1,0}, dx[4]{0,1,0,-1};

pair<int, int> bfs(int sy, int sx, vector<vector<int>>& G, vector<vector<int>>& vis, int cover) {
vis[sy][sx] = cover;
int area = 0, wall = 0;
queue<pair<int, int>> q;
q.push({sy, sx});
while(!q.empty()) {
auto [y, x] = q.front(); q.pop();
for(int i = 0; i < 4; i++) {
int ny = y + dy[i], nx = x + dx[i];
if(0 <= ny and ny < n and 0 <= nx and nx < m) {
if(G[ny][nx] == 1 and vis[ny][nx] != cover) {
vis[ny][nx] = cover;
q.push({ny, nx});
} else if(G[ny][nx] == 0) {
wall++;
if(vis[ny][nx] != cover) {
vis[ny][nx] = cover;
area++;
}
}
}
}
}
return {area, wall};
}

void floodfill(int y, int x, vector<vector<int>>& G) {
if(0 <= y and y < n and 0 <= x and x < m and G[y][x] == 1) {
G[y][x] = -1;
floodfill(y + 1, x, G);
floodfill(y - 1, x, G);
floodfill(y, x + 1, G);
floodfill(y, x - 1, G);
}
}
array<int, 3> getMaxArea(vector<vector<int>>& G) {
int y = -1, x = -1, wall = 0, cells = 0, cover = 2;
vector<vector<int>> vis(n, vector<int>(m, 0));

for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
if(vis[i][j]) continue;
if(G[i][j] <= 0) continue;
auto [a, w] = bfs(i, j, G, vis, cover++);
if(a > cells) {
cells = a;
y = i, x = j;
wall = w;
}
}
}

return {y, x, wall};
}

void spread(vector<vector<int>>& G) {
vector<vector<bool>> vis(n, vector<bool>(m, false));
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
if(vis[i][j]) continue;
if(G[i][j] <= 0) continue;
for(int k = 0; k < 4; k++) {
int ny = i + dy[k], nx = j + dx[k];
if(0 <= ny and ny < n and 0 <= nx and nx < m and !vis[ny][nx] and G[ny][nx] == 0) {
vis[ny][nx] = true;
G[ny][nx] = 1;
}
}
}
}
}
int helper(vector<vector<int>>& G) {
auto [y, x, wall] = getMaxArea(G);
if(wall == 0) return 0;
floodfill(y, x, G);

spread(G);

return wall;
}
public:
int containVirus(vector<vector<int>>& isInfected) {
n = isInfected.size(), m = isInfected[0].size();
int res = 0;
while(true) {
int count = helper(isInfected);
if(!count) break;
res += count;
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/06/01/PS/LeetCode/contain-virus/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.