-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution749.cs
108 lines (91 loc) · 3.32 KB
/
Solution749.cs
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
97
98
99
100
101
102
103
104
105
106
107
108
using System.Text;
using LeetCode.DataStructures;
namespace LeetCode.Solutions;
public class Solution749
{
private static readonly int[][] directions = [
[1, 0], [-1, 0], [0, 1], [0, -1]
];
/// <summary>
/// 749. Contain Virus - Hard
/// <a href="https://leetcode.com/problems/contain-virus">See the problem</a>
/// </summary>
public int ContainVirus(int[][] isInfected)
{
int m = isInfected.Length, n = isInfected[0].Length;
int totalWalls = 0;
while (true)
{
// Identify each infected region and calculate walls and threats
var regions = new List<HashSet<(int, int)>>();
var threats = new List<HashSet<(int, int)>>();
var walls = new List<int>();
var visited = new bool[m, n];
for (var i = 0; i < m; i++)
{
for (var j = 0; j < n; j++)
{
if (isInfected[i][j] == 1 && !visited[i, j])
{
var region = new HashSet<(int, int)>();
var threat = new HashSet<(int, int)>();
var wallCount = 0;
DFS(isInfected, i, j, visited, region, threat, ref wallCount);
regions.Add(region);
threats.Add(threat);
walls.Add(wallCount);
}
}
}
if (regions.Count == 0) break;
// Find the most dangerous region
var maxThreatIdx = 0;
for (var i = 1; i < threats.Count; i++)
{
if (threats[i].Count > threats[maxThreatIdx].Count)
{
maxThreatIdx = i;
}
}
// Add walls to contain the most dangerous region
totalWalls += walls[maxThreatIdx];
foreach (var cell in regions[maxThreatIdx])
{
isInfected[cell.Item1][cell.Item2] = -1; // Quarantine this region
}
// Spread virus in other regions
var anySpread = false;
for (int i = 0; i < regions.Count; i++)
{
if (i == maxThreatIdx) continue; // Skip quarantined region
foreach (var cell in threats[i])
{
isInfected[cell.Item1][cell.Item2] = 1;
anySpread = true;
}
}
if (!anySpread) break; // If no spread, we are done
}
return totalWalls;
}
private void DFS(int[][] isInfected, int x, int y, bool[,] visited, HashSet<(int, int)> region, HashSet<(int, int)> threat, ref int wallCount)
{
int m = isInfected.Length, n = isInfected[0].Length;
visited[x, y] = true;
region.Add((x, y));
foreach (var dir in directions)
{
int nx = x + dir[0], ny = y + dir[1];
if (nx < 0 || ny < 0 || nx >= m || ny >= n) continue;
if (isInfected[nx][ny] == 1 && !visited[nx, ny])
{
DFS(isInfected, nx, ny, visited, region, threat, ref wallCount);
}
else if (isInfected[nx][ny] == 0)
{
threat.Add((nx, ny));
wallCount++;
}
}
}
}