-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
93 lines (89 loc) · 2.49 KB
/
Solution.cs
File metadata and controls
93 lines (89 loc) · 2.49 KB
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
public class Solution
{
public int MaximumSafenessFactor(IList<IList<int>> grid)
{
int n = grid.Count;
int INF = int.MaxValue;
int[][] dist = new int[n][];
for (int i = 0; i < n; i++)
{
dist[i] = new int[n];
Array.Fill(dist[i], INF);
}
Queue<(int x, int y)> queue = [];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (grid[i][j] == 1)
{
dist[i][j] = 0;
queue.Enqueue((i, j));
}
}
}
int l = 0;
int[] dirs = [1, 0, -1, 0, 1];
while (queue.Count > 0)
{
int size = queue.Count;
l++;
for (int i = 0; i < size; i++)
{
var (x, y) = queue.Dequeue();
for (int d = 0; d < 4; d++)
{
int x1 = x + dirs[d];
int y1 = y + dirs[d + 1];
if (x1 >= 0 && y1 >= 0 && x1 < n && y1 < n && dist[x1][y1] == INF)
{
dist[x1][y1] = l;
queue.Enqueue((x1, y1));
}
}
}
}
int[][] fn = new int[n][];
for (int i = 0; i < n; i++)
{
fn[i] = new int[n];
}
fn[0][0] = dist[0][0];
queue.Clear();
queue.Enqueue((0, 0));
while (queue.Count > 0)
{
var (x, y) = queue.Dequeue();
for (int d = 0; d < 4; d++)
{
int x1 = x + dirs[d];
int y1 = y + dirs[d + 1];
if (x1 >= 0 && y1 >= 0 && x1 < n && y1 < n)
{
if (Math.Min(fn[x][y], dist[x1][y1]) > fn[x1][y1])
{
fn[x1][y1] = Math.Min(fn[x][y], dist[x1][y1]);
queue.Enqueue((x1, y1));
}
}
}
}
return fn[n - 1][n - 1];
}
int Dist(int x, int y, IList<IList<int>> grid)
{
int dist = int.MaxValue;
int n = grid.Count;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (grid[i][j] == 1)
{
dist = Math.Min(dist, Math.Abs(x - i) + Math.Abs(y - j));
}
}
}
return dist;
}
}