Problem p00223 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
#include <queue>
#include <string.h>
using namespace std;
struct l {
  int tx;
  int ty;
  int kx;
  int ky;
  int cnt;
};
queue<l> p;
bool ps[50][50][50][50] = {};
int main() {
  int x, y, tlx, tly, klx, kly, d[50][50], i, j, cl,
      tmx[4] = {0, 1, 0, -1}, tmy[4] = {-1, 0, 1, 0}, kmx[4] = {0, -1, 0, 1},
      kmy[4] = {1, 0, -1, 0}, k, z;
  l v;
  while (1) {
    cin >> x >> y;
    if (x == 0 && y == 0)
      break;
    cin >> tlx >> tly;
    cin >> klx >> kly;
    for (i = 0; i < y; i++) {
      for (j = 0; j < x; j++) {
        cin >> d[i][j];
      }
    }
    memset(ps, 0, sizeof(ps));
    v.tx = tlx - 1;
    v.ty = tly - 1;
    v.kx = klx - 1;
    v.ky = kly - 1;
    v.cnt = 0;
    p.push(v);
    while (1) {
      tlx = p.front().tx;
      tly = p.front().ty;
      klx = p.front().kx;
      kly = p.front().ky;
      cl = p.front().cnt;
      p.pop();
      if (tlx == klx && tly == kly || cl >= 100) {
        while (!p.empty()) {
          p.pop();
        }
        break;
      }
      if (ps[tlx][tly][klx][kly]) {
        if (!p.empty())
          continue;
        else
          break;
      } else {
        ps[tlx][tly][klx][kly] = true;
      }
      for (i = 0; i < 4; i++) {
        if (d[tly + tmy[i]][tlx + tmx[i]] == 0 && (tlx + tmx[i]) >= 0 &&
            (tlx + tmx[i]) < x && (tly + tmy[i]) >= 0 && (tly + tmy[i]) < y) {
          v.tx = tlx + tmx[i];
          v.ty = tly + tmy[i];
        } else {
          v.tx = tlx;
          v.ty = tly;
        }
        if (d[kly + kmy[i]][klx + kmx[i]] == 0 && (klx + kmx[i]) >= 0 &&
            (klx + kmx[i]) < x && (kly + kmy[i]) >= 0 && (kly + kmy[i]) < y) {
          v.kx = klx + kmx[i];
          v.ky = kly + kmy[i];
        } else {
          v.kx = klx;
          v.ky = kly;
        }
        v.cnt = cl + 1;
        p.push(v);
      }
    }
    if (tlx == klx && tly == kly && cl != 0) {
      cout << cl << endl;
    } else {
      cout << "NA" << endl;
    }
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <queue>
using namespace std;
struct l {
  int tx;
  int ty;
  int kx;
  int ky;
  int cnt;
};
queue<l> p;
int ps[50505050] = {};
int main() {
  int x, y, tlx, tly, klx, kly, d[50][50], i, j, cl,
      tmx[4] = {0, 1, 0, -1}, tmy[4] = {-1, 0, 1, 0}, kmx[4] = {0, -1, 0, 1},
      kmy[4] = {1, 0, -1, 0}, k, z;
  l v;
  while (1) {
    cin >> x >> y;
    if (x == 0 && y == 0)
      break;
    cin >> tlx >> tly;
    cin >> klx >> kly;
    for (i = 0; i < y; i++) {
      for (j = 0; j < x; j++) {
        cin >> d[i][j];
      }
    }
    v.tx = tlx - 1;
    v.ty = tly - 1;
    v.kx = klx - 1;
    v.ky = kly - 1;
    v.cnt = 0;
    p.push(v);
    while (1) {
      tlx = p.front().tx;
      tly = p.front().ty;
      klx = p.front().kx;
      kly = p.front().ky;
      cl = p.front().cnt;
      p.pop();
      if (tlx == klx && tly == kly || cl >= 100) {
        while (!p.empty()) {
          p.pop();
        }
        for (i = 0; i < 50505050; i++) {
          ps[i] = 0;
        }
        break;
      }
      if (ps[tlx * 1000000 + tly * 10000 + klx * 100 + kly] == 1) {
        if (!p.empty())
          continue;
        else
          break;
      } else {
        ps[tlx * 1000000 + tly * 10000 + klx * 100 + kly] = 1;
      }
      for (i = 0; i < 4; i++) {
        if (d[tly + tmy[i]][tlx + tmx[i]] == 0 && (tlx + tmx[i]) >= 0 &&
            (tlx + tmx[i]) < x && (tly + tmy[i]) >= 0 && (tly + tmy[i]) < y) {
          v.tx = tlx + tmx[i];
          v.ty = tly + tmy[i];
        } else {
          v.tx = tlx;
          v.ty = tly;
        }
        if (d[kly + kmy[i]][klx + kmx[i]] == 0 && (klx + kmx[i]) >= 0 &&
            (klx + kmx[i]) < x && (kly + kmy[i]) >= 0 && (kly + kmy[i]) < y) {
          v.kx = klx + kmx[i];
          v.ky = kly + kmy[i];
        } else {
          v.kx = klx;
          v.ky = kly;
        }
        v.cnt = cl + 1;
        p.push(v);
      }
    }
    if (tlx == klx && tly == kly && cl != 0) {
      cout << cl << endl;
    } else {
      cout << "NA" << endl;
    }
  }
  return 0;
}