[Java/C++] Straightforward solution - sorted array + BFS
My idea to solve this problem is by two steps:\n1. Save each {tree height, tree position} into an array heights, and then sort this array based on  its tree hei
My idea to solve this problem is by two steps:\n1. Save each ```{tree height, tree position}``` into an array ```heights```, and then sort this array based on  its ```tree height```. (You can also do this by using ```TreeMap``` or  ```PriorityQueue```);\n2. Accumulate the shortest steps between each two adajacent points in ```heights[]```.\n\nJava version:\n```\nclass Solution {\n    int[][] dir = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};\n    \n    public int cutOffTree(List<List<Integer>> forest) {\n        if (forest == null || forest.size() == 0) return -1;\n        int m = forest.size(), n = forest.get(0).size(), res = 0;\n        //first step: sort the tree position based on its height\n        List<int[]> heights = new ArrayList<>();\n        for(int i = 0; i<m; i++){\n            for(int j = 0; j<n; j++){\n                if(forest.get(i).get(j) > 1)heights.add( new int[]{forest.get(i).get(j), i, j} );\n            }\n        }\n        Collections.sort(heights, new Comparator<int[]>() {\n            public int compare(int[] arr1, int[] arr2) {\n                return Integer.compare(arr1[0], arr2[0]);\n            }\n        });\n        //second step: accumulate the shortest steps between each two adajacent points in heights[].\n        int start_x = 0, start_y = 0; \n        for(int i = 0; i<heights.size(); i++){\n            int cnt_steps = BFS(forest, m, n, start_x, start_y, heights.get(i)[1], heights.get(i)[2]); \n            if(cnt_steps == -1)return -1;\n            res += cnt_steps;\n            start_x = heights.get(i)[1]; \n            start_y = heights.get(i)[2];\n        }\n        return res;\n    }\n    \n    public int BFS(List<List<Integer>> forest, int m, int n, int start_x, int start_y, int des_x, int des_y){\n        if(start_x == des_x && start_y == des_y)return 0;\n        int steps = 0;\n        Queue<int[]> q = new LinkedList<>();\n        q.add(new int[]{start_x, start_y});\n        int[][] visited = new int[m][n];\n        visited[start_x][start_y] = 1;\n        while(!q.isEmpty()){\n            int qsize = q.size();\n            steps++;\n            while(qsize-- >0 ){\n                int[] cur = q.poll();\n                int cur_x = cur[0], cur_y = cur[1];\n                for(int k = 0; k<4; k++){\n                    int x = cur_x + dir[k][0], y = cur_y + dir[k][1];\n                    if(x>=0 && x<m && y>=0 && y<n && forest.get(x).get(y) > 0 && visited[x][y] == 0){\n                        if(x == des_x && y == des_y)return steps;\n                        visited[x][y] = 1;\n                        q.add(new int[]{x,y});\n                    }\n                }\n            }\n        }\n        return -1;\n    }\n}\n```\n\nC++ version:\n```\nclass Solution {\npublic:    \n    vector<vector<int>> dir = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};\n    int cutOffTree(vector<vector<int>>& forest) {\n        int m = forest.size(), n , res = 0;\n        if(m == 0)return -1;\n        n = forest[0].size();\n        //first step: sort the tree position based on its height\n        vector<vector<int>> heights;\n        for(int i = 0; i<m; i++){\n            for(int j = 0; j<n; j++){\n                if(forest[i][j] > 1)heights.push_back({forest[i][j], i, j});\n            }\n        }\n        sort(heights.begin(), heights.end());\n        //second step: accumulate the shortest steps between each two adajacent points in heights[].\n        int start_x = 0, start_y = 0; \n        for(int i = 0; i<heights.size(); i++){\n            int cnt_steps = BFS(forest, m, n, start_x, start_y, heights[i][1], heights[i][2]); \n            if(cnt_steps == -1)return -1;\n            res += cnt_steps;\n            start_x = heights[i][1], start_y = heights[i][2];\n        }\n        return res;\n    }\n    \n    int BFS(vector<vector<int>>& forest, int m, int n, int start_x, int start_y, int des_x, int des_y){\n        if(start_x == des_x && start_y == des_y)return 0;\n        int steps = 0;\n        queue<pair<int, int>> q;\n        q.push({start_x, start_y});\n        vector<vector<int>> visited(m, vector<int>(n, 0));\n        visited[start_x][start_y] = 1;\n        while(!q.empty()){\n            int qsize = q.size();\n            steps++;\n            while(qsize-- >0 ){\n                int cur_x = q.front().first, cur_y = q.front().second;\n                q.pop();\n                for(int k = 0; k<4; k++){\n                    int x = cur_x + dir[k][0], y = cur_y + dir[k][1];\n                    if(x>=0 && x<m && y>=0 && y<n && forest[x][y] > 0 && visited[x][y] == 0){\n                        if(x == des_x && y == des_y)return steps;\n                        visited[x][y] = 1;\n                        q.push({x,y});\n                    }\n                }\n            }\n        }\n        return -1;\n    }\n};\n```
[]