java ez 2 understand
```\nclass Solution {\n    //approach: 1st store all the positions in the min heap acc. to their height\n    //now start removing the elements from the heap and
```\nclass Solution {\n    //approach: 1st store all the positions in the min heap acc. to their height\n    //now start removing the elements from the heap and calculate their dis using bfs\n    // if at any point we cann\'t reach at the next position return -1;\n    // else keep on adding the distances and return;\n    public int cutOffTree(List<List<Integer>> forest) {\n       PriorityQueue<int[]> pq=new PriorityQueue<>((a,b)->(forest.get(a[0]).get(a[1])-forest.get(b[0]).get(b[1])));\n       for(int i=0;i<forest.size();i++){\n           for(int j=0;j<forest.get(0).size();j++){\n               if(forest.get(i).get(j)>1)\n                   pq.add(new int[]{i,j});\n           }\n       }\n        int ans=0;\n       int curr[]={0,0};\n       while(pq.size()>0){\n           int[] temp=pq.poll();\n           int dis=calcDis(forest,curr,temp);\n           //System.out.println(dis+" "+temp.height);\n           if(dis==-1)\n               return -1;\n           ans+=dis;\n           curr=temp;\n       }\n        return ans;\n    }\n    int calcDis(List<List<Integer>> forest,int start[],int end[]){\n        int n=forest.size(),m=forest.get(0).size();\n        boolean vis[][]=new boolean[n][m];\n        Queue<int[]> queue=new LinkedList<>();\n        queue.add(start);\n        vis[start[0]][start[1]]=true;\n        int dis=0;\n        while(queue.size()>0){\n            int len =queue.size();\n            while(len-->0){\n                int temp[]=queue.remove();\n                int r=temp[0],c=temp[1];\n                if(r==end[0] && c==end[1])\n                    return dis;\n                if(r+1<n && !vis[r+1][c] && forest.get(r+1).get(c)!=0){\n                    queue.add(new int[]{r+1,c});\n                    vis[r+1][c]=true;\n                }if(r-1>=0 && !vis[r-1][c] && forest.get(r-1).get(c)!=0){\n                    queue.add(new int[]{r-1,c});\n                    vis[r-1][c]=true;\n                }if(c-1>=0 && !vis[r][c-1] && forest.get(r).get(c-1)!=0){\n                    queue.add(new int[]{r,c-1});\n                    vis[r][c-1]=true;\n                }if(c+1<m && !vis[r][c+1] && forest.get(r).get(c+1)!=0){\n                    queue.add(new int[]{r,c+1});\n                    vis[r][c+1]=true;\n                }\n            }\n            dis++;\n        }\n        return -1;\n        \n    }\n}
['Breadth-First Search', 'Heap (Priority Queue)', 'Java']