-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2290-minimum-obstacle-removal.dart
63 lines (53 loc) · 1.4 KB
/
2290-minimum-obstacle-removal.dart
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
import 'dart:collection';
int minimumObstacles(List<List<int>> grid) {
final m = grid.length;
final n = grid[0].length;
final directions = [
[0, 1], // right
[1, 0], // down
[0, -1], // left
[-1, 0] // up
];
// BFS.
final deque = DoubleLinkedQueue<List<int>>();
// Distance matrix to track minimum removals to reach each cell.
final dist = List.generate(m, (_) => List.filled(n, double.maxFinite.toInt()));
deque.addFirst([0, 0]);
dist[0][0] = 0;
while (deque.isNotEmpty) {
final current = deque.removeFirst();
final x = current[0];
final y = current[1];
for (var dir in directions) {
final nx = x + dir[0];
final ny = y + dir[1];
if (nx >= 0 && nx < m && ny >= 0 && ny < n) {
final newDist = dist[x][y] + grid[nx][ny];
if (newDist < dist[nx][ny]) {
dist[nx][ny] = newDist;
// If it's an obstacle, add to the back of the deque; otherwise, to the front.
if (grid[nx][ny] == 1) {
deque.addLast([nx, ny]);
} else {
deque.addFirst([nx, ny]);
}
}
}
}
}
return dist[m - 1][n - 1];
}
void main() {
final grid1 = [
[0, 1, 1],
[1, 1, 0],
[1, 1, 0]
];
print(minimumObstacles(grid1)); // Output: 2
final grid2 = [
[0, 1, 0, 0, 0],
[0, 1, 0, 1, 0],
[0, 0, 0, 1, 0]
];
print(minimumObstacles(grid2)); // Output: 0
}