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

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

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

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

class NODE {
public:
  int x1;
  int y1;
  int x2;
  int y2;
  int depth;
};

int main() {
  int X, Y, tx, ty, kx, ky;
  bool data[52][52];
  while (cin >> X >> Y, X) {
    cin >> tx >> ty >> kx >> ky;
    rep(i, 52) rep(j, 52) data[i][j] = 1;
    rep(i, Y) rep(j, X) { cin >> data[1 + i][1 + j]; }
    queue<NODE> Q;
    NODE t = {tx, ty, kx, ky, 0};
    Q.push(t);
    bool done[52][52][52][52] = {0};
    while (Q.size()) {
      NODE q = Q.front();
      Q.pop();
      if (q.depth >= 100)
        continue;
      if (done[q.x1][q.y1][q.x2][q.y2])
        continue;
      done[q.x1][q.y1][q.x2][q.y2] = true;

      if (q.x1 == q.x2 && q.y1 == q.y2) {
        cout << q.depth << endl;
        goto end;
      }
      rep(i, 4) {
        NODE next;
        next.x1 = q.x1 + dx[i];
        next.y1 = q.y1 + dy[i];
        next.x2 = q.x2 - dx[i];
        next.y2 = q.y2 - dy[i];
        next.depth = q.depth + 1;
        if (data[next.y1][next.x1] == 1) {
          next.x1 = q.x1;
          next.y1 = q.y1;
        }
        if (data[next.y2][next.x2] == 1) {
          next.x2 = q.x2;
          next.y2 = q.y2;
        }
        Q.push(next);
      }
    }
    cout << "NA" << endl;
  end:;
  }
}

Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int same[55][55][55][55];
struct maigo {
  int x[2]; // ????????????[0]???????????????[1]
  int y[2]; // ????????????[0]???????????????[1]
  int count;
};
int main() {
  queue<maigo> q;
  int tx, ty;
  int kx, ky;
  int **depart, X, Y;
  int flag = 0, a = 0;
  int c = 0;
  int f = 0;
  cin >> X >> Y;
  while (X != 0 && Y != 0) {
    for (int i = 0; i < 55; i++) {
      for (int l = 0; l < 55; l++) {
        for (int i1 = 0; i1 < 55; i1++) {
          for (int l1 = 0; l1 < 55; l1++) {
            same[i][l][i1][l1] = 0;
          }
        }
      }
    }
    depart = new int *[Y + 2];
    for (int i = 0; i < Y + 2; i++) {
      depart[i] = new int[X + 2];
    }
    for (int i = 0; i < Y + 2; i++) {
      for (int l = 0; l < X + 2; l++) {
        depart[i][l] = 3;
      }
    }
    cin >> tx >> ty;
    cin >> kx >> ky;
    for (int i = 1; i < Y + 1; i++) {
      for (int l = 1; l < X + 1; l++) {
        cin >> depart[i][l];
      }
    }
    maigo temp;
    temp.x[0] = tx;
    temp.y[0] = ty;
    temp.x[1] = kx;
    temp.y[1] = ky;
    temp.count = 1;
    q.push(temp);
    while (!q.empty() && flag == 0) {
      maigo t = q.front();
      maigo into;
      q.pop();
      if (t.count >= 100) {
        a = 1;
        flag = 1;
        break;
      }
      int dx1[4] = {1, 0, -1, 0};
      int dx2[4] = {0, 1, 0, -1};
      for (int i = 0; i < 4; i++) {
        if (depart[t.x[0] + dx1[i]][t.y[0] + dx2[i]] == 0 &&
            depart[t.x[1] + dx1[i] * (-1)][t.y[1] + dx2[i] * (-1)] == 0) {
          if (t.x[0] + dx1[i] == t.x[1] + dx1[i] * (-1) &&
              t.y[0] + dx2[i] == t.y[1] + dx2[i] * (-1)) {
            cout << t.count << endl;
            flag = 1;
          }
          into.x[0] = t.x[0] + dx1[i], into.y[0] = t.y[0] + dx2[i];
          into.x[1] = t.x[1] + dx1[i] * (-1),
          into.y[1] = t.y[1] + dx2[i] * (-1);

          into.count = t.count + 1;
          q.push(into);
        }
        if (depart[t.x[0] + dx1[i]][t.y[0] + dx2[i]] ==
                0 && //???????????? ok ???????????? no ?????´???
            depart[t.x[1] + dx1[i] * (-1)][t.y[1] + dx2[i] * (-1)] != 0) {
          if (t.x[0] + dx1[i] == t.x[1] && t.y[0] + dx2[i] == t.y[1]) {
            cout << t.count << endl;
            flag = 1;
          }
          into.x[0] = t.x[0] + dx1[i], into.y[0] = t.y[0] + dx2[i];
          into.x[1] = t.x[1], into.y[1] = t.y[1];

          into.count = t.count + 1;
          q.push(into);
        }
        if (depart[t.x[0] + dx1[i]][t.y[0] + dx2[i]] !=
                0 && //???????????? no??????????????? yes????????´???
            depart[t.x[1] + dx1[i] * (-1)][t.y[1] + dx2[i] * (-1)] == 0) {
          if (t.x[0] == t.x[1] + dx1[i] * (-1) &&
              t.y[0] == t.y[1] + dx2[i] * (-1)) {
            cout << t.count << endl;
            flag = 1;
          }
          into.x[0] = t.x[0], into.y[0] = t.y[0];
          into.x[1] = t.x[1] + dx1[i] * (-1),
          into.y[1] = t.y[1] + dx2[i] * (-1);

          into.count = t.count + 1;
          q.push(into);
        }
      }
    }
    if (a == 1 || flag == 0) {
      cout << "NA" << endl;
    }
    while (!q.empty())
      q.pop();
    a = 0;
    flag = 0;
    cin >> X >> Y;
  }
  return 0;
}