Hi, when I try to submit my code, the leetcode return this error:
*** Error in `sandbox run': free(): invalid next size (fast): 0x000000000141f9d0 ***That is my code for 675.
class Solution {
private:
vector<int> d = {-1, 0, 1, 0, -1};
int ans = 0;
int n, m;
struct Compare{
bool operator() (vector<int> &a, vector<int> &b) {
return a[0] < b[0];
}
}cmp;
public:
int cutOffTree(vector<vector<int>>& forest) {
int num_trees = 0;
if (forest.size() == 0) return 0;
if (forest[0][0] == 0) return -1;
m = forest.size(); n = forest[0].size();
vector<vector<int>> points;
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
if (forest[i][j] > 1)
points.push_back({forest[i][j], i, j});
sort(points.begin(), points.end(), cmp);
if (forest[0][0] == 1)
points.insert(points.begin(), {forest[0][0], 0, 0});
for (int i = 0; i<points.size()-1; i++) {
int t = bfs(forest, points[i], points[i+1]);
if (t == -1) return -1;
ans += t;
}
return ans;
}
int bfs(vector<vector<int>>& forest, vector<int> src, vector<int> dst) {
vector<vector<int>> vst(m, vector<int>(n, 0));
queue<vector<int>> q;
q.push({src[1], src[2]});
vst[src[1]][src[2]] = 1;
while(!q.empty()) {
auto top = q.front();
q.pop();
if (top[0] == dst[1] && top[1] == dst[2])
return vst[top[0]][top[1]] - 1;
for (int i = 0; i<4; i++) {
int x = top[0] + d[i], y = top[1] + d[i+1];
if (isvalid(x, y) && forest[x][y] && !vst[x][y]) {
q.push({x, y});
vst[x][y] = vst[top[0]][top[1]] + 1;
}
}
}
return -1;
}
bool isvalid(int x, int y) {
return x >= 0 && x < m && y >= 0 && y < n;
}
};