Beats 100% ---> Simple Approach
Intuition\nThe problem requires finding a path in a grid from the top-left to the bottom-right corner, ensuring the character\u2019s health stays above zero. Ea
### Intuition\nThe problem requires finding a path in a grid from the top-left to the bottom-right corner, ensuring the character\u2019s health stays above zero. Each cell can either be safe (0) or contain a hazard (1) that reduces health by 1. We want to find the optimal path that keeps the character\u2019s health as high as possible by the time they reach the destination.\n\n### Approach\n1. **Priority Queue for Optimal Path**: Use a priority queue (max-heap) to always expand the path with the highest health first. This allows us to explore only paths with the greatest remaining health, reducing unnecessary checks.\n2. **Health Tracking**: Track the maximum health at each cell with a `dis` matrix. This avoids redundant checks and ensures each cell is only revisited if we can reach it with more health than previously.\n3. **Directional Movement**: Use four possible directions (up, down, left, right) to move through the grid.\n4. **Early Exit Condition**: As soon as we reach the bottom-right cell with positive health, return `true`.\n\n### Complexity\n- **Time Complexity**: \\(O(V \\log V)\\), where \\(V\\) is the number of cells in the grid. Each cell can be pushed to the priority queue at most once with the maximum health possible.\n- **Space Complexity**: \\(O(V)\\), where \\(V\\) is the number of cells, to store the `dis` matrix and the priority queue.\n\n### Code\n```cpp\nclass Solution {\npublic:\n    bool findSafeWalk(vector<vector<int>>& grid, int health) {\n        priority_queue<pair<int, vector<int>>> pq;\n        if (grid[0][0] == 1) health--;\n        pq.push({health, {0, 0}});\n        \n        vector<vector<int>> dis(grid.size(), vector<int>(grid[0].size(), INT_MIN));\n        dis[0][0] = health;\n\n        vector<vector<int>> dir = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};\n\n        while (!pq.empty()) {\n            auto t = pq.top();\n            pq.pop();\n\n            int curr_health = t.first;\n            int x = t.second[0], y = t.second[1];\n\n            if (x == grid.size() - 1 && y == grid[0].size() - 1) {\n                return curr_health >= 1;\n            }\n\n            for (auto d : dir) {\n                int a = x + d[0];\n                int b = y + d[1];\n\n                if (a >= 0 && a < grid.size() && b >= 0 && b < grid[0].size()) {\n                    int new_health = curr_health - (grid[a][b] == 1 ? 1 : 0);\n\n                    if (new_health > dis[a][b]) {\n                        dis[a][b] = new_health;\n                        pq.push({new_health, {a, b}});\n                    }\n                }\n            }\n        }\n        return false;\n    }\n};\n```
['C++']