C++ Getting Accepted and TLE with same code
This same sol is submitting TLE and was approved following a few submissions without any changes.\n I\'ve experienced this rather frequently.\n Has anyone encou
*  ***This same sol is submitting TLE and was approved following a few submissions without any changes.***\n*  ***I\'ve experienced this rather frequently.***\n*  ***Has anyone encountered this issue??***\n```\nclass Solution {\n    vector<int> dx{0,0,1,-1},dy{-1,1,0,0};\n    int n,m;\n    int BFS(vector<vector<int>>& forest ,int sx,int sy,int ex, int ey){\n        vector<vector<bool>> vis(n,vector<bool>(m,false));int c = 0;\n        queue<pair<int,int>> q;\n        q.push({sx,sy});\n        vis[sx][sy] = true;\n        \n        while(!q.empty()){\n            int qs = q.size();\n            while(qs--){\n                \n                pair<int,int> p = q.front();q.pop();\n                \n                if(p.first == ex and p.second == ey) return c;\n                for(int i = 0;i<4;i++){\n                    int newX=p.first + dx[i];\n                    int newY=p.second + dy[i];\n                    if(newX<0 || newY<0 || newX==n || newY==m || forest[newX][newY]==0 || vis[newX][newY]==true)continue;\n                    vis[newX][newY]=true;\n                    q.push({newX,newY});\n                }\n            }c++;\n        }\n        return -1;\n    }\npublic:\n    int cutOffTree(vector<vector<int>>& forest) {\n        int ans = 0;n=forest.size(),m=forest[0].size();\n        vector<vector<int>> v;\n        for(int i = 0;i<n;i++){\n            for(int j=0;j<m;j++){\n                if(forest[i][j]>1) v.push_back({forest[i][j],i,j});\n            }\n        }\n        sort(v.begin(),v.end());\n        \n        int sx = 0,sy=0;\n        for(auto it:v){\n            int minStepsForNextTree = BFS(forest,sx,sy,it[1],it[2]);\n            if(minStepsForNextTree==-1) return -1;\n            ans+=minStepsForNextTree;\n            sx = it[1],sy=it[2];\n        }\n        return ans;\n    }\n};\n```
['Breadth-First Search', 'C', 'Sorting']