Java | DFS
Code\njava []\nclass Solution {\n    boolean visited[][][];\n    public boolean findSafeWalk(List<List<Integer>> grid, int health) {\n        visited = new bool
# Code\n```java []\nclass Solution {\n    boolean visited[][][];\n    public boolean findSafeWalk(List<List<Integer>> grid, int health) {\n        visited = new boolean[grid.size()][grid.get(0).size()][health+1];\n        boolean res = dfs(grid, health, 0, 0);\n        return res;\n    }\n\n    private boolean dfs(List<List<Integer>> grid, int health, int i, int j) {\n        if(i<0 || j<0 || i>=grid.size() || j>=grid.get(i).size() || visited[i][j][health]){\n            return false;\n        }\n        visited[i][j][health] = true;\n        if (grid.get(i).get(j) == 1) {\n            health--;\n        }\n        if(health < 1){\n            return false;\n        }\n        if(i == grid.size()-1 && j == grid.get(i).size()-1){\n            return true;\n        }\n        boolean left = dfs(grid, health, i, j-1);\n        boolean right = dfs(grid, health, i, j+1);\n        boolean top = dfs(grid, health, i-1, j);\n        boolean bottom = dfs(grid, health, i+1, j);\n        return left || right || top || bottom;\n    }\n}\n```
['Java']