publicintContainVirus(int[][] isInfected) { int m = isInfected.Length, n = isInfected[0].Length; int ans = 0; while (true) { IList<ISet<int>> neighbors = new List<ISet<int>>(); IList<int> firewalls = new List<int>(); for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (isInfected[i][j] == 1) { Queue<Tuple<int, int>> queue = new Queue<Tuple<int, int>>(); queue.Enqueue(new Tuple<int, int>(i, j)); ISet<int> neighbor = new HashSet<int>(); int firewall = 0, idx = neighbors.Count + 1; isInfected[i][j] = -idx;
while (queue.Count > 0) { Tuple<int, int> tuple = queue.Dequeue(); int x = tuple.Item1, y = tuple.Item2; for (int d = 0; d < 4; ++d) { int nx = x + dirs[d][0], ny = y + dirs[d][1]; if (nx >= 0 && nx < m && ny >= 0 && ny < n) { if (isInfected[nx][ny] == 1) { queue.Enqueue(new Tuple<int, int>(nx, ny)); isInfected[nx][ny] = -idx; } elseif (isInfected[nx][ny] == 0) { ++firewall; neighbor.Add(GetHash(nx, ny)); } } } } neighbors.Add(neighbor); firewalls.Add(firewall); } } }
if (neighbors.Count == 0) { break; }
int maxIdx = 0; for (int i = 1; i < neighbors.Count; ++i) { if (neighbors[i].Count > neighbors[maxIdx].Count) { maxIdx = i; } } ans += firewalls[maxIdx]; for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (isInfected[i][j] < 0) { if (isInfected[i][j] != -maxIdx - 1) { isInfected[i][j] = 1; } else { isInfected[i][j] = 2; } } } } for (int i = 0; i < neighbors.Count; ++i) { if (i != maxIdx) { foreach (int val in neighbors[i]) { int x = val >> 16, y = val & ((1 << 16) - 1); isInfected[x][y] = 1; } } } if (neighbors.Count == 1) { break; } } return ans; }
classSolution: defcontainVirus(self, isInfected: List[List[int]]) -> int: dirs = [(-1, 0), (1, 0), (0, -1), (0, 1)] m, n = len(isInfected), len(isInfected[0]) ans = 0
whileTrue: neighbors, firewalls = list(), list() for i inrange(m): for j inrange(n): if isInfected[i][j] == 1: q = deque([(i, j)]) neighbor = set() firewall, idx = 0, len(neighbors) + 1 isInfected[i][j] = -idx
while q: x, y = q.popleft() for d inrange(4): nx, ny = x + dirs[d][0], y + dirs[d][1] if0 <= nx < m and0 <= ny < n: if isInfected[nx][ny] == 1: q.append((nx, ny)) isInfected[nx][ny] = -idx elif isInfected[nx][ny] == 0: firewall += 1 neighbor.add((nx, ny)) neighbors.append(neighbor) firewalls.append(firewall) ifnot neighbors: break
idx = 0 for i inrange(1, len(neighbors)): iflen(neighbors[i]) > len(neighbors[idx]): idx = i ans += firewalls[idx] for i inrange(m): for j inrange(n): if isInfected[i][j] < 0: if isInfected[i][j] != -idx - 1: isInfected[i][j] = 1 else: isInfected[i][j] = 2 for i, neighbor inenumerate(neighbors): if i != idx: for x, y in neighbor: isInfected[x][y] = 1 iflen(neighbors) == 1: break return ans
const dirs = [[-1, 0], [1, 0], [0, -1], [0, 1]]; var containVirus = function(isInfected) { const m = isInfected.length, n = isInfected[0].length; let ans = 0; while (true) { const neighbors = []; const firewalls = []; for (let i = 0; i < m; ++i) { for (let j = 0; j < n; ++j) { if (isInfected[i][j] === 1) { const queue = []; queue.push([i, j]); const neighbor = newSet(); let firewall = 0, idx = neighbors.length + 1; isInfected[i][j] = -idx;
while (queue.length > 0) { const arr = queue.shift(); let x = arr[0], y = arr[1]; for (let d = 0; d < 4; ++d) { let nx = x + dirs[d][0], ny = y + dirs[d][1]; if (nx >= 0 && nx < m && ny >= 0 && ny < n) { if (isInfected[nx][ny] === 1) { queue.push([nx, ny]); isInfected[nx][ny] = -idx; } elseif (isInfected[nx][ny] === 0) { ++firewall; neighbor.add(getHash(nx, ny)); } } } } neighbors.push(neighbor); firewalls.push(firewall); } } }
if (neighbors.length === 0) { break; }
let idx = 0; for (let i = 1; i < neighbors.length; ++i) { if (neighbors[i].size > neighbors[idx].size) { idx = i; } } ans += firewalls[idx]; for (let i = 0; i < m; ++i) { for (let j = 0; j < n; ++j) { if (isInfected[i][j] < 0) { if (isInfected[i][j] !== -idx - 1) { isInfected[i][j] = 1; } else { isInfected[i][j] = 2; } } } } for (let i = 0; i < neighbors.length; ++i) { if (i !== idx) { for (const val of neighbors[i]) { let x = val >> 16, y = val & ((1 << 16) - 1); isInfected[x][y] = 1; } } } if (neighbors.length === 1) { break; } } return ans; }