[Tag: Easy] Textbook DJ Kstra
Perseverance and Upsolving is the key.
Perseverance and Upsolving is the key.
```csharp []
public class Solution {
    int r; int c;
    IList<int> x = new List<int>(){0,0,-1,+1};
    IList<int> y = new List<int>(){-1,+1,0,0};

    public bool FindSafeWalk(IList<IList<int>> mat, int health) {
        r = mat.Count;
        c = mat[0].Count;


        SortedSet<(int, int)> minHeap = new SortedSet<(int,int)>(Comparer<(int,int)>.Create((a, b) => {
            int cmp = a.Item1.CompareTo(b.Item1);
            return cmp == 0 ? a.Item2.CompareTo(b.Item2) : cmp;
        }));

        IList<int> dist = Enumerable.Repeat(int.MaxValue, r*c).ToList();

        dist[0] = mat[0][0];

        minHeap.Add((dist[0],0));

        while(minHeap.Count > 0){
            var p = minHeap.Min;
            minHeap.Remove(p);

            int currDist = p.Item1;
            int currNode = p.Item2;
            int i = currNode/c;
            int j = currNode - i * c;

            if (currDist > dist[currNode]) continue;

            for (int dir = 0; dir < 4; dir++){
                int adjI = i + x[dir];
                int adjJ = j + y[dir];
                if (!inBounds(adjI, adjJ)) continue;

                int w = mat[adjI][adjJ];
                int adjNode = getNode(adjI, adjJ);

                if (currDist + w >= dist[adjNode]) continue;

                dist[adjNode] = currDist + w;
                minHeap.Add((dist[adjNode], adjNode));
            }
        }

        return dist[r*c - 1] < health;
        
    }

    int getNode(int i, int j){
        return i * c + j;
    }

    bool inBounds(int i, int j){
        return i >= 0 && i < r && j >= 0 && j < c;
    }
}
```
['C#']