| | | 1 | | // -------------------------------------------------------------------------------- |
| | | 2 | | // Copyright (C) 2026 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.MinimumCostToMakeAtLeastOneValidPathInGrid; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public sealed class MinimumCostToMakeAtLeastOneValidPathInGridBreadthFirstSearch : IMinimumCostToMakeAtLeastOneValidPath |
| | | 16 | | { |
| | 3 | 17 | | private readonly (int X, int Y)[] _directions = |
| | 3 | 18 | | [ |
| | 3 | 19 | | (0, 1), |
| | 3 | 20 | | (0, -1), |
| | 3 | 21 | | (1, 0), |
| | 3 | 22 | | (-1, 0) |
| | 3 | 23 | | ]; |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Time complexity - O(n * m) |
| | | 27 | | /// Space complexity - O(n * m) |
| | | 28 | | /// </summary> |
| | | 29 | | /// <param name="grid"></param> |
| | | 30 | | /// <returns></returns> |
| | | 31 | | public int MinCost(int[][] grid) |
| | 3 | 32 | | { |
| | 3 | 33 | | var m = grid.Length; |
| | 3 | 34 | | var n = grid[0].Length; |
| | | 35 | | |
| | 3 | 36 | | var cost = new int[m, n]; |
| | | 37 | | |
| | 24 | 38 | | for (var i = 0; i < m; i++) |
| | 9 | 39 | | { |
| | 76 | 40 | | for (var j = 0; j < n; j++) |
| | 29 | 41 | | { |
| | 29 | 42 | | cost[i, j] = int.MaxValue; |
| | 29 | 43 | | } |
| | 9 | 44 | | } |
| | | 45 | | |
| | 3 | 46 | | cost[0, 0] = 0; |
| | | 47 | | |
| | 3 | 48 | | var linkedList = new LinkedList<(int X, int Y, int CurrentCost)>(); |
| | | 49 | | |
| | 3 | 50 | | linkedList.AddFirst((0, 0, 0)); |
| | | 51 | | |
| | 36 | 52 | | while (linkedList.First != null) |
| | 33 | 53 | | { |
| | 33 | 54 | | var (x, y, currentCost) = linkedList.First.Value; |
| | | 55 | | |
| | 33 | 56 | | linkedList.RemoveFirst(); |
| | | 57 | | |
| | 33 | 58 | | if (currentCost > cost[x, y]) |
| | 4 | 59 | | { |
| | 4 | 60 | | continue; |
| | | 61 | | } |
| | | 62 | | |
| | 290 | 63 | | for (var d = 0; d < _directions.Length; d++) |
| | 116 | 64 | | { |
| | 116 | 65 | | var nx = x + _directions[d].X; |
| | 116 | 66 | | var ny = y + _directions[d].Y; |
| | | 67 | | |
| | 116 | 68 | | var newCost = currentCost; |
| | | 69 | | |
| | 116 | 70 | | if (grid[x][y] != d + 1) |
| | 87 | 71 | | { |
| | 87 | 72 | | newCost++; |
| | 87 | 73 | | } |
| | | 74 | | |
| | 116 | 75 | | if (nx < 0 || ny < 0 || nx >= m || ny >= n || newCost >= cost[nx, ny]) |
| | 86 | 76 | | { |
| | 86 | 77 | | continue; |
| | | 78 | | } |
| | | 79 | | |
| | 30 | 80 | | cost[nx, ny] = newCost; |
| | | 81 | | |
| | 30 | 82 | | if (grid[x][y] == d + 1) |
| | 12 | 83 | | { |
| | 12 | 84 | | linkedList.AddFirst((nx, ny, newCost)); |
| | 12 | 85 | | } |
| | | 86 | | else |
| | 18 | 87 | | { |
| | 18 | 88 | | linkedList.AddLast((nx, ny, newCost)); |
| | 18 | 89 | | } |
| | 30 | 90 | | } |
| | 29 | 91 | | } |
| | | 92 | | |
| | 3 | 93 | | return cost[m - 1, n - 1]; |
| | 3 | 94 | | } |
| | | 95 | | } |