Java Clean Solution | Weekly Contest
Code\njava []\nclass Pair{\n    int r, c, h;\n     public Pair(int r, int c, int h){\n         this.r = r;\n         this.c = c;\n         this.h = h;\n     }\n
# Code\n```java []\nclass Pair{\n    int r, c, h;\n     public Pair(int r, int c, int h){\n         this.r = r;\n         this.c = c;\n         this.h = h;\n     }\n}\n\nclass Solution {\n    int[] dir_X = {0, 1, 0, -1};\n    int[] dir_Y = {1, 0, -1, 0};\n    \n    public boolean findSafeWalk(List<List<Integer>> grid, int health) {\n        int m = grid.get(0).size();\n        int n = grid.size();\n        \n        int[][] vis = new int[n][m];\n        for (int i = 0; i < n; i++) {\n            Arrays.fill(vis[i], -1);\n        }\n        vis[0][0] = health - grid.get(0).get(0);\n        \n        Queue<Pair> q = new LinkedList<>();\n        q.offer(new Pair(0, 0, health - grid.get(0).get(0)));\n\n        while (!q.isEmpty()) {\n            Pair p = q.poll();\n            int r = p.r;\n            int c = p.c;\n            int h = p.h;\n            \n            if (r == n - 1 && c == m - 1 && h > 0) {\n                return true;\n            }\n            for (int i=0; i<4; i++) {\n                int nr = r + dir_X[i];\n                int nc = c + dir_Y[i];\n                \n                if(nr>=0 && nr<n && nc>=0 && nc<m){\n                    int nh = h - grid.get(nr).get(nc);\n                    \n                    if(nh>0 && nh > vis[nr][nc]){\n                        vis[nr][nc] = nh;\n                        q.offer(new Pair(nr, nc, nh));\n                    }\n                }\n            }\n        }\n\n        return false;\n    }\n}\n```
['Array', 'Math', 'Queue', 'Heap (Priority Queue)', 'Java']