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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <map>
#include <queue>
#include <stack>
#include <utility>
#include <vector>
#define pb push_back
#define sz(x) (int)x.size()
#define scf scanf
#define ptf printf
#define forp(i, j, k) for (int i = j; i < k; i++)
#define form(i, j, k) for (int i = j; i > k; i--)
using namespace std;
typedef long long LL;
const int N = 51, dx[5] = {0, -1, 1, 0, 0}, dy[5] = {0, 0, 0, -1, 1};

struct node {
  int x, y;
} q[N * N];

int u[N][N], d[N][N], wl[N][N][5], ix, iy, ox, oy, w, h;

int bfs() {
  memset(u, 0, sizeof(u));
  q[0].x = ix, q[0].y = iy;
  u[ix][iy] = 1;
  d[ix][iy] = 0;
  for (int l = 0, r = 1; l < r; l++) {
    int x = q[l].x, y = q[l].y;
    if (x == ox && y == oy)
      return d[ox][oy];
    forp(i, 1, 5) {
      int xx = x + dx[i], yy = y + dy[i];
      if (!wl[x][y][i] && xx >= 0 && xx < w && yy >= 0 && yy < h &&
          !u[xx][yy]) {
        q[r].x = xx;
        q[r].y = yy;
        r++;
        d[xx][yy] = d[x][y] + 1;
        u[xx][yy] = 1;
      }
    }
  }
  return -1;
}

int main() {
  int x1, y1, x2, y2, n;
  scf("%d%d%d", &w, &h, &n);
  forp(i, 0, n) {
    scf("%d%d%d%d", &x1, &y1, &x2, &y2);
    if (x1 == x2 && (x1 == 0 || x1 == w))
      continue;
    if (y1 == y2 && (y1 == 0 || y1 == h))
      continue;
    if (x1 == x2)
      forp(j, min(y1, y2), max(y1, y2)) wl[x1 - 1][j][2] = wl[x1][j][1] = 1;
    if (y1 == y2)
      forp(j, min(x1, x2), max(x1, x2)) wl[j][y1 - 1][4] = wl[j][y1][3] = 1;
  }
  scf("%d%d%d%d", &ix, &iy, &ox, &oy);
  int tans, ans;
  tans = ans = bfs();
  forp(i, 0, w) forp(j, 0, h) {
    if (i != w - 1 && !wl[i][j][2]) {
      wl[i][j][2] = wl[i + 1][j][1] = 1;
      tans = max(tans, bfs());
      wl[i][j][2] = wl[i + 1][j][1] = 0;
    }
    if (j != h - 1 && !wl[i][j][4]) {
      wl[i][j][4] = wl[i][j + 1][3] = 1;
      tans = max(tans, bfs());
      wl[i][j][4] = wl[i][j + 1][3] = 0;
    }
  }
  ptf("%d\\n", tans - ans);
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <map>
#include <queue>
#include <stack>
#include <utility>
#include <vector>
#define pb push_back
#define sz(x) (int)x.size()
#define scf scanf
#define ptf printf
#define forp(i, j, k) for (int i = j; i < k; i++)
#define form(i, j, k) for (int i = j; i > k; i--)
using namespace std;
typedef long long LL;
const int N = 51, dx[5] = {0, -1, 1, 0, 0}, dy[5] = {0, 0, 0, -1, 1};

struct node {
  int x, y;
} q[N * N];

int u[N][N], d[N][N], wl[N][N][5], ix, iy, ox, oy, w, h;

int bfs() {
  memset(u, 0, sizeof(u));
  q[0].x = ix, q[0].y = iy;
  u[ix][iy] = 1;
  d[ix][iy] = 0;
  for (int l = 0, r = 1; l < r; l++) {
    int x = q[l].x, y = q[l].y;
    if (x == ox && y == oy)
      return d[ox][oy];
    forp(i, 1, 5) {
      int xx = x + dx[i], yy = y + dy[i];
      if (!wl[x][y][i] && xx >= 0 && xx < w && yy >= 0 && yy < h &&
          !u[xx][yy]) {
        q[r].x = xx;
        q[r].y = yy;
        r++;
        d[xx][yy] = d[x][y] + 1;
        u[xx][yy] = 1;
      }
    }
  }
  return -1;
}

int main() {
  int x1, y1, x2, y2, n;
  scf("%d%d%d", &w, &h, &n);
  forp(i, 0, n) {
    scf("%d%d%d%d", &x1, &y1, &x2, &y2);
    if (x1 == x2)
      forp(j, min(y1, y2), max(y1, y2)) wl[x1 - 1][j][2] = wl[x1][j][1] = 1;
    if (y1 == y2)
      forp(j, min(x1, x2), max(x1, x2)) wl[j][y1 - 1][4] = wl[j][y1][3] = 1;
  }
  scf("%d%d%d%d", &ix, &iy, &ox, &oy);
  int tans, ans;
  tans = ans = bfs();
  forp(i, 0, w) forp(j, 0, h) {
    if (i != w - 1 && !wl[i][j][2]) {
      wl[i][j][2] = wl[i + 1][j][1] = 1;
      tans = max(tans, bfs());
      wl[i][j][2] = wl[i + 1][j][1] = 0;
    }
    if (j != h - 1 && !wl[i][j][4]) {
      wl[i][j][4] = wl[i][j + 1][3] = 1;
      tans = max(tans, bfs());
      wl[i][j][4] = wl[i][j + 1][3] = 0;
    }
  }
  ptf("%d\\n", tans - ans);
  return 0;
}