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

#include <algorithm>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define all(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back
#define each(i, c)                                                             \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define dbg(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;

const int dy[] = {-1, 0, 1, 0}, dx[] = {0, -1, 0, 1};
int h, w, n, sy, sx, gy, gx;
bool tate[60][60], yoko[60][60], v[60][60];

int calc() {
  memset(v, 0, sizeof(v));
  queue<pi> q;
  q.push(mp(sy * w + sx, 0));
  while (!q.empty()) {
    int y = q.front().first / w, x = q.front().first % w;
    int c = q.front().second;
    q.pop();

    if (v[y][x])
      continue;
    v[y][x] = 1;

    if (y == gy && x == gx)
      return c;
    rep(d, 4) {
      int ny = y + dy[d], nx = x + dx[d];
      if (ny < 0 || ny >= h || nx < 0 || nx >= w || v[ny][nx])
        continue;
      if (dy[d] && yoko[max(y, ny)][x])
        continue;
      if (dx[d] && tate[y][max(x, nx)])
        continue;
      q.push(mp(ny * w + nx, c + 1));
    }
  }
  return inf;
}
int main() {
  cin >> w >> h >> n;
  rep(i, n) {
    int x, y, X, Y;
    cin >> x >> y >> X >> Y;
    if (x == X) {
      if (y > Y)
        swap(y, Y);
      while (y < Y)
        tate[y++][x] = 1;
    } else {
      if (x > X)
        swap(x, X);
      while (x < X)
        yoko[y][x++] = 1;
    }
  }
  cin >> sx >> sy >> gx >> gy;
  int ans = 0, cost = calc();
  rep(i, h + 1) rep(j, w + 1) {
    if (!yoko[i][j]) {
      yoko[i][j] = 1;
      int tmp = calc();
      if (tmp != inf)
        ans = max(ans, tmp - cost);
      yoko[i][j] = 0;
    }
    if (!tate[i][j]) {
      tate[i][j] = 1;
      int tmp = calc();
      if (tmp != inf)
        ans = max(ans, tmp - cost);
      tate[i][j] = 0;
    }
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define all(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back
#define each(i, c)                                                             \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define dbg(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;

const int dy[] = {-1, 0, 1, 0}, dx[] = {0, -1, 0, 1};
int h, w, n, sy, sx, gy, gx;
bool tate[60][60], yoko[60][60], v[60][60];

int calc() {
  memset(v, 0, sizeof(v));
  queue<pi> q;
  q.push(mp(sy * w + sx, 0));
  while (!q.empty()) {
    int y = q.front().first / w, x = q.front().first % w;
    int c = q.front().second;
    q.pop();

    if (v[y][x])
      continue;
    v[y][x] = 1;

    if (y == gy && x == gx)
      return c;
    rep(d, 4) {
      int ny = y + dy[d], nx = x + dx[d];
      if (ny < 0 || ny >= h || nx < 0 || nx >= w || v[ny][nx])
        continue;
      if (dy[d] && yoko[max(y, ny)][x])
        continue;
      if (dx[d] && tate[y][max(x, nx)])
        continue;
      q.push(mp(ny * w + nx, c + 1));
    }
  }
  return inf;
}
int main() {
  cin >> w >> h >> n;
  rep(i, n) {
    int x, y, X, Y;
    cin >> x >> y >> X >> Y;
    assert(x == X && y < Y || y == Y && x < X);
    if (x == X)
      while (y < Y)
        tate[y++][x] = 1;
    else
      while (x < X)
        yoko[y][x++] = 1;
  }
  cin >> sx >> sy >> gx >> gy;
  int ans = 0, cost = calc();
  rep(i, h + 1) rep(j, w + 1) {
    if (!yoko[i][j]) {
      yoko[i][j] = 1;
      int tmp = calc();
      if (tmp != inf)
        ans = max(ans, tmp - cost);
      yoko[i][j] = 0;
    }
    if (!tate[i][j]) {
      tate[i][j] = 1;
      int tmp = calc();
      if (tmp != inf)
        ans = max(ans, tmp - cost);
      tate[i][j] = 0;
    }
  }
  cout << ans << endl;
  return 0;
}