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.
Example 1:
Input: isInfected = [[0,1,0,0,0,0,0,1],[0,1,0,0,0,0,0,1],[0,0,0,0,0,0,0,1],[0,0,0,0,0,0,0,0]] Output: 10 Explanation: There are 2 contaminated regions. On the first day, add 5 walls to quarantine the viral region on the left. The board after the virus spreads is: On the second day, add 5 walls to quarantine the viral region on the right. The virus is fully contained.
Example 2:
Input: isInfected = [[1,1,1],[1,0,1],[1,1,1]] Output: 4 Explanation: Even though there is only one cell saved, there are 4 walls built. Notice that walls are only built on the shared boundary of two different cells.
Example 3:
Input: isInfected = [[1,1,1,0,0,0,0,0,0],[1,0,1,0,1,1,1,1,1],[1,1,1,0,0,0,0,0,0]] Output: 13 Explanation: The region on the left only builds two new walls.
Constraints:
m == isInfected.length
n == isInfected[i].length
1 <= m, n <= 50
isInfected[i][j]
is either0
or1
.- There is always a contiguous viral region throughout the described process that will infect strictly more uncontaminated squares in the next round.
Companies:
Accolite, Bloomberg
Related Topics:
Array, Depth-First Search, Breadth-First Search, Matrix, Simulation
- Enumerate the components, find the one with the greatest infection
- Compute the edge count of this component, and mark this component as locked.
- Apply the infection of other components. Components might merge together.
- Repeat until there is nothing to infect or all the components are locked.
// OJ: https://leetcode.com/problems/contain-virus/
// Author: github.com/lzl124631x
// Time: O()
// Space: O()
class UnionFind {
vector<int> id;
public:
UnionFind(int n) : id(n) {
iota(begin(id), end(id), 0);
}
int find(int a) {
return id[a] == a ? a : (id[a] = find(id[a]));
}
void connect(int a, int b) {
id[find(a)] = find(b);
}
bool connected(int a, int b) {
return find(a) == find(b);
}
};
class Solution {
public:
int containVirus(vector<vector<int>>& A) {
int M = A.size(), N = A[0].size(), dirs[4][2] = {{0,1},{0,-1},{1,0},{-1,0}}, ans = 0;
auto getKey = [&](int x, int y) { return x * N + y; };
UnionFind uf(M * N);
auto getState = [&](int x, int y) {
int k = uf.find(getKey(x, y));
return A[k / N][k % N];
};
while (true) {
vector<int> cnt(M * N); // count of new infection
for (int i = 0; i < M; ++i) { // connect infected components
for (int j = 0; j < N; ++j) {
if (A[i][j] != 1) continue;
for (auto &[dx, dy] : dirs) {
int x = i + dx, y = j + dy;
if (x < 0 || x >= M || y < 0 || y >= N || A[x][y] != 1) continue;
uf.connect(getKey(i, j), getKey(x, y));
}
}
}
for (int i = 0; i < M; ++i) { // get the greatest infection region
for (int j = 0; j < N; ++j) {
if (getState(i, j) != 0) continue; // for each uninfected cell, check which infected regions can infect it.
vector<int> infect;
for (auto &[dx, dy] : dirs) {
int x = i + dx, y = j + dy;
if (x < 0 || x >= M || y < 0 || y >= N || A[x][y] != 1) continue;
infect.push_back(uf.find(getKey(x, y)));
}
sort(begin(infect), end(infect));
infect.erase(unique(begin(infect), end(infect)), end(infect));
for (int k : infect) cnt[k]++;
}
}
int maxInfect = -1;
for (int i = 0; i < M * N; ++i) {
int x = i / N, y = i % N;
if (A[x][y] == 1 && (maxInfect == -1 || cnt[uf.find(i)] > cnt[maxInfect])) maxInfect = uf.find(i);
}
if (maxInfect == -1) break;
for (int i = 0; i < M; ++i) { // build walls
for (int j = 0; j < N; ++j) {
if (uf.find(getKey(i, j)) != maxInfect) continue; // Only visit the cells in the maxInfect region
A[i][j] = 2; // lock this region
for (auto &[dx, dy] : dirs) {
int x = i + dx, y = j + dy;
if (x < 0 || x >= M || y < 0 || y >= N || A[x][y] != 0) continue;
++ans;
}
}
}
for (int i = 0; i < M; ++i) { // infect new regions
for (int j = 0; j < N; ++j) {
if (A[i][j] != 0) continue; // For each uninfected cells, test if it should be infected.
for (auto &[dx, dy] : dirs) {
int x = i + dx, y = j + dy;
if (x < 0 || x >= M || y < 0 || y >= N || A[x][y] != 1) continue;
uf.connect(getKey(i, j), getKey(x, y)); // connect this uninfected region with the infected region
}
}
}
for (int i = 0; i < M; ++i) {
for (int j = 0; j < N; ++j) {
if (A[i][j] == 0 && getState(i, j) == 1) A[i][j] = 1; // If this cell itself is not infected, but it should be, mark it as infected
}
}
}
return ans;
}
};