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

#include <bits/stdc++.h>
using namespace std;

int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};

bool f[405][405][2];
int d[405][405];
int H, W, M, Q, h, w;
int ax[100], ay[100], bx[100], by[100];
map<int, int> X, Y;

int solve(int sx, int sy, int tx, int ty) {
  memset(d, 1, sizeof(d));
  deque<int> qx, qy;
  qx.push_front(sx);
  qy.push_front(sy);
  d[sx][sy] = 0;
  while (!qx.empty()) {
    int px = qx.front();
    qx.pop_front();
    int py = qy.front();
    qy.pop_front();
    // cout<<px<<' '<<py<<endl;
    if (px == tx && py == ty)
      return d[px][py];
    for (int i = 0; i < 4; i++) {
      int nx = px + dx[i];
      int ny = py + dy[i];
      if (nx <= 0 || ny <= 0)
        continue;
      if (w <= nx || h <= ny)
        continue;
      int flg = 0;
      if (i < 2) {
        if (f[px][py][i])
          flg = 1;
      } else {
        if (f[nx][ny][i - 2])
          flg = 1;
      }
      if (d[px][py] + flg < d[nx][ny]) {
        d[nx][ny] = d[px][py] + flg;
        if (flg) {
          qx.push_back(nx);
          qy.push_back(ny);
        } else {
          qx.push_front(nx);
          qy.push_front(ny);
        }
      }
    }
  }
  return -1;
}

void func(map<int, int> &X) {
  map<int, int>::iterator it;
  int C = 0;
  for (it = X.begin(); it != X.end(); ++it)
    X[it->first] = ++C;
}

int sx[105], sy[105];
int tx[105], ty[105];

int main() {
  scanf("%d %d %d", &W, &H, &M);
  for (int i = 0; i < M; i++) {
    scanf("%d %d %d %d", &ax[i], &ay[i], &bx[i], &by[i]);
    if (ax[i] > bx[i])
      swap(ax[i], bx[i]);
    if (ay[i] > by[i])
      swap(ay[i], by[i]);
    X[ax[i]] = Y[ay[i]] = i;
    X[bx[i]] = Y[by[i]] = i;
  }
  X[0] = Y[0] = 1;
  X[W] = Y[H] = M;
  scanf("%d", &Q);
  for (int i = 0; i < Q; i++) {
    scanf("%d %d %d %d", &sx[i], &sy[i], &tx[i], &ty[i]);
    X[sx[i]] = Y[sy[i]] = i;
    X[tx[i]] = Y[ty[i]] = i;
  }
  func(X);
  func(Y);
  w = X[W], h = Y[H];
  for (int i = 0; i < M; i++) {
    int AX = X[ax[i]], AY = Y[ay[i]];
    int BX = X[bx[i]], BY = Y[by[i]];
    if (AX == BX) {
      for (int y = AY; y < BY; y++)
        f[AX][y][0] = true;
    } else {
      for (int x = AX; x < BX; x++)
        f[x][AY][1] = true;
    }
  }
  for (int i = 0; i < Q; i++) {
    sx[i] = X[sx[i]];
    sy[i] = Y[sy[i]];
    tx[i] = X[tx[i]];
    ty[i] = Y[ty[i]];
    printf("%d\\n", solve(sx[i], sy[i], tx[i], ty[i]));
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};

bool f[305][305][2];
int d[305][305];
int H, W, M, Q, h, w;
int ax[100], ay[100], bx[100], by[100];
map<int, int> X, Y;

int solve(int sx, int sy, int tx, int ty) {
  memset(d, 1, sizeof(d));
  deque<int> qx, qy;
  qx.push_front(sx);
  qy.push_front(sy);
  d[sx][sy] = 0;
  while (!qx.empty()) {
    int px = qx.front();
    qx.pop_front();
    int py = qy.front();
    qy.pop_front();
    // cout<<px<<' '<<py<<endl;
    if (px == tx && py == ty)
      return d[px][py];
    for (int i = 0; i < 4; i++) {
      int nx = px + dx[i];
      int ny = py + dy[i];
      if (nx <= 0 || ny <= 0)
        continue;
      if (w <= nx || h <= ny)
        continue;
      int flg = 0;
      if (i < 2) {
        if (f[px][py][i])
          flg = 1;
      } else {
        if (f[nx][ny][i - 2])
          flg = 1;
      }
      if (d[px][py] + flg < d[nx][ny]) {
        d[nx][ny] = d[px][py] + flg;
        if (flg) {
          qx.push_back(nx);
          qy.push_back(ny);
        } else {
          qx.push_front(nx);
          qy.push_front(ny);
        }
      }
    }
  }
  return -1;
}

void func(map<int, int> &X) {
  map<int, int>::iterator it;
  int C = 0;
  for (it = X.begin(); it != X.end(); ++it)
    X[it->first] = ++C;
}

int sx[105], sy[105];
int tx[105], ty[105];

int main() {
  scanf("%d %d %d", &W, &H, &M);
  for (int i = 0; i < M; i++) {
    scanf("%d %d %d %d", &ax[i], &ay[i], &bx[i], &by[i]);
    if (ax[i] > bx[i])
      swap(ax[i], bx[i]);
    if (ay[i] > by[i])
      swap(ay[i], by[i]);
    X[ax[i]] = Y[ay[i]] = i;
    X[bx[i]] = Y[by[i]] = i;
  }
  X[0] = Y[0] = 1;
  X[W] = Y[H] = M;
  scanf("%d", &Q);
  for (int i = 0; i < Q; i++) {
    scanf("%d %d %d %d", &sx[i], &sy[i], &tx[i], &ty[i]);
    X[sx[i]] = Y[sy[i]] = i;
    X[tx[i]] = Y[ty[i]] = i;
  }
  func(X);
  func(Y);
  w = X[W], h = Y[H];
  for (int i = 0; i < M; i++) {
    int AX = X[ax[i]], AY = Y[ay[i]];
    int BX = X[bx[i]], BY = Y[by[i]];
    if (AX == BX) {
      for (int y = AY; y < BY; y++)
        f[AX][y][0] = true;
    } else {
      for (int x = AX; x < BX; x++)
        f[x][AY][1] = true;
    }
  }
  for (int i = 0; i < Q; i++) {
    sx[i] = X[sx[i]];
    sy[i] = Y[sy[i]];
    tx[i] = X[tx[i]];
    ty[i] = Y[ty[i]];
    printf("%d\\n", solve(sx[i], sy[i], tx[i], ty[i]));
  }
  return 0;
}