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

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
#define fi first
#define se second

template <typename A, typename B> inline bool chmax(A &a, B b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename A, typename B> inline bool chmin(A &a, B b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, pii> pip;
typedef pair<pii, pii> ppp;
typedef pair<int, ppp> P;
const ll INF = 1ll << 29;
const ll MOD = 1000000007;
const double EPS = 1e-9;

int W, H;
pii tp, kp;
int maps[50][50];
bool done[50][50][50][50];

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

bool ok(pii p) {
  if (!(p.fi >= 0 && p.fi < H && p.se >= 0 && p.se < W))
    return false;
  if (maps[p.fi][p.se] == 1)
    return false;
  return true;
}

int main() {
  while (cin >> W >> H, W || H) {
    cin >> tp.se >> tp.fi >> kp.se >> kp.fi;
    tp.se--;
    tp.fi--;
    kp.se--;
    kp.fi--;
    REP(i, H) REP(j, W) scanf("%d", &maps[i][j]);

    memset(done, 0, sizeof(done));
    queue<P> que;
    que.push(P(0, ppp(tp, kp)));
    done[tp.fi][tp.se][kp.fi][kp.se] = true;

    int ans = INF;

    while (!que.empty()) {
      int c = que.front().fi;
      pii p1 = que.front().se.fi, p2 = que.front().se.se;
      que.pop();

      if (p1 == p2) {
        ans = c;
        break;
      }
      if (c >= 100)
        break;

      REP(i, 4) {
        pii np1(p1.fi + dy[i], p1.se + dx[i]);
        pii np2(p2.fi + dy[(i + 2) % 4], p2.se + dx[(i + 2) % 4]);

        bool o1 = ok(np1), o2 = ok(np2);
        if (!o1 && !o2)
          continue;

        np1 = o1 ? np1 : p1;
        np2 = o2 ? np2 : p2;
        if (!chmax(done[np1.fi][np1.se][np2.fi][np2.se], 1))
          continue;
        que.push(P(c + 1, ppp(np1, np2)));
      }
    }

    if (ans == INF)
      puts("NA");
    else
      cout << ans << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
#define fi first
#define se second

template <typename A, typename B> inline bool chmax(A &a, B b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename A, typename B> inline bool chmin(A &a, B b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, pii> pip;
typedef pair<pii, pii> ppp;
typedef pair<int, ppp> P;
const ll INF = 1ll << 29;
const ll MOD = 1000000007;
const double EPS = 1e-9;

int W, H;
pii tp, kp;
int maps[50][50];
bool done[50][50][50][50];

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

bool ok(pii p) {
  if (!(p.fi >= 0 && p.fi < H && p.se >= 0 && p.se < W))
    return false;
  if (maps[p.fi][p.se] == 1)
    return false;
  return true;
}

int main() {
  while (cin >> W >> H, W || H) {
    cin >> tp.se >> tp.fi >> kp.se >> kp.fi;
    tp.se--;
    tp.fi--;
    kp.se--;
    kp.fi--;
    REP(i, H) REP(j, W) scanf("%d", &maps[i][j]);

    memset(done, 0, sizeof(done));
    queue<P> que;
    que.push(P(0, ppp(tp, kp)));
    done[tp.fi][tp.se][kp.fi][kp.se] = true;

    int ans = INF;

    while (!que.empty()) {
      int c = que.front().fi;
      pii p1 = que.front().se.fi, p2 = que.front().se.se;
      que.pop();

      if (p1 == p2) {
        ans = c;
        break;
      }
      if (c >= 100)
        break;

      REP(i, 4) {
        pii np1(p1.fi + dy[i], p1.se + dx[i]);
        pii np2(p2.fi + dy[(i + 2) % 4], p2.se + dx[(i + 2) % 4]);

        bool o1 = ok(np1), o2 = ok(np2);
        if (!o1 && !o2)
          continue;

        np1 = o1 ? np1 : p1;
        np2 = o2 ? np2 : p2;
        if (done[np1.fi][np1.se][np2.fi][np2.se])
          continue;
        que.push(P(c + 1, ppp(np1, np2)));
        done[p1.fi][p1.se][p2.fi][p2.se] = true;
      }
    }

    if (ans == INF)
      puts("NA");
    else
      cout << ans << endl;
  }
  return 0;
}