C++ || Solution approach || Commented BFS Code || Beats 95%  at time of submission
Solution Approach:\nSince it is given that we need to visit node in increasing order. A simple direct BFS cannot solve our problem, because we might need to vis
**Solution Approach:**\nSince it is given that we need to visit node in increasing order. A simple direct BFS cannot solve our problem, because we might need to visit single cell multiple times. \nExample: in below input, we will first move to 2 (1->3->2)[2 steps] , then from 2 we will move to 3(2->3)[1 step], then to 4,5,6,7,8 with one step each. so ans will be 2 + 1 + 1 + 1 + 1 + 1 + 1 = 8\n```\n\t\t1       3        2 \n\t\t0       4        5\n\t\t8       7        6\n```\n\nActual idea to solve this problem is move from first point to second, determine the steps required using BFS, then move from second point to third, again use BFS to find number of steps and so on. In end add all the steps required, which will be our final answer.\n\n**Algo:**\n1. Figure out all the point present in grid that are greater than 1 and needs to be visited.(save in vector)\n2. sort the vector obtained above. We will move as per this vector values.\n3. Apply standard BFS to determine steps required to move from [0,0] (point A) to cell with value = vector[0] (point B).\n4. Now make point A = point B and point B = vector[1] and apply BFS again, to determine steps required.\n5. Follow step 4 till all the vector is parsed or if one of the point is not reachable.\n6. Add all the steps obtained in step 4 and return as answer, if all points are reachable, else return -1.\n\n```\nclass Solution {\npublic:\n    int cutOffTree(vector<vector<int>>& f) {\n      \n        /* Way to move in 4 direction */\n        int dx[] = {1,0,-1,0};\n        int dy[] = {0,1,0,-1};\n        \n        /* keeping a copy of original forest */\n        vector<vector<int>> orig(f.begin(),f.end());\n        \n        vector<int> p;\n        \n        /* store all the values that are greater than 1 */\n        for(int i = 0 ; i < f.size(); i++ )\n        {\n            for(int j = 0; j < f[0].size(); j++)\n            {\n                if(f[i][j] > 1 )\n                {\n                    p.push_back(f[i][j]);\n                }\n            }\n        }\n        \n        /*sort the element, we will visit each node one by one in this order */\n        sort(p.begin(),p.end());\n        \n        int ans = 0;\n        \n        int x = 0;  /* x coordinate */\n        int y = 0;  /* y coordinate */\n        int ans1 = 0; /* steps required to move from point A to point B */\n        int vis = 0;  /* Indicate whether it is possible to reach some point or not */\n        int flag = 0; /* variable to break out of two loop,as goto is not a right option */\n        \n        \n        /* outer loop will run for all values that needs to be visited. */\n        for(int i = 0 ; i < p.size(); i++)\n        {\n            vis = 0;\n            int val = p[i];\n           \n            queue<pair<int,int>> q;\n            q.push({x,y});\n            \n            /* our standard BFS implementation with just one tweak \n               -> break out of loop when our val is found and move on to next value. \n             */\n            while(!q.empty())\n            {\n                \n                if(val == orig[q.front().first][q.front().second])\n                {         \n                    x = q.front().first;   /* setting up coordinate for next iteration */\n                    y = q.front().second;\n                    vis = 1;   /* if val is found, vis is made one otherwise we can directly                                      return -1 */\n                    break;\n                }\n               \n                ans1++; /* increasing step count for bfs */\n                flag = 0;\n                int size = q.size();\n             \n                while(size--)\n                {\n                    \n                    pair<int,int> temp = q.front();\n                    q.pop();\n                    x = temp.first;\n                    y = temp.second;\n                   \n                    for(int j = 0 ;j < 4; j++)\n                    {\n                        int newX = x + dx[j];\n                        int newY = y + dy[j];\n                        \n                        if(newX >= 0 && newX < f.size() && newY >= 0 && newY < f[0].size() &&\n                          f[newX][newY] >= 1)\n                        {\n                            if(val == orig[newX][newY])\n                            {         \n                                vis = 1;    /* if val is found, vis is made one otherwise we                                                   can directly return -1 */\n                                x = newX;   /* setting up coordinate for next iteration */\n                                y = newY;\n                                flag = 1;\n                                break;\n                            }\n                            q.push({newX,newY});\n                            f[newX][newY] = 0;/*making it 0 so our bfs will converge quicky */\n                        }\n                    }\n                    if(flag)\n                    {\n                        break;\n                    }\n                }\n                if(flag)\n                    break;\n            }\n            \n            ans = ans + ans1; /* adding to the final result */\n            \n            /* copying original value to variable for next iterations*/\n            flag = 0;\n            f = orig;\n            ans1 = 0;\n            \n            if(vis == 0)  /* there is no path to reach a given point , return -1*/\n                return -1;\n                 \n        }\n        return ans;\n        \n    }\n};\n```
['Breadth-First Search', 'C', 'Sorting']