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

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

#define REP(i, a, b) for (i = a; i < b; ++i)
#define rep(i, n) REP(i, 0, n)
const int inf = 1 << 29;

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int memo[50][50][50][50], mp[50][50];
int X, Y, tx, ty, kx, ky;

struct State {
  int tx, ty, kx, ky;
  State(int tx, int ty, int kx, int ky) : tx(tx), ty(ty), kx(kx), ky(ky) { ; }
  State() { ; }
};

inline bool range(int x, int m) { return (x >= 0 && x < m); }

bool ok(int tx, int ty) {
  if (range(tx, X) && range(ty, Y) && mp[ty][tx] == 0)
    return true;
  return false;
}

int main() {
  int i, j, k, l, ans;
  while (cin >> X >> Y, X | Y) {
    cin >> tx >> ty >> kx >> ky;
    tx--, ty--, kx--, ky--;
    rep(i, 50) rep(j, 50) rep(k, 50) rep(l, 50) memo[i][j][k][l] = inf;
    ans = inf;
    memo[tx][ty][kx][ky] = 0;

    rep(i, Y) {
      rep(j, X) {
        cin >> k;
        mp[i][j] = k;
      }
    }

    queue<State> Q;
    Q.push(State(tx, ty, kx, ky));
    while (!Q.empty()) {

      State now = Q.front();
      Q.pop();
      if (now.tx == now.kx && now.ty == now.ky) {
        ans = memo[now.tx][now.ty][now.kx][now.ky];
        break;
      }

      rep(i, 4) {
        int ntx = now.tx + dx[i], nty = now.ty + dy[i];
        int nkx = now.kx - dx[i], nky = now.ky - dy[i];
        if (ok(ntx, nty) && ok(nkx, nky) && memo[ntx][nty][nkx][nky] == inf) {
          memo[ntx][nty][nkx][nky] = memo[now.tx][now.ty][now.kx][now.ky] + 1;
          Q.push(State(ntx, nty, nkx, nky));
        } else if (ok(ntx, nty) && !ok(nkx, nky) &&
                   memo[ntx][nty][now.kx][now.ky] == inf) {
          memo[ntx][nty][now.kx][now.ky] =
              memo[now.tx][now.ty][now.kx][now.ky] + 1;
          Q.push(State(ntx, nty, now.kx, now.ky));
        } else if (ok(nkx, nky) && !ok(ntx, nty) &&
                   memo[now.tx][now.ty][nkx][nky] == inf) {
          memo[now.tx][now.ty][nkx][nky] =
              memo[now.tx][now.ty][now.kx][now.ky] + 1;
          Q.push(State(now.tx, now.ty, nkx, nky));
        }
      }
    }

    if (ans < inf)
      cout << ans << endl;
    else
      cout << "NA" << endl;
  }
}

Error: MLE

Fixed submission:

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

#define REP(i, a, b) for (i = a; i < b; ++i)
#define rep(i, n) REP(i, 0, n)
const int inf = 1 << 29;

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int memo[50][50][50][50], mp[50][50];
int X, Y, tx, ty, kx, ky;

struct State {
  int tx, ty, kx, ky;
  State(int tx, int ty, int kx, int ky) : tx(tx), ty(ty), kx(kx), ky(ky) { ; }
  State() { ; }
};

inline bool range(int x, int m) { return (x >= 0 && x < m); }

bool ok(int tx, int ty) {
  if (range(tx, X) && range(ty, Y) && mp[ty][tx] == 0)
    return true;
  return false;
}

int main() {
  int i, j, k, l, ans;
  while (cin >> X >> Y, X | Y) {
    cin >> tx >> ty >> kx >> ky;
    tx--, ty--, kx--, ky--;
    rep(i, 50) rep(j, 50) rep(k, 50) rep(l, 50) memo[i][j][k][l] = inf;
    ans = inf;
    memo[tx][ty][kx][ky] = 0;

    rep(i, Y) {
      rep(j, X) {
        cin >> k;
        mp[i][j] = k;
      }
    }

    queue<State> Q;
    Q.push(State(tx, ty, kx, ky));
    while (!Q.empty()) {

      State now = Q.front();
      Q.pop();
      if (now.tx == now.kx && now.ty == now.ky) {
        ans = memo[now.tx][now.ty][now.kx][now.ky];
        break;
      }

      rep(i, 4) {
        int ntx = now.tx + dx[i], nty = now.ty + dy[i];
        int nkx = now.kx - dx[i], nky = now.ky - dy[i];
        if (ok(ntx, nty) && ok(nkx, nky) && memo[ntx][nty][nkx][nky] == inf) {
          memo[ntx][nty][nkx][nky] = memo[now.tx][now.ty][now.kx][now.ky] + 1;
          Q.push(State(ntx, nty, nkx, nky));
        } else if (ok(ntx, nty) && !ok(nkx, nky)) {
          memo[ntx][nty][now.kx][now.ky] =
              memo[now.tx][now.ty][now.kx][now.ky] + 1;
          Q.push(State(ntx, nty, now.kx, now.ky));
        } else if (ok(nkx, nky) && !ok(ntx, nty)) {
          memo[now.tx][now.ty][nkx][nky] =
              memo[now.tx][now.ty][now.kx][now.ky] + 1;
          Q.push(State(now.tx, now.ty, nkx, nky));
        }
      }
    }

    if (ans < inf)
      cout << ans << endl;
    else
      cout << "NA" << endl;
  }
}