Most Detailed Solution || Explained Approach || Sorting + BFS
Code\n\nclass Solution {\npublic:\n    int cutOffTree(vector<vector<int>>& forest) {\n\n        // Given : Clear the trees in increasing order of height\n      
# Code\n```\nclass Solution {\npublic:\n    int cutOffTree(vector<vector<int>>& forest) {\n\n        // Given : Clear the trees in increasing order of height\n        // constraints : n , m <= 50 = Larger Complexity Acceptable\n\n        // Solution : To handle the contriant we are given that we have to cut them\n        // in increasing order which provides us with solution itself\n\n        // If gives tress are [ 10 , 12 , 11 ]\n        // if i cut 10 next i have to cut will be 11 and i need to find a way to reach 11 \n        // if possible find steps to it if not return -1\n\n        vector<vector<int>> trees;\n        for(int i = 0; i < forest.size(); i++){\n\n            for(int j = 0; j < forest[0].size(); j++){\n\n                if(forest[i][j] > 1){\n\n                    trees.push_back({forest[i][j] , i , j});\n                }\n            }\n            cout<<endl;\n        }\n\n        // sorting the trees according to their height\n        sort(trees.begin() , trees.end());\n\n        // how it gurantees minimum ? As bfs does a level order search so we will\n        // surely get the closest step automatically\n\n        // defining all valid moves in advance\n        vector<pair<int,int>> moves = {{0 , 1} , {1 , 0} , { 0 , -1} , {-1 , 0}};\n\n        // defining start points \n        int sx = 0 , sy = 0;\n        int ans = 0;\n        // now we will cut trees in order\n        for(int i = 0; i < trees.size(); i++){\n            \n            // using breadth first search to find if next tree is reachable or not\n            int cost = bfs(forest , sx , sy , trees[i][1] , trees[i][2] , moves);\n            // if not reachable return -1\n            if(cost == -1){\n                \n                return -1;\n            }\n            // if reachable add the 1 to cost\n            ans += cost;\n            // update the start points to new tree\n            sx = trees[i][1];\n            sy = trees[i][2];\n\n        }\n\n        return ans;\n        \n    }\n\n    int bfs(vector<vector<int>> forest , int sx , int sy , int fx , int fy , vector<pair<int,int>> &moves){\n\n        // if we are search for start point\n        if(fx == sx && fy == sy) return 0;\n\n        // now let\'s do a bfs \n\n        queue<pair<int,int>> q;\n        q.push({sx , sy});\n        int n = forest.size() , m = forest[0].size();\n        vector<vector<int>> seen(n , vector<int>(m , 0));\n        seen[sx][sy] = 1;\n        int cost = 0;\n        while(!q.empty()){\n\n            int qsize = q.size();\n            // iterating the current level\n            for(int i = 0; i < qsize; i++){\n\n                pair<int,int> point = q.front();\n                q.pop();\n\n                for(int j = 0 ; j < moves.size(); j++){\n\n                    long int newx = point.first + moves[j].first;\n                    long int newy = point.second + moves[j].second;\n\n                    // skip if new point is not valid point\n                    if(newx >= n || newy >= m || newx < 0 || newy < 0 ) continue;\n\n                    // if new point is not obstacle and also not seen before the add it\n                    if(forest[newx][newy] != 0 && seen[newx][newy] == 0){\n\n                        q.push({newx , newy});\n                        seen[newx][newy] = 1;\n\n                        if(newx == fx && newy == fy){\n\n                            return cost + 1;\n                        }\n\n                    }\n    \n                }\n                \n            }\n            \n            cost++;\n\n\n        }\n\n        // if not found return -1\n        return -1;\n\n\n    }\n};\n```
['Breadth-First Search', 'Sorting', 'C++']