[C++] || BFS FOR BEGINNERS || EASY
WE CANNOT USE A SIMPLE RECURSION CONCEPT HERE, IT WILL TLE.  It is because this question is asking us to now find trees in increasing order, but cut trees in in
WE CANNOT USE A SIMPLE RECURSION CONCEPT HERE, IT WILL TLE.  It is because this question is asking us to now find trees in increasing order, but cut trees in increasing order, so there will be cases when you will ignore the tree and will again return back to it. Kind of like a question where, minimum time to visit all nodes and return back to source question\n\nSEE THE BELOW MATRIX FOR EXAMPLE\n\n5        8       9\n25    19      10\n99     0       22\n\nOur start point will always be (0,0) but we wanna cut off the smallest tree first of all, so we try to find the walk count from 0,0 to the shortest tree height index, and then we make this tree index as our start index for the next greater tree height and continue on and on\n\n**If you are standing in a cell with a tree, you can choose whether to cut it off.**\nThe important condition is to cut the shortest tree first, and move on to next greater\n```\nclass Solution {\npublic:\n    int BFS(vector<vector<int>> &forest, int sX, int sY, int dX, int dY){\n        int n=forest.size();\n        int m=forest[0].size();\n        int X[4]={1,-1,0,0};\n        int Y[4]={0,0,1,-1};\n        vector<vector<bool>> vis(n,vector<bool>(m,false));\n        queue<pair<int,int>> q;\n        q.push({sX,sY});\n        vis[sX][sY]=true;\n        int res=0;\n        while(!q.empty()){\n            int size=q.size();\n            while(size--){\n                auto curr=q.front();\n                q.pop();\n                if(curr.first==dX && curr.second==dY){\n                    return res;\n                }\n                for(int dir=0;dir<4;dir++){\n                    int newX=curr.first+X[dir];\n                    int newY=curr.second+Y[dir];\n                    if(newX<0 || newY<0 || newX==n || newY==m || forest[newX][newY]==0 || vis[newX][newY]==true){\n                        continue;\n                    }\n                    vis[newX][newY]=true;\n                    q.push({newX,newY});\n                }\n            }\n            res++;\n        }\n        return -1;\n    }\n    \n    int cutOffTree(vector<vector<int>>& forest) {\n        int n=forest.size();\n        int m=forest[0].size();\n        vector<vector<int>> trees;\n        for(int i=0;i<n;i++){\n            for(int j=0;j<m;j++){\n                if(forest[i][j]>1){\n                    trees.push_back({forest[i][j],i,j});\n                }\n            }\n        }\n        sort(trees.begin(),trees.end());\n        int res=0;\n        int startX=0;\n        int startY=0;\n        for(auto tree: trees){\n            int treeX=tree[1];\n            int treeY=tree[2];\n            int distance=BFS(forest,startX,startY,treeX,treeY);\n            if(distance==-1){\n                return -1;\n            }\n            res+=distance;\n            startX=treeX;\n            startY=treeY;\n        }\n        return res;\n    }\n};\n```
['Breadth-First Search', 'C']