C++ solution avoid TLE in 2019
The idea is that find the heights of trees in order as your multiple goals. Then, you go traverse the tree one by one from your goals.\nOriginally, you can easi
The idea is that find the heights of trees in order as your multiple goals. Then, you go traverse the tree one by one from your goals.\nOriginally, you can easily pass by the solution like above method. However, new testcases impose extra computing time so that you might encouter TLE issue.\n\nHere is what I modify.\n1) adopt ```pair<int,int>``` instead of ```vector<int>```;\n2) avoid copying by value operations; this is also common sense.\n3) try allocate your memory by essential operations, such as int x[][] instead;\n\nthe time complexity is O((m*n)\xB2); then, space complexity is O(m*n).\n\nThe following is my implementation. At first section, I fill out my goals, and sort the goals. For that, you can implement it by priority_queue as well. But it might incur extra computing resources like push, pop. Afterwards, at second section, I bfs my shortest path from source to  target spots. After the initated source goal you reach, you pick up the subsequent goal from your candidate goals as your new target, and alter original target into you new source spot. If you cannot get your target for the round, directly return -1; otherwise, you can keep going and accumulating your step.\n\n```\nclass Solution {\n    const int xs[4] = {0, 0,-1,1};\n    const int ys[4] = {-1,1, 0,0};\npublic:\n    int cutOffTree(vector<vector<int>>& forest) {\n        std::ios::sync_with_stdio(false);\n        cin.tie(nullptr);\n        cout.tie(nullptr);\n        if (forest.empty() || forest[0].empty())  return -1;\n        int m = forest.size(),\n                n = forest[0].size();\n\n        vector<vector<int>> goal;\n\n        for(int r=0; r < m; r++) {\n            for(int c=0; c < n; c++) {\n                if(forest[r][c] > 1) {\n                    goal.push_back({r,c});\n                }\n            }\n        }\n\n        sort(begin(goal), end(goal), [&forest](const vector<int>& a, const vector<int> &b) {\n            return forest[a[0]][a[1]] < forest[b[0]][b[1]];\n        });\n\n\n\n        vector<int> start = {0,0};\n        int rst = 0;\n        for(auto &g : goal) {\n            auto cur = bfs(m,n, start, g, forest);\n            if( cur == -1) return -1;\n            rst += cur;\n            start = g;\n        }\n        return rst;\n    }\n\n    int bfs(const int &m, const int& n, vector<int>& src, vector<int>& target, vector<vector<int>>& forest)\n    {\n        bool visited[m][n];\n        memset(visited, 0, sizeof(visited));\n\n        queue<pair<int,int>> q;\n        q.push({src[0],src[1]});\n        visited[src[0]][src[1]] = true;\n        register int step = 0;\n        while(!q.empty()) {\n            int sz = q.size();\n            for(int i=0; i < sz; ++i) {\n                auto from = q.front(); q.pop();\n                if(from.first == target[0] && from.second==target[1]) return step; // to handle the first spot like example 3\n                for(int d = 0; d < 4; ++d) {\n                    auto R = from.first+xs[d],\n                            C = from.second+ys[d];\n                    if(0<= R && R < m && 0 <= C && C < n && !visited[R][C] && forest[R][C] >= 1) {\n                        visited[R][C] = true;\n                        q.push({R,C});\n                        if(R == target[0] && C == target[1]) return step+1;\n                    }\n                }\n            }\n            ++step;\n        }\n        return -1;\n\n    }\n};\n```
[]