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

#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;

const int MAXH = 55;
const int MAXW = 55;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
const int INF = 1 << 28;

int W, H, N;
int G[MAXH][MAXW];

int bfs(int sx, int sy, int tx, int ty) {
  if (sx == tx && sy == ty)
    return 0;
  int cost[MAXH][MAXW];
  queue<pair<int, int>> que;
  fill(cost[0], cost[MAXH], INF);
  cost[sy][sx] = 0;
  que.push(make_pair(sx, sy));
  while (que.size()) {
    pair<int, int> p = que.front();
    que.pop();
    int px = p.first;
    int py = p.second;
    for (int k = 0; k < 4; ++k) {
      if (G[py][px] & (1 << k))
        continue;
      int nx = px + dx[k];
      int ny = py + dy[k];
      if (nx < 0 || nx >= W)
        continue;
      if (ny < 0 || ny >= H)
        continue;
      if (cost[ny][nx] != INF)
        continue;
      cost[ny][nx] = cost[py][px] + 1;
      que.push(make_pair(nx, ny));
      if (nx == tx && ny == ty)
        return cost[ny][nx];
    }
  }
  return INF;
}

int main() {
  fill(G[0], G[MAXH], 0);
  cin >> W >> H >> N;
  for (int k = 0; k < N; ++k) {
    int sx, sy, dx, dy;
    cin >> sx >> sy >> dx >> dy;
    if (sx == dx) {
      for (int i = min(sy, dy); i < max(sy, dy); ++i) {
        if (sx)
          G[i][sx - 1] |= 1 << 0;
        G[i][sx] |= 1 << 2;
      }
    } else {
      for (int i = min(sx, dx); i < max(sx, dx); ++i) {
        if (sy)
          G[sy - 1][i] |= 1 << 1;
        G[sy][i] |= 1 << 3;
      }
    }
  }
  int ix, iy, ox, oy;
  cin >> ix >> iy >> ox >> oy;
  int base = bfs(ix, iy, ox, oy);
  int res = base;
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      for (int k = 0; k < 4; ++k) {
        int ni = i + dy[k];
        int nj = j + dx[k];
        if (ni < 0 || ni >= H)
          continue;
        if (nj < 0 || nj >= W)
          continue;
        if (!(G[i][j] & (1 << k))) {
          G[i][j] |= 1 << k;
          G[ni][nj] |= 1 << ((k + 2) % 4);
          int cost = bfs(ix, iy, ox, oy);
          if (cost != INF) {
            res = max(res, cost);
          }
          G[i][j] &= ~(1 << k);
          G[ni][nj] &= ~(1 << ((k + 2) % 4));
        }
      }
    }
  }
  cout << res - base << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;

const int MAXH = 55;
const int MAXW = 55;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
const int INF = 1 << 28;

int W, H, N;
int G[MAXH][MAXW];

int bfs(int sx, int sy, int tx, int ty) {
  if (sx == tx && sy == ty)
    return 0;

  int cost[MAXH][MAXW];
  queue<pair<int, int>> que;
  fill(cost[0], cost[MAXH], INF);
  cost[sy][sx] = 0;
  que.push(make_pair(sx, sy));
  while (que.size()) {
    pair<int, int> p = que.front();
    que.pop();
    int px = p.first;
    int py = p.second;
    for (int k = 0; k < 4; ++k) {
      if (G[py][px] & (1 << k))
        continue;
      int nx = px + dx[k];
      int ny = py + dy[k];
      if (nx < 0 || nx >= W)
        continue;
      if (ny < 0 || ny >= H)
        continue;
      if (cost[ny][nx] != INF)
        continue;
      cost[ny][nx] = cost[py][px] + 1;
      que.push(make_pair(nx, ny));
      if (nx == tx && ny == ty)
        return cost[ny][nx];
    }
  }
  return INF;
}

int main() {
  fill(G[0], G[MAXH], 0);
  cin >> W >> H >> N;
  for (int k = 0; k < N; ++k) {
    int sx, sy, dx, dy;
    cin >> sx >> sy >> dx >> dy;
    if (sx == dx) {
      for (int i = min(sy, dy); i < max(sy, dy); ++i) {
        G[i][sx - 1] |= 1 << 0;
        G[i][sx] |= 1 << 2;
      }
    } else {
      for (int i = min(sx, dx); i < max(sx, dx); ++i) {
        G[sy - 1][i] |= 1 << 1;
        G[sy][i] |= 1 << 3;
      }
    }
  }
  int ix, iy, ox, oy;
  cin >> ix >> iy >> ox >> oy;
  int base = bfs(ix, iy, ox, oy);
  int res = 0;
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      for (int k = 0; k < 4; ++k) {
        int ni = i + dy[k];
        int nj = j + dx[k];
        if (ni < 0 || ni >= H)
          continue;
        if (nj < 0 || nj >= W)
          continue;
        if (!(G[i][j] & (1 << k))) {
          G[i][j] |= 1 << k;
          G[ni][nj] |= 1 << ((k + 2) % 4);
          int cost = bfs(ix, iy, ox, oy);
          if (cost != INF) {
            res = max(res, cost);
          }
          G[i][j] &= ~(1 << k);
          G[ni][nj] &= ~(1 << ((k + 2) % 4));
        }
      }
    }
  }
  cout << res - base << endl;
  return 0;
}