JAVA simple and fast with explanation and comments
Overview\n\nMy solution uses a BFS algorithm to find the distance between 2 any trees and then creates a path from the origin to the final tree by going via all
# Overview\n\nMy solution uses a BFS algorithm to find the distance between 2 any trees and then creates a path from the origin to the final tree by going via all the trees in the correct order.\n\n---\n\n# Step by Step\n1. Find all trees (all heights > 1 in the forest) and store in a list for sorting (with the height cached in the int[])\n2. Starting from (0, 0) get the distance between our current postion and the next tree (T1)\n3. Set current position to be the tree T1, and then repeat\n4. If any distance is -1, that means we can\'t find a valid path so we should return -1;\n5. Sum all paths together and return the result\n\n---\n# The Code\n\n```\nclass Solution {\n    public int cutOffTree(List<List<Integer>> forest) {\n        List<int[]> trees = new ArrayList();\n        \n\t\t// Find all trees\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                    trees.add(new int[]{i, j, forest.get(i).get(j)});\n        \n\t\t// Sort trees based on height\n        trees.sort((a, b) -> Integer.compare(a[2], b[2]));\n        \n        int dist = 0;\n        \n\t\t// Set current postion to be the origin\n        int[] currPos = new int[]{0, 0};\n        \n        for(int[] tree : trees) {\n            int d = getDist(forest, currPos, tree);\n\t\t\t// If we cannot find a path, return -1\n            if (d == -1) return -1;\n            dist+= d;\n\t\t\t// Set current position to be this tree\n            currPos= tree;\n        }\n        \n        return dist;\n    }\n    \n\t// Standard Breadth First Search algorithm\n    private int getDist(List<List<Integer>> forest, int[] aPos, int[] bPos) {\n        int aR = aPos[0];\n        int aC = aPos[1];\n        int bR = bPos[0];\n        int bC = bPos[1];\n        \n        boolean[][] seen = new boolean[forest.size()][forest.get(0).size()];\n        \n        Queue<int[]> q = new LinkedList();\n        q.offer(new int[]{aR, aC});\n        \n        int steps = 0;\n        while(!q.isEmpty()) {\n            int size = q.size();\n            \n\t\t\t// Search through all positions in queue in 1 step\n            for(int i = 0; i < size; i++) {\n                int[] pos = q.poll();\n                int r = pos[0];\n                int c = pos[1];\n\t\t\t\t\n\t\t\t\t// If we have found target, return steps\n                if (r == bR && c == bC) return steps;\n                \n\t\t\t\t// validate position\n                if (r < 0 || r >= forest.size() || c < 0 || c >= forest.get(0).size()) continue;\n                if (seen[r][c]) continue;\n                if (forest.get(r).get(c) == 0) continue;\n                \n                seen[r][c] = true;\n                \n\t\t\t\t// Expand into 4 directions\n                q.offer(new int[]{r+1, c});\n                q.offer(new int[]{r-1, c});\n                q.offer(new int[]{r, c+1});\n                q.offer(new int[]{r, c-1});\n            }\n            \n            steps++;\n        }\n        \n\t\t// If we haven\'t found target and no more positions to check, return -1\n        return -1;\n    }\n}\n```
['Breadth-First Search', 'Java']