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

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

int x, y, tx, ty, kx, ky, cou;
int mas[51][51];
int ans = 0;
bool used[51][51][51][51];
int dx[] = {-1, 0, 0, 1};
int dy[] = {0, -1, 1, 0};
struct P {
  int tx, ty, kx, ky, cou;
  P(int tx, int ty, int kx, int ky, int cou)
      : tx(tx), ty(ty), kx(kx), ky(ky), cou(cou) {}
};
int bfs();

int main() {
  while (1) {
    cin >> x >> y;
    if (x == 0 && y == 0)
      break;
    memset(used, false, sizeof(used));
    cin >> tx >> ty;
    cin >> kx >> ky;
    for (int i = 0; i < y; i++) {
      for (int j = 0; j < x; j++) {
        cin >> mas[i][j];
      }
    }
    ans = bfs();
    if (ans == -1)
      cout << "NA" << endl;
    else
      cout << ans << endl;
  }
}

int bfs() {
  queue<P> que;
  que.push(P(tx - 1, ty - 1, kx - 1, ky - 1, 0));
  used[tx - 1][ty - 1][kx - 1][ky - 1] = true;
  while (!que.empty()) {
    P p = que.front();
    if (p.tx == p.kx && p.ty == p.ky)
      return p.cou;
    else if (p.cou >= 100)
      return -1;
    for (int i = 0; i < 4; i++) {
      int ntx = p.tx + dx[i];
      int nty = p.ty + dy[i];
      int nkx = p.kx + dx[3 - i];
      int nky = p.ky + dy[3 - i];
      if (ntx < 0 || ntx >= x || nty < 0 || nty >= y || mas[nty][ntx] == 1) {
        ntx = p.tx;
        nty = p.ty;
      }
      if (nkx < 0 || nkx >= x || nky < 0 || nky >= y || mas[nky][nkx] == 1) {
        nkx = p.kx;
        nky = p.ky;
      }
      if (used[ntx][nty][nkx][nky] == false) {
        que.push(P(ntx, nty, nkx, nky, p.cou + 1));
        used[ntx][nty][nkx][nky] = true;
      }
    }
    que.pop();
  }
  return -1;
}

Error: MLE

Fixed submission:

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

int x, y, tx, ty, kx, ky, cou;
int mas[111][111];
int ans = 0;
bool used[111][111][111][111];
int dx[] = {-1, 0, 0, 1};
int dy[] = {0, -1, 1, 0};
struct P {
  int tx, ty, kx, ky, cou;
  P(int tx, int ty, int kx, int ky, int cou)
      : tx(tx), ty(ty), kx(kx), ky(ky), cou(cou) {}
};
int bfs();

int main() {
  while (1) {
    cin >> x >> y;
    if (x == 0 && y == 0)
      break;
    memset(used, false, sizeof(used));
    cin >> tx >> ty;
    cin >> kx >> ky;
    for (int i = 0; i < y; i++) {
      for (int j = 0; j < x; j++) {
        cin >> mas[i][j];
      }
    }
    ans = bfs();
    if (ans == -1)
      cout << "NA" << endl;
    else
      cout << ans << endl;
  }
}

int bfs() {
  queue<P> que;
  que.push(P(tx - 1, ty - 1, kx - 1, ky - 1, 0));
  used[tx - 1][ty - 1][kx - 1][ky - 1] = true;
  while (!que.empty()) {
    P p = que.front();
    if (p.tx == p.kx && p.ty == p.ky)
      return p.cou;
    else if (p.cou >= 100)
      return -1;
    for (int i = 0; i < 4; i++) {
      int ntx = p.tx + dx[i];
      int nty = p.ty + dy[i];
      int nkx = p.kx + dx[3 - i];
      int nky = p.ky + dy[3 - i];
      if (ntx < 0 || ntx >= x || nty < 0 || nty >= y || mas[nty][ntx] == 1) {
        ntx = p.tx;
        nty = p.ty;
      }
      if (nkx < 0 || nkx >= x || nky < 0 || nky >= y || mas[nky][nkx] == 1) {
        nkx = p.kx;
        nky = p.ky;
      }
      if (used[ntx][nty][nkx][nky] == false) {
        que.push(P(ntx, nty, nkx, nky, p.cou + 1));
        used[ntx][nty][nkx][nky] = true;
      }
    }
    que.pop();
  }
  return -1;
}