Simple BFS with Queue
Approachuse h[i][j] to record maximum health of visiting grid(i, j)Complexity

Time complexity: O(m*n)


Space complexity: O(m*n)

Code

# Approach
<!-- Describe your approach to solving the problem. -->
use h[i][j] to record maximum health of visiting grid(i, j)
# Complexity
- Time complexity: O(m*n)

- Space complexity: O(m*n)


# Code
```cpp []
class Solution {
public:
    using pii = pair<int, int>;
    bool findSafeWalk(vector<vector<int>>& grid, int health) {
        int m = grid.size(), n = grid[0].size();
        vector<vector<int>> h(m, vector<int>(n, -1));
        int x[4] = {1, 0, -1, 0};
        int y[4] = {0, 1, 0, -1};
        
        // bfs
        queue<pii> q;
        q.emplace(0, 0);
        h[0][0] = grid[0][0] == 0 ? health : health-1;

        while(!q.empty()) {
            int i = q.front().first, j = q.front().second;
            q.pop();
            if (h[i][j] < 1) continue;
            for (int k = 0; k < 4; k++) {
                int ni = i + y[k], nj = j + x[k];
                if (in_range(ni, nj, m, n)) {
                    int nh = grid[ni][nj] == 0 ? h[i][j] : h[i][j] - 1;
                    if (nh > h[ni][nj]) {
                        h[ni][nj] = nh;
                        q.emplace(ni, nj);
                    }
                }
            }
        }

        return h[m-1][n-1] >= 1;
    }

    inline bool in_range(int i, int j, int m, int n) {
        if (i < 0 || i >= m || j < 0 || j >= n) return false;
        return true;
    }
};
```
['C++']