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

#include <cstdio>
#include <queue>
#include <set>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)

inline int getInt() {
  int s;
  scanf("%d", &s);
  return s;
}

#define IN(x, s, g) ((x) >= (s) && (x) < (g))
#define ISIN(x, y, w, h) (IN((x), 0, (w)) && IN((y), 0, (h)))

using namespace std;

int wall[64][64][4];

const int _dx[] = {0, 1, 0, -1};
const int _dy[] = {-1, 0, 1, 0};

enum Dir { DOWN = 0, RIGHT = 1, LEFT = 3, UP = 2 };

int ix, iy;
int ox, oy;
int h, w;

inline int mkid(int x, int y) { return w * y + x; }
inline int getX(int id) { return id % w; }
inline int getY(int id) { return id / w; }

int bfs() {
  queue<int> q;
  vector<int> memo(h * w, -1);

  memo[mkid(ix, iy)] = 0;
  q.push(mkid(ix, iy));

  while (q.size()) {
    int id = q.front();
    q.pop();
    int x = getX(id);
    int y = getY(id);

    if (x == ox && y == oy)
      return memo[id];

    REP(i, 4) {
      int xx = x + _dx[i];
      int yy = y + _dy[i];
      int ii = mkid(xx, yy);
      if (ISIN(xx, yy, w, h) && memo[ii] == -1 && wall[y][x][i] == 0) {
        memo[ii] = memo[id] + 1;
        q.push(ii);
      }
    }
  }

  return -1;
}

int main() {
  w = getInt();
  h = getInt();

  int n = getInt();

  REP(i, n) {
    int sx = getInt();
    int sy = getInt();
    int dx = getInt();
    int dy = getInt();

    if (sx > dx)
      swap(sx, dx);
    if (sy > dy)
      swap(sy, dy);

    if (sx == dx) {
      for (int y = sy; y < dy; y++) {
        wall[y][sx - 1][RIGHT] = 1;
        wall[y][sx][LEFT] = 1;
      }
    } else {
      for (int x = sx; x < dx; x++) {
        wall[sy - 1][x][UP] = 1;
        wall[sy][x][DOWN] = 1;
      }
    }
  }

  ix = getInt();
  iy = getInt();
  ox = getInt();
  oy = getInt();

  int d = bfs();

  int ans = 0;
  REP(i, h) REP(j, w) {
    if (i + 1 < h && wall[i][j][UP] == 0) {
      wall[i][j][UP] = 1;
      wall[i + 1][j][DOWN] = 1;
      ans = max(ans, bfs() - d);
      wall[i][j][UP] = 0;
      wall[i + 1][j][DOWN] = 0;
    }
    if (j + 1 < w && wall[i][j][RIGHT] == 0) {
      wall[i][j][RIGHT] = 1;
      wall[i][j + 1][LEFT] = 1;
      ans = max(ans, bfs() - d);
      wall[i][j][RIGHT] = 0;
      wall[i][j + 1][LEFT] = 0;
    }
  }

  printf("%d\\n", ans);

  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <queue>
#include <set>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)

inline int getInt() {
  int s;
  scanf("%d", &s);
  return s;
}

#define IN(x, s, g) ((x) >= (s) && (x) < (g))
#define ISIN(x, y, w, h) (IN((x), 0, (w)) && IN((y), 0, (h)))

using namespace std;

int wall[50][50][4];

const int _dx[] = {0, 1, 0, -1};
const int _dy[] = {-1, 0, 1, 0};

enum Dir { DOWN = 0, RIGHT = 1, LEFT = 3, UP = 2 };

int ix, iy;
int ox, oy;
int h, w;

inline int mkid(int x, int y) { return w * y + x; }
inline int getX(int id) { return id % w; }
inline int getY(int id) { return id / w; }

int bfs() {
  queue<int> q;
  vector<int> memo(h * w, -1);

  memo[mkid(ix, iy)] = 0;
  q.push(mkid(ix, iy));

  while (q.size()) {
    int id = q.front();
    q.pop();
    int x = getX(id);
    int y = getY(id);

    if (x == ox && y == oy)
      return memo[id];

    REP(i, 4) {
      int xx = x + _dx[i];
      int yy = y + _dy[i];
      int ii = mkid(xx, yy);
      if (ISIN(xx, yy, w, h) && memo[ii] == -1 && wall[y][x][i] == 0) {
        memo[ii] = memo[id] + 1;
        q.push(ii);
      }
    }
  }

  return -1;
}

int main() {
  w = getInt();
  h = getInt();

  int n = getInt();

  REP(i, n) {
    int sx = getInt();
    int sy = getInt();
    int dx = getInt();
    int dy = getInt();

    if (sx > dx)
      swap(sx, dx);
    if (sy > dy)
      swap(sy, dy);

    if (sx == dx) {
      for (int y = sy; y < dy; y++) {
        wall[y][sx - 1][RIGHT] = 1;
        wall[y][sx][LEFT] = 1;
      }
    } else {
      for (int x = sx; x < dx; x++) {
        wall[sy - 1][x][UP] = 1;
        wall[sy][x][DOWN] = 1;
      }
    }
  }

  ix = getInt();
  iy = getInt();
  ox = getInt();
  oy = getInt();

  int d = bfs();

  int ans = 0;
  REP(i, h) REP(j, w) {
    if (i + 1 < h && wall[i][j][UP] == 0) {
      wall[i][j][UP] = 1;
      wall[i + 1][j][DOWN] = 1;
      ans = max(ans, bfs() - d);
      wall[i][j][UP] = 0;
      wall[i + 1][j][DOWN] = 0;
    }
    if (j + 1 < w && wall[i][j][RIGHT] == 0) {
      wall[i][j][RIGHT] = 1;
      wall[i][j + 1][LEFT] = 1;
      ans = max(ans, bfs() - d);
      wall[i][j][RIGHT] = 0;
      wall[i][j + 1][LEFT] = 0;
    }
  }

  printf("%d\\n", ans);

  return 0;
}