Problem p01342 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <queue>

#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

const int INF = 1 << 29;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};

int bfs(int h, int w, const bool B[99][99], int sy, int sx, int ty, int tx) {
  int d[99][99];
  rep(i, h) rep(j, w) d[i][j] = INF;
  d[sy][sx] = 0;

  queue<int> Q;
  Q.push(sy * w + sx);
  while (!Q.empty()) {
    int y = Q.front() / w, x = Q.front() % w;
    Q.pop();
    rep(k, 4) {
      int yy = y + dy[k], xx = x + dx[k];
      if (0 <= yy && yy < h && 0 <= xx && xx < w && !B[yy][xx] &&
          d[yy][xx] > d[y][x] + 1) {
        d[yy][xx] = d[y][x] + 1;
        Q.push(yy * w + xx);
      }
    }
  }
  return d[ty][tx];
}

int main() {
  int w, h, n;
  scanf("%d%d%d", &w, &h, &n);
  int wall[1000][4];
  rep(i, n) rep(j, 4) scanf("%d", wall[i] + j);
  int sx, sy, tx, ty;
  scanf("%d%d%d%d", &sx, &sy, &tx, &ty);
  sx *= 2;
  sy *= 2;
  tx *= 2;
  ty *= 2;

  bool B[99][99] = {};
  rep(i, h - 1) rep(j, w - 1) B[2 * i + 1][2 * j + 1] = true;
  rep(i, n) {
    if (wall[i][0] == wall[i][2]) { // ツ縦ツづ個陛?
      for (int y = min(wall[i][1], wall[i][3]); y < max(wall[i][1], wall[i][3]);
           y++) {
        if (0 < wall[i][0] && wall[i][0] < w) {
          B[2 * y][2 * wall[i][0] - 1] = true;
        }
      }
    } else { // ツ可。ツづ個陛?
      for (int x = min(wall[i][0], wall[i][2]); x < max(wall[i][0], wall[i][2]);
           x++) {
        if (0 < wall[i][0] && wall[i][1] < h) {
          B[2 * wall[i][1] - 1][2 * x] = true;
        }
      }
    }
  }
  h = 2 * h - 1;
  w = 2 * w - 1;

  int d0 = bfs(h, w, B, sy, sx, ty, tx), d = d0;
  rep(i, h) rep(j, w) if ((i % 2 == 1 || j % 2 == 1) && !B[i][j]) {
    B[i][j] = true;
    int tmp = bfs(h, w, B, sy, sx, ty, tx);
    if (tmp < INF)
      d = max(d, tmp);
    B[i][j] = false;
  }
  printf("%d\n", (d - d0) / 2);

  return 0;
}

Fixed submission:
#include <cstdio>
#include <queue>

#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

const int INF = 1 << 29;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};

int bfs(int h, int w, const bool B[99][99], int sy, int sx, int ty, int tx) {
  int d[99][99];
  rep(i, h) rep(j, w) d[i][j] = INF;
  d[sy][sx] = 0;

  queue<int> Q;
  Q.push(sy * w + sx);
  while (!Q.empty()) {
    int y = Q.front() / w, x = Q.front() % w;
    Q.pop();
    rep(k, 4) {
      int yy = y + dy[k], xx = x + dx[k];
      if (0 <= yy && yy < h && 0 <= xx && xx < w && !B[yy][xx] &&
          d[yy][xx] > d[y][x] + 1) {
        d[yy][xx] = d[y][x] + 1;
        Q.push(yy * w + xx);
      }
    }
  }
  return d[ty][tx];
}

int main() {
  int w, h, n;
  scanf("%d%d%d", &w, &h, &n);
  int wall[1000][4];
  rep(i, n) rep(j, 4) scanf("%d", wall[i] + j);
  int sx, sy, tx, ty;
  scanf("%d%d%d%d", &sx, &sy, &tx, &ty);
  sx *= 2;
  sy *= 2;
  tx *= 2;
  ty *= 2;

  bool B[99][99] = {};
  rep(i, h - 1) rep(j, w - 1) B[2 * i + 1][2 * j + 1] = true;
  rep(i, n) {
    if (wall[i][0] == wall[i][2]) { // ツ縦ツづ個陛?
      for (int y = min(wall[i][1], wall[i][3]); y < max(wall[i][1], wall[i][3]);
           y++) {
        if (0 < wall[i][0] && wall[i][0] < w) {
          B[2 * y][2 * wall[i][0] - 1] = true;
        }
      }
    } else { // ツ可。ツづ個陛?
      for (int x = min(wall[i][0], wall[i][2]); x < max(wall[i][0], wall[i][2]);
           x++) {
        if (0 < wall[i][1] && wall[i][1] < h) {
          B[2 * wall[i][1] - 1][2 * x] = true;
        }
      }
    }
  }
  h = 2 * h - 1;
  w = 2 * w - 1;

  int d0 = bfs(h, w, B, sy, sx, ty, tx), d = d0;
  rep(i, h) rep(j, w) if ((i % 2 == 1 || j % 2 == 1) && !B[i][j]) {
    B[i][j] = true;
    int tmp = bfs(h, w, B, sy, sx, ty, tx);
    if (tmp < INF)
      d = max(d, tmp);
    B[i][j] = false;
  }
  printf("%d\n", (d - d0) / 2);

  return 0;
}