| | 1 | | // -------------------------------------------------------------------------------- |
| | 2 | | // Copyright (C) 2025 Eugene Eremeev (also known as Yevhenii Yeriemeieiv). |
| | 3 | | // All Rights Reserved. |
| | 4 | | // -------------------------------------------------------------------------------- |
| | 5 | | // This software is the confidential and proprietary information of Eugene Eremeev |
| | 6 | | // (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not |
| | 7 | | // disclose such Confidential Information and shall use it only in accordance with |
| | 8 | | // the terms of the license agreement you entered into with Eugene Eremeev (also |
| | 9 | | // known as Yevhenii Yeriemeieiv). |
| | 10 | | // -------------------------------------------------------------------------------- |
| | 11 | |
|
| | 12 | | namespace LeetCode.Algorithms.FindTheSafestPathInGrid; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class FindTheSafestPathInGridPriorityQueue : IFindTheSafestPathInGrid |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Time complexity - O(n^2 log n^2) |
| | 19 | | /// Space complexity - O(n^2) |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="grid"></param> |
| | 22 | | /// <returns></returns> |
| | 23 | | public int MaximumSafenessFactor(IList<IList<int>> grid) |
| 3 | 24 | | { |
| 3 | 25 | | var n = grid.Count; |
| | 26 | |
|
| 3 | 27 | | var thieves = new List<(int, int)>(); |
| | 28 | |
|
| 26 | 29 | | for (var r = 0; r < n; r++) |
| 10 | 30 | | { |
| 88 | 31 | | for (var c = 0; c < n; c++) |
| 34 | 32 | | { |
| 34 | 33 | | if (grid[r][c] == 1) |
| 5 | 34 | | { |
| 5 | 35 | | thieves.Add((r, c)); |
| 5 | 36 | | } |
| 34 | 37 | | } |
| 10 | 38 | | } |
| | 39 | |
|
| 3 | 40 | | var distance = new int[n][]; |
| | 41 | |
|
| 26 | 42 | | for (var i = 0; i < n; i++) |
| 10 | 43 | | { |
| 10 | 44 | | distance[i] = new int[n]; |
| | 45 | |
|
| 10 | 46 | | Array.Fill(distance[i], int.MaxValue); |
| 10 | 47 | | } |
| | 48 | |
|
| 3 | 49 | | var queue = new Queue<(int, int)>(); |
| | 50 | |
|
| 19 | 51 | | foreach (var thief in thieves) |
| 5 | 52 | | { |
| 5 | 53 | | queue.Enqueue(thief); |
| | 54 | |
|
| 5 | 55 | | distance[thief.Item1][thief.Item2] = 0; |
| 5 | 56 | | } |
| | 57 | |
|
| 3 | 58 | | int[][] directions = [[0, 1], [1, 0], [0, -1], [-1, 0]]; |
| | 59 | |
|
| 37 | 60 | | while (queue.Count > 0) |
| 34 | 61 | | { |
| 34 | 62 | | var (r, c) = queue.Dequeue(); |
| | 63 | |
|
| 374 | 64 | | foreach (var dir in directions) |
| 136 | 65 | | { |
| 272 | 66 | | int nr = r + dir[0], nc = c + dir[1]; |
| | 67 | |
|
| 136 | 68 | | if (nr < 0 || nr >= n || nc < 0 || nc >= n || distance[nr][nc] != int.MaxValue) |
| 107 | 69 | | { |
| 107 | 70 | | continue; |
| | 71 | | } |
| | 72 | |
|
| 29 | 73 | | distance[nr][nc] = distance[r][c] + 1; |
| | 74 | |
|
| 29 | 75 | | queue.Enqueue((nr, nc)); |
| 29 | 76 | | } |
| 34 | 77 | | } |
| | 78 | |
|
| 62 | 79 | | var priorityQueue = new PriorityQueue<(int, int, int), int>(Comparer<int>.Create((a, b) => b - a)); |
| | 80 | |
|
| 3 | 81 | | priorityQueue.Enqueue((distance[0][0], 0, 0), distance[0][0]); |
| | 82 | |
|
| 3 | 83 | | var visited = new bool[n][]; |
| | 84 | |
|
| 26 | 85 | | for (var i = 0; i < n; i++) |
| 10 | 86 | | { |
| 10 | 87 | | visited[i] = new bool[n]; |
| 10 | 88 | | } |
| | 89 | |
|
| 3 | 90 | | visited[0][0] = true; |
| | 91 | |
|
| 24 | 92 | | while (priorityQueue.Count > 0) |
| 24 | 93 | | { |
| 24 | 94 | | var (minDist, r, c) = priorityQueue.Dequeue(); |
| | 95 | |
|
| 24 | 96 | | if (r == n - 1 && c == n - 1) |
| 3 | 97 | | { |
| 3 | 98 | | return minDist; |
| | 99 | | } |
| | 100 | |
|
| 231 | 101 | | foreach (var dir in directions) |
| 84 | 102 | | { |
| 168 | 103 | | int nr = r + dir[0], nc = c + dir[1]; |
| | 104 | |
|
| 84 | 105 | | if (nr < 0 || nr >= n || nc < 0 || nc >= n || visited[nr][nc]) |
| 56 | 106 | | { |
| 56 | 107 | | continue; |
| | 108 | | } |
| | 109 | |
|
| 28 | 110 | | visited[nr][nc] = true; |
| | 111 | |
|
| 28 | 112 | | priorityQueue.Enqueue((Math.Min(minDist, distance[nr][nc]), nr, nc), |
| 28 | 113 | | Math.Min(minDist, distance[nr][nc])); |
| 28 | 114 | | } |
| 21 | 115 | | } |
| | 116 | |
|
| 0 | 117 | | return 0; |
| 3 | 118 | | } |
| | 119 | | } |