🚧Easy C++ Solution with Intuition and Approach || PQ + BFS
Intuition\nAs there are trees of different distinct heights and we have to cut them in ascending order of their heights , \n> Its for sure that if we have 4 tre
# Intuition\nAs there are trees of different distinct heights and we have to cut them in ascending order of their heights , \n> Its for sure that if we have 4 trees with heights 1,2,3,4 at different positions in the forest , then we\'ll cut the trees in the order 1->2->3->4 ; \n\nThus, we first sort the heights of the trees using a min-heap pq , and then just sum the distances of moving from the min to the second min and so on until only one element is left in the pq.\n\n> (i..e.. dist 1->2 + dist 2->3 + dist 3->4) , then only one element in pq which is 4 , which is the max height tree of the forest....\n\n# Approach\n- First we store all the trees in a min-heap to attain their ascending sorted order.\n- Then, we just sum the consequent distances of moving from the min height tree to the second min height tree and so on until only one tree is left in the minheap(which is the max height tree, marking that we have cut all the trees now and don\'t need to continue any further).\n- We find these distances of moving from one point to the other using simple bfs\n- Dijikstra\'s algorithm can also be used in order to optimize time but as the constraints are not that strict a simple bfs works for us here.\n\n---\n> Do upvote if helpful , Happy Coding\uD83C\uDF86\n\n---\n\n\n# Code\n```\nclass Solution {\npublic:\n    int x[4]={0,0,1,-1};\n    int y[4]={1,-1,0,0};\n\n    int bfs(vector<vector<int>> &forest,int curri,int currj,int dsti,int dstj,int m,int n){\n        int out=0;\n        queue<pair<int,int>> q;\n        vector<vector<bool>> vis(m,vector<bool>(n,false));\n        q.push({curri,currj});\n        vis[curri][currj]=true;\n        int steps=0;\n\n        while(!q.empty()){\n            steps++;\n            int size=q.size();\n            while(size--){\n                int i=q.front().first;\n                int j=q.front().second;\n                q.pop();\n                if(i==dsti && j==dstj)return steps-1;\n                for(int k=0;k<4;k++){\n                    int nr=i+x[k];\n                    int nc=j+y[k];\n                    if(nr>=0 && nr<m && nc>=0 && nc<n && !vis[nr][nc] && forest[nr][nc]!=0){\n                        if(nr==dsti && nc==dstj)return steps;\n                        q.push({nr,nc});\n                        vis[nr][nc]=true;\n                    }\n                }\n            }\n        }\n\n        return -1;\n    }\n\n    int cutOffTree(vector<vector<int>>& forest) {\n        int m=forest.size();\n        int n=forest[0].size();\n        priority_queue<pair<int,pair<int,int>>,vector<pair<int,pair<int,int>>>,greater<pair<int,pair<int,int>>>> pq;\n        for(int i=0;i<m;i++){\n            for(int j=0;j<n;j++){\n                if(forest[i][j]>1)pq.push({forest[i][j],{i,j}});\n            }\n        }\n        pq.push({1,{0,0}});\n        int out=0;\n        while(pq.size()>1){\n            int curri=pq.top().second.first , currj=pq.top().second.second;\n            pq.pop();\n            int dsti=pq.top().second.first , dstj=pq.top().second.second;\n            int temp=bfs(forest,curri,currj,dsti,dstj,m,n);\n            if(temp==-1)return -1;\n            out+=temp;\n        }\n        return out;\n    }\n};\n```\n![upvote xd.jpeg](https://assets.leetcode.com/users/images/e5e8e93f-0df9-4835-b71a-2a9468920807_1692369668.9972389.jpeg)\n
['Breadth-First Search', 'Heap (Priority Queue)', 'C++']