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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define all(c) c.begin(), c.end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
using namespace std;
typedef pair<int, int> P;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int W, H, N, x, y, X, Y;
bool e[50][50][4];
int bfs() {
  int d[50][50] = {};
  rep(i, W) rep(j, H) d[i][j] = -1;
  d[x][y] = 0;
  queue<P> que;
  que.push(P(x, y));
  while (!que.empty()) {
    P p = que.front();
    que.pop();
    int x = p.fs, y = p.sc;
    rep(di, 4) {
      int nx = x + dx[di], ny = y + dy[di];
      if (e[x][y][di] && d[nx][ny] < 0) {
        d[nx][ny] = d[x][y] + 1;
        que.push(P(nx, ny));
      }
    }
  }
  return d[X][Y];
}
int main() {
  cin >> W >> H >> N;
  rep(i, W) rep(j, H) {
    rep(d, 4) {
      if (0 <= i + dx[d] && i + dx[d] < W && 0 <= j + dy[d] && j + dy[d] < H)
        e[i][j][d] = 1;
    }
  }
  rep(i, N) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    if (a == c) {
      if (b > d)
        swap(b, d);
      for (int j = b; j < d; j++) {
        if (a >= 1)
          e[a - 1][j][0] = 0;
        if (a < W)
          e[a][j][2] = 0;
      }
    } else {
      if (a > c)
        swap(a, c);
      for (int j = a; j < c; j++) {
        if (b >= 1)
          e[j][b - 1][1] = 0;
        if (b < H)
          e[j][b][3] = 0;
      }
    }
  }
  cin >> x >> y >> X >> Y;
  int D = bfs(), d = D;
  rep(i, W) rep(j, H) rep(k, 2) {
    if (e[i][j][k]) {
      e[i][j][k] = e[i + dx[k]][j + dy[k]][k + 2] = 0;
      chmax(d, bfs());
      e[i][j][k] = e[i + dx[k]][j + dy[k]][k + 2] = 1;
    }
  }
  cout << d - D << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define all(c) c.begin(), c.end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
using namespace std;
typedef pair<int, int> P;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int W, H, N, x, y, X, Y;
bool e[50][50][4];
int bfs() {
  int d[50][50] = {};
  rep(i, W) rep(j, H) d[i][j] = -1;
  d[x][y] = 0;
  queue<P> que;
  que.push(P(x, y));
  while (!que.empty()) {
    P p = que.front();
    que.pop();
    int x = p.fs, y = p.sc;
    rep(di, 4) {
      int nx = x + dx[di], ny = y + dy[di];
      if (e[x][y][di] && d[nx][ny] < 0) {
        d[nx][ny] = d[x][y] + 1;
        que.push(P(nx, ny));
      }
    }
  }
  return d[X][Y];
}
int main() {
  cin >> W >> H >> N;
  rep(i, W) rep(j, H) {
    rep(d, 4) {
      if (0 <= i + dx[d] && i + dx[d] < W && 0 <= j + dy[d] && j + dy[d] < H)
        e[i][j][d] = 1;
    }
  }
  rep(i, N) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    if (a == c) {
      if (b > d)
        swap(b, d);
      for (int j = b; j < d; j++)
        e[a - 1][j][0] = e[a][j][2] = 0;
    } else {
      if (a > c)
        swap(a, c);
      for (int j = a; j < c; j++)
        e[j][b - 1][1] = e[j][b][3] = 0;
    }
  }
  cin >> x >> y >> X >> Y;
  int D = bfs(), d = D;
  rep(i, W) rep(j, H) rep(k, 2) {
    if (e[i][j][k]) {
      e[i][j][k] = e[i + dx[k]][j + dy[k]][k + 2] = 0;
      chmax(d, bfs());
      e[i][j][k] = e[i + dx[k]][j + dy[k]][k + 2] = 1;
    }
  }
  cout << d - D << endl;
}