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

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb emplace_back
#define INF (1e9 + 1)

int w, h;
int v[51][51];

struct Pos {
  int ay, ax, by, bx;

  bool operator<(Pos obj) const {
    int t = ay + ax * 60 + by * 60 * 60 + bx * 60 * 60 * 60;
    int p = obj.ay + obj.ax * 60 + obj.by * 60 * 60 + obj.bx * 60 * 60 * 60;
    return t < p;
  }
};

int main() {
  while (cin >> w >> h && (w || h)) {
    int ax, ay, bx, by;
    cin >> ax >> ay >> bx >> by;
    ax--, ay--, bx--, by--;

    rep(i, h) rep(j, w) cin >> v[i][j];

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

    map<Pos, int> mp;
    queue<Pos> que;

    mp[Pos{ay, ax, by, bx}] = 0;
    que.push(Pos{ay, ax, by, bx});

    bool f = false;
    while (que.size()) {
      Pos q = que.front();
      int iy = q.ay, ix = q.ax;
      int jy = q.by, jx = q.bx;
      if (iy == jy && ix == jx) {
        cout << mp[Pos{iy, ix, jy, jx}] << endl;
        f = true;
        break;
      }

      rep(k, 4) {
        int ddix = ix + dx[k];
        int ddjx = jx - dx[k];
        int ddiy = iy + dy[k];
        int ddjy = jy - dy[k];

        if (ddiy < 0 || ddiy >= h || ddix < 0 || ddix >= w || v[ddiy][ddix])
          ddiy = iy, ddix = ix;
        if (ddjy < 0 || ddjy >= h || ddjx < 0 || ddjx >= w || v[ddjy][ddjx])
          ddjy = jy, ddjx = jx;

        if (mp.count(Pos{ddiy, ddix, ddjy, ddjx}) == 0 ||
            mp[Pos{ddiy, ddix, ddjy, ddjx}] > mp[Pos{iy, ix, jy, jx}] + 1) {
          que.push(Pos{ddiy, ddix, ddjy, ddjx});
          mp[Pos{ddiy, ddix, ddjy, ddjx}] = mp[Pos{iy, ix, jy, jx}] + 1;
        }
      }
      que.pop();
    }

    if (!f)
      cout << "NA" << endl;
  }
}

Error: MLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb emplace_back
#define INF (1e9 + 1)

int w, h;
int v[51][51];

struct Pos {
  int ay, ax, by, bx;

  bool operator<(Pos obj) const {
    int t = ay + ax * 60 + by * 60 * 60 + bx * 60 * 60 * 60;
    int p = obj.ay + obj.ax * 60 + obj.by * 60 * 60 + obj.bx * 60 * 60 * 60;
    return t < p;
  }
};

int main() {
  while (cin >> w >> h && (w || h)) {
    int ax, ay, bx, by;
    cin >> ax >> ay >> bx >> by;
    ax--, ay--, bx--, by--;

    rep(i, h) rep(j, w) cin >> v[i][j];

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

    map<Pos, int> mp;
    queue<Pos> que;

    mp[Pos{ay, ax, by, bx}] = 0;
    que.push(Pos{ay, ax, by, bx});

    while (que.size()) {
      Pos q = que.front();
      int iy = q.ay, ix = q.ax;
      int jy = q.by, jx = q.bx;

      rep(k, 4) {
        int ddix = ix + dx[k];
        int ddjx = jx - dx[k];
        int ddiy = iy + dy[k];
        int ddjy = jy - dy[k];

        if (ddiy < 0 || ddiy >= h || ddix < 0 || ddix >= w || v[ddiy][ddix])
          ddiy = iy, ddix = ix;
        if (ddjy < 0 || ddjy >= h || ddjx < 0 || ddjx >= w || v[ddjy][ddjx])
          ddjy = jy, ddjx = jx;

        if (mp.count(Pos{ddiy, ddix, ddjy, ddjx}) == 0 ||
            mp[Pos{ddiy, ddix, ddjy, ddjx}] > mp[Pos{iy, ix, jy, jx}] + 1) {
          que.push(Pos{ddiy, ddix, ddjy, ddjx});
          mp[Pos{ddiy, ddix, ddjy, ddjx}] = mp[Pos{iy, ix, jy, jx}] + 1;
        }
      }
      que.pop();
    }

    int ans = INF;
    rep(i, h) {
      rep(j, w) {
        if (mp.count(Pos{i, j, i, j}) == 0)
          continue;
        ans = min(ans, mp[Pos{i, j, i, j}]);
      }
    }
    if (ans >= 100)
      cout << "NA" << endl;
    else
      cout << ans << endl;
  }
}