C++ || Commented Explanation || Easy to Understand || BFS + Greedy
\n/*\n\n 0 --> obstacle\n 1 --> empty cell\n>1 --> tree\'s height\n\n North , East , South , West --> Moves Possible\n \n We can cut off all the trees , if and 
```\n/*\n\n 0 --> obstacle\n 1 --> empty cell\n>1 --> tree\'s height\n\n North , East , South , West --> Moves Possible\n \n We can cut off all the trees , if and only if there exists a\n path from the current tree to the next greater tree value.\n\n We will sort the tree\'s according to their values and then do a\n bfs traversal b/w adjacent trees and calculate the shortest \n distance b/w them , if there is no path possible , then return \n -1, otherwise at the end of the program return the summation of \n those distances.\n \n T.C : O(M*N*M*N)\n S.C : O(M*N)\n\n*/\n\n// Build a Tree Cell Structure for simplicity sake //\n\nstruct Tree{\n  \n    public:\n    \n    int value;\n    int x;\n    int y;\n    \n};\n\n// Helps to sort the trees on the basis of their values //\n\nbool static comp(const Tree&x,const Tree&y){\n    \n    return x.value < y.value;\n    \n}\n\n// 4-directional coordinates //\n\nint dx[4] = {0,0,1,-1};\nint dy[4] = {1,-1,0,0};\n\nint visited[52][52];\n\nint bfs_traversal(int src1,int src2,int dest1,int dest2,vector<vector<int>>&forest){\n    \n    int m = forest.size();\n    int n = forest[0].size();\n    \n    // Reset the visited array //\n    \n    for(int i=0;i<m;i++){\n        for(int j=0;j<n;j++){\n            visited[i][j] = 0;\n        }\n    }\n    \n    // Data structure widely used for BFS Traversal //\n    \n    queue<pair<int,int>>q;\n    \n    // Single Source BFS //\n    \n    q.push({src1,src2});\n    \n    int lvl = 0;\n    \n    while(!q.empty()){\n        \n        int sz = q.size();\n        \n        while(sz--){\n            \n            auto curr = q.front();\n            q.pop();\n            \n            int x = curr.first;\n            int y = curr.second;\n            \n            if(x==dest1 && y==dest2){\n                return lvl;\n            }\n            \n            if(visited[x][y]) continue;\n            \n            visited[x][y] = 1;\n            \n            for(int dir=0;dir<4;dir++){\n                \n                int nx = x + dx[dir];\n                int ny = y + dy[dir];\n                \n                if(nx>=0 && nx<m && ny>=0 && ny<n && !visited[nx][ny] && forest[nx][ny]){\n                    \n                    q.push({nx,ny});\n                    \n                }\n                \n            }\n            \n        }\n        \n        lvl++;\n        \n    }\n    \n    return -1;\n    \n}\n\nclass Solution {\npublic:\n    \n    int cutOffTree(vector<vector<int>>& forest) {\n        \n        int m=forest.size();\n        int n=forest[0].size();\n        \n        vector<Tree>vec;\n        \n        for(int i=0;i<m;i++){\n            for(int j=0;j<n;j++){\n                if(forest[i][j]>1){\n                    vec.push_back({forest[i][j],i,j});\n                }\n            }\n        }\n        \n        // Starting Point - By default //\n        \n        vec.push_back({0,0,0});\n        \n        // Sort on the basis of the tree values //\n        \n        sort(vec.begin(),vec.end(),comp);\n        \n        int ans = 0;\n        \n        // BFS Traversal b/w adjacent trees //\n        \n        for(int i=0;i<vec.size()-1;i++){\n            \n            int curr = bfs_traversal(vec[i].x,vec[i].y,vec[i+1].x,vec[i+1].y,forest);\n            \n            // If there doesn\'t exist a single path b/w adjacent trees , then simple return -1 (invalid sequence) //\n            \n            if(curr<0){\n                return -1;\n            }\n            \n            ans += curr;\n            \n        }\n        \n        return ans;\n        \n    }\n};\n```
['Greedy', 'Breadth-First Search', 'C']