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

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

struct Point {
  int tx, ty, kx, ky, time;
};

int main() {
  int i, j, k, l, X, Y, G[51][51], ans;
  bool D[51][51][51][51];
  while (1) {
    Point p;
    queue<Point> Q;
    cin >> X >> Y;
    if (X == 0 && Y == 0)
      break;
    cin >> p.tx >> p.ty;
    cin >> p.kx >> p.ky;

    for (i = 1; i <= Y; i++) {
      for (j = 1; j <= X; j++) {
        cin >> G[j][i];
        for (k = 1; k <= X; k++) {
          for (l = 1; l <= Y; l++)
            D[j][k][i][l] = false;
        }
      }
    }

    p.time = 0;
    Q.push(p);
    D[p.tx][p.kx][p.ty][p.ky] = true;
    ans = 0;

    while (!Q.empty()) {
      p = Q.front();
      Q.pop();
      if (p.tx == p.kx && p.ty == p.ky || p.time >= 100) {
        ans = p.time;
        break;
      }

      int x[4] = {1, 0, -1, 0};
      int y[4] = {0, 1, 0, -1};
      Point v;

      for (i = 0; i < 4; i++) {
        int ntx = p.tx + x[i];
        int nty = p.ty + y[i];
        int nkx = p.kx + x[(i + 2) % 4];
        int nky = p.ky + y[(i + 2) % 4];
        if (ntx > 0 && nkx > 0 && ntx <= X && nkx <= X && nty > 0 && nky > 0 &&
            nty <= Y && nky <= Y && !G[ntx][nty] && !G[nkx][nky] &&
            !D[ntx][nkx][nty][nky]) {
          D[ntx][nkx][nty][nky] = true;
          v.tx = ntx;
          v.ty = nty;
          v.kx = nkx;
          v.ky = nky;
          v.time = p.time + 1;
          Q.push(v);
        } else if (nkx > 0 && nkx <= X && nky > 0 && nky <= Y && !G[nkx][nky] &&
                   (ntx <= 0 || ntx > X || nty <= 0 || nty > Y ||
                    G[ntx][nty]) &&
                   !D[p.tx][nkx][p.ty][nky]) {
          D[p.tx][nkx][p.ty][nky] = true;
          v.tx = p.tx;
          v.ty = p.ty;
          v.kx = nkx;
          v.ky = nky;
          v.time = p.time + 1;
          Q.push(v);
        } else if (ntx > 0 && ntx <= X && nty > 0 && nty <= Y && !G[ntx][nty] &&
                   (nkx <= 0 || nkx > X || nky <= 0 || nky > Y ||
                    G[nkx][nky]) &&
                   !D[ntx][p.kx][nty][p.ky]) {
          D[ntx][p.kx][nty][p.ky] = true;
          v.tx = ntx;
          v.ty = nty;
          v.kx = p.kx;
          v.ky = p.ky;
          v.time = p.time + 1;
          Q.push(v);
        }
      }
    }

    if (ans > 0 && ans < 100)
      cout << ans << endl;
    else
      cout << "NA" << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <cstdio>
#include <iostream>
#include <queue>
#include <string>
using namespace std;

struct Point {
  int tx, ty, kx, ky, time;
};

int main() {
  int i, j, k, l, X, Y;
  bool G[51][51], D[5151][5151];
  while (1) {
    Point p;
    queue<Point> Q;
    cin >> X >> Y;
    if (X == 0 && Y == 0)
      break;
    cin >> p.tx >> p.ty;
    cin >> p.kx >> p.ky;

    l = 100;
    for (i = 1; i <= Y; i++) {
      for (j = 1; j <= X; j++) {
        cin >> G[j][i];
        D[j][i] = 0;
        for (k = 100; k <= 100 * X; k++)
          D[k + j][l + i] = false;
      }
      i += 100;
    }

    Q.push(p);
    D[p.tx][p.ty] = 0;
    p.time = 0;

    while (!Q.empty()) {
      p = Q.front();
      Q.pop();
      if (p.tx == p.kx && p.ty == p.ky || p.time >= 100)
        break;
      int x[4] = {1, 0, -1, 0};
      int y[4] = {0, 1, 0, -1};
      Point v;

      for (i = 0; i < 4; i++) {
        int ntx = p.tx + x[i];
        int nty = p.ty + y[i];
        int nkx = p.kx + x[(i + 2) % 4];
        int nky = p.ky + y[(i + 2) % 4];
        if (ntx > 1 && nkx > 1 && ntx <= X && nkx <= X && nty > 1 && nky > 1 &&
            nty <= Y && nky <= Y && !D[ntx + 100 * nkx][nty + 100 * nky] &&
            !G[ntx][nty] && !G[nkx][nky]) {
          D[ntx + 100 * nkx][nty + 100 * nky] = true;
          v.tx = ntx;
          v.ty = nty;
          v.kx = nkx;
          v.ky = nky;
          v.time = p.time + 1;
          Q.push(v);
        } else if (nkx > 1 && nkx <= X && nky > 1 && nky <= Y && !G[nkx][nky] &&
                   (ntx < 1 || ntx > X || nty < 1 || nty > Y || G[ntx][nty]) &&
                   !D[p.tx + 100 * nkx][p.ty + 100 * nky]) {
          D[p.tx + 100 * nkx][p.ty + 100 * nky] = true;
          v.tx = p.tx;
          v.ty = p.ty;
          v.kx = nkx;
          v.ky = nky;
          v.time = p.time + 1;
          Q.push(v);
        } else if (ntx > 1 && ntx <= X && nty > 1 && nty <= Y && !G[ntx][nty] &&
                   (nkx < 1 || nkx > X || nky < 1 || nky > Y || G[nkx][nky]) &&
                   !D[ntx + 100 * p.kx][nty + 100 * p.ky]) {
          D[ntx + 100 * p.kx][nty + 100 * p.ky] = true;
          v.tx = ntx;
          v.ty = nty;
          v.kx = p.kx;
          v.ky = p.ky;
          v.time = p.time + 1;
          Q.push(v);
        }
      }
    }

    if (p.time < 100)
      cout << p.time << endl;
    else
      cout << "NA" << endl;
  }
  return 0;
}