Backtracking
\n# Code\ncpp []\nclass Solution {\npublic:\n    bool dfs(int r, int c, vector<vector<int>>& grid, vector<vector<int>>& vis, vector<vector<vector<int>>>& dp, in
\n# Code\n```cpp []\nclass Solution {\npublic:\n    bool dfs(int r, int c, vector<vector<int>>& grid, vector<vector<int>>& vis, vector<vector<vector<int>>>& dp, int h) {\n        if (r < 0 || c < 0 || r >= grid.size() || c >= grid[0].size() || vis[r][c])\n            return false;\n        if (h < 1) return false;\n        h -= grid[r][c];\n        if (r == grid.size() - 1 && c == grid[0].size() - 1 && h) return true;\n        if (dp[r][c][h] != -1) return dp[r][c][h];\n        vis[r][c] = 1;\n        bool left = false, right = false, up = false, down = false;\n        \n        if (c > 0 && !vis[r][c - 1])\n            left = dfs(r, c - 1, grid, vis, dp, h);\n        if (c < grid[0].size() - 1 && !vis[r][c + 1])\n            right = dfs(r, c + 1, grid, vis, dp, h);\n        if (r > 0 && !vis[r - 1][c])\n            up = dfs(r - 1, c, grid, vis, dp, h);\n        if (r < grid.size() - 1 && !vis[r + 1][c])\n            down = dfs(r + 1, c, grid, vis, dp, h);\n\n        vis[r][c] = 0;\n        return dp[r][c][h] = (left || right || up || down);\n    }\n    \n    bool findSafeWalk(vector<vector<int>>& grid, int h) {\n        int n = grid.size(), m = grid[0].size();\n        vector<vector<int>> vis(n, vector<int>(m, 0));\n        vector<vector<vector<int>>> dp(n, vector<vector<int>>(m, vector<int>(h + 1, -1)));\n        return dfs(0, 0, grid, vis, dp, h);\n    }\n};\n\n```
['C++']