Problem p01342 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define MAX 110
#define inf 1 << 28
#define f first
#define s second
#define mp make_pair
using namespace std;

int w, h, n, si, sj, gi, gj;
bool grid[MAX][MAX] = {};
int Y[4] = {1, 0, -1, 0};
int X[4] = {0, 1, 0, -1};

int bfs() {
  int d[MAX][MAX];
  for (int i = 0; i < MAX; i++) {
    for (int j = 0; j < MAX; j++)
      d[i][j] = inf;
  }
  d[si][sj] = 0;
  queue<pair<int, int>> q;
  q.push(mp(si, sj));

  while (q.size()) {
    pair<int, int> u = q.front();
    q.pop();
    if (u.f == gi && u.s == gj)
      return d[gi][gj];

    for (int k = 0; k < 4; k++) {
      int a = u.f + Y[k], b = u.s + X[k];
      if (grid[a][b])
        continue;
      a += Y[k];
      b += X[k];
      if (d[u.f][u.s] + 1 < d[a][b]) {
        d[a][b] = d[u.f][u.s] + 1;
        q.push(mp(a, b));
      }
    }
  }
  return -1;
}

int main() {
  int a, b, c, d;

  cin >> w >> h >> n;

  for (int i = 0; i <= h * 2; i++)
    grid[i][0] = grid[i][w * 2] = true;
  for (int i = 0; i <= w * 2; i++)
    grid[0][i] = grid[h * 2][i] = true;

  for (int i = 0; i < n; i++) {
    cin >> a >> b >> c >> d;
    for (int j = min(a, c) * 2; j <= max(a, c) * 2; j++) {
      for (int k = min(b, d) * 2; k <= max(b, d) * 2; k++)
        grid[k][j] = true;
    }
  }
  cin >> sj >> si >> gj >> gi;
  si = si * 2 + 1;
  sj = sj * 2 + 1;
  gi = gi * 2 + 1;
  gj = gj * 2 + 1;

  int dis = bfs(), ans = 0;

  for (int i = 2; i <= h * 2 - 2; i += 2) {
    for (int j = 1; j <= w * 2 - 1; j += 2) {
      if (grid[i][j])
        continue;
      grid[i][j] = true;
      ans = max(bfs() - dis, ans);
      grid[i][j] = false;
    }
  }
  for (int i = 1; i <= h * 2 - 1; i += 2) {
    for (int j = 2; j <= w * 2 - 2; j += 2) {
      if (grid[i][j])
        continue;
      grid[i][j] = true;
      ans = max(bfs() - dis, ans);
      grid[i][j] = false;
    }
  }
  cout << ans << endl;

  return 0;
}

Error: -6
*** stack smashing detected ***: terminated


Fixed submission:

#include <bits/stdc++.h>
#define MAX 200
#define inf 1 << 28
#define f first
#define s second
#define mp make_pair
using namespace std;

int w, h, n, si, sj, gi, gj;
bool grid[MAX][MAX] = {};
int Y[4] = {1, 0, -1, 0};
int X[4] = {0, 1, 0, -1};

void init() {
  for (int i = 0; i < h * 2 + 1; i++) {
    grid[i][0] = true;
    grid[i][w * 2] = true;
  }
  for (int i = 0; i < w * 2 + 1; i++) {
    grid[0][i] = true;
    grid[h * 2][i] = true;
  }
}

int bfs() {
  int d[MAX][MAX];
  for (int i = 0; i <= MAX; i++) {
    for (int j = 0; j <= MAX; j++)
      d[i][j] = inf;
  }
  d[si][sj] = 0;
  queue<pair<int, int>> q;
  q.push(mp(si, sj));

  while (!q.empty()) {
    pair<int, int> u = q.front();
    q.pop();
    if (u.f == gi && u.s == gj)
      return d[u.f][u.s];

    for (int k = 0; k < 4; k++) {
      int a = u.f + Y[k], b = u.s + X[k];
      if (grid[a][b])
        continue;
      a = u.f + 2 * Y[k];
      b = u.s + 2 * X[k];
      if (d[u.f][u.s] + 1 < d[a][b]) {
        d[a][b] = d[u.f][u.s] + 1;
        q.push(mp(a, b));
      }
    }
  }
  return -1;
}

int main() {
  int a, b, c, d;

  cin >> w >> h >> n;
  init();

  for (int i = 0; i < n; i++) {
    cin >> a >> b >> c >> d;
    for (int j = a * 2; j <= c * 2; j++) {
      for (int k = b * 2; k <= d * 2; k++)
        grid[k][j] = true;
    }
  }
  cin >> sj >> si >> gj >> gi;
  si = si * 2 + 1;
  sj = sj * 2 + 1;
  gi = gi * 2 + 1;
  gj = gj * 2 + 1;

  int dis = bfs(), ans = 0;

  for (int i = 2; i <= h * 2 - 2; i++) {
    for (int j = 1; j <= w * 2 - 1; j++) {
      if (grid[i][j])
        continue;
      grid[i][j] = true;
      int ndis = bfs();
      ans = max(ndis - dis, ans);
      grid[i][j] = false;
    }
  }
  for (int i = 1; i <= h * 2 - 1; i++) {
    for (int j = 2; j <= w * 2 - 2; j++) {
      if (grid[i][j])
        continue;
      grid[i][j] = true;
      int ndis = bfs();
      ans = max(ndis - dis, ans);
      grid[i][j] = false;
    }
  }
  cout << ans << endl;
  return 0;
}