01 BFS - faster than Dijkstra
IntuitionApproachComplexity
Time complexity:

Space complexity:
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```cpp []
class Solution {
public:
    int ans = INT_MAX;
    // void dfs(int row, int col, int n, int m,vector<vector<int>>& grid,vector<vector<int>> &vis, int sum){
    //     vis[row][col] = 1;
    //     if(row==n-1 && col==m-1)
    //       ans = min(ans,sum);

          
    //       for(int i=0;i<4;i++){
    //         int nrow = row + delRow[i];
    //         int ncol = col + delCol[i];

    //         if(nrow>=0 && nrow<n && ncol>=0 && ncol<m && !vis[nrow][ncol]){
    //             if(grid[nrow][ncol]==0)
    //               dfs(nrow,ncol,n,m,grid,vis,sum);
    //             else
    //               dfs(nrow,ncol,n,m,grid,vis,sum+1);
                    
    //         }
    //       }
    //     vis[row][col] = 0;

    // }
    bool findSafeWalk(vector<vector<int>>& grid, int health) {
        int n = grid.size();
        int m = grid[0].size();
        vector<vector<int>> dist(n,vector<int>(m,INT_MAX));
        int delRow[] = {-1,0,1,0};
          int delCol[] = {0,1,0,-1};

        deque<pair<int,int>> dq;
        dq.push_front({0,0});
        dist[0][0] = 0;

        while(!dq.empty()){
            int row = dq.front().first;
            int col = dq.front().second;
            dq.pop_front();

            for(int i=0;i<4;i++){
            int nrow = row + delRow[i];
            int ncol = col + delCol[i];

            if(nrow>=0 && nrow<n && ncol>=0 && ncol<m){
               int wt = grid[nrow][ncol];

               if(dist[nrow][ncol]> dist[row][col] + wt){
                     dist[nrow][ncol] = dist[row][col] + wt;

                     if(wt==0)
                       dq.push_front({nrow,ncol});
                     else
                       dq.push_back({nrow,ncol});  

               }
                    
            }
          }
        }
      
       if(grid[0][0]==1)
        dist[n-1][m-1]++;

    return (health - dist[n-1][m-1])>=1;
         

         
    }
};
```
['C++']