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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define r(i, n) for (int i = 0; i < n; i++)
using namespace std;
#define mk make_pair
#define f first
#define s second
typedef pair<int, int> P;
int a[51][51], ax, bx, ay, by;
int dx1[] = {1, 0, -1, 0};
int dy1[] = {0, 1, 0, -1};
int dx2[] = {-1, 0, 1, 0};
int dy2[] = {0, -1, 0, 1};
bool used[10000000];
int gethash(P q, P w) {
  return q.f + q.s * 52 + w.f * 52 * 52 + w.s * 52 * 52 * 52;
}
int main() {
  int w, h;
  while (cin >> w >> h, w) {
    memset(used, 0, sizeof(used));
    cin >> ax >> ay >> bx >> by;
    ax--, ay--, bx--, by--;
    r(i, h) r(j, w) cin >> a[i][j];
    used[gethash(P(ay, ax), P(by, bx))] = 1;
    queue<pair<int, pair<pair<int, int>, pair<int, int>>>> q;
    q.push(mk(0, mk(mk(ay, ax), mk(by, bx))));
    while (!q.empty()) {
      pair<int, pair<pair<int, int>, pair<int, int>>> p = q.front();
      q.pop();
      P p1 = p.s.f, p2 = p.s.s;
      if (p1 == p2) {
        cout << p.f << endl;
        goto L;
      }
      r(i, 4) {
        int x1 = p1.s + dx1[i], x2 = p2.s + dx2[i], y1 = p1.f + dy1[i],
            y2 = p2.f + dy2[i];
        if (x1 < 0 || x1 >= w)
          x1 = p1.s;
        if (x2 < 0 || x2 >= w)
          x2 = p2.s;
        if (y1 < 0 || y1 >= h)
          y1 = p1.f;
        if (y2 < 0 || y2 >= h)
          y2 = p2.f;
        if (a[y1][x1])
          x1 = p1.s, y1 = p1.f;
        if (a[y2][x2])
          x2 = p2.s, y2 = p2.f;
        if (!used[gethash(P(y1, x1), P(y2, x2))]) {
          used[gethash(P(y1, x1), P(y2, x2))] = 1;
          q.push(mk(p.f + 1, mk(mk(y1, x1), mk(y2, x2))));
        }
      }
    }
    cout << "NA" << endl;
  L:;
  }
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
using namespace std;
#define mk make_pair
#define f first
#define s second
typedef pair<int, int> P;
int a[51][51], ax, bx, ay, by;
int dx1[] = {1, 0, -1, 0};
int dy1[] = {0, 1, 0, -1};
int dx2[] = {-1, 0, 1, 0};
int dy2[] = {0, -1, 0, 1};
bool used[100000000];
int gethash(P q, P w) {
  return q.f + q.s * 52 + w.f * 52 * 52 + w.s * 52 * 52 * 52;
}
int main() {
  int w, h;
  while (cin >> w >> h, w) {
    memset(used, 0, sizeof(used));
    cin >> ax >> ay >> bx >> by;
    ax--, ay--, bx--, by--;
    r(i, h) r(j, w) cin >> a[i][j];
    used[gethash(P(ay, ax), P(by, bx))] = 1;
    queue<pair<int, pair<pair<int, int>, pair<int, int>>>> q;
    q.push(mk(0, mk(mk(ay, ax), mk(by, bx))));
    while (!q.empty()) {
      pair<int, pair<pair<int, int>, pair<int, int>>> p = q.front();
      q.pop();
      P p1 = p.s.f, p2 = p.s.s;
      if (p1 == p2) {
        cout << p.f << endl;
        goto L;
      }
      r(i, 4) {
        int x1 = p1.s + dx1[i], x2 = p2.s + dx2[i], y1 = p1.f + dy1[i],
            y2 = p2.f + dy2[i];
        if (x1 < 0 || x1 >= w)
          x1 = p1.s;
        if (x2 < 0 || x2 >= w)
          x2 = p2.s;
        if (y1 < 0 || y1 >= h)
          y1 = p1.f;
        if (y2 < 0 || y2 >= h)
          y2 = p2.f;
        if (a[y1][x1])
          x1 = p1.s, y1 = p1.f;
        if (a[y2][x2])
          x2 = p2.s, y2 = p2.f;
        if (!used[gethash(P(y1, x1), P(y2, x2))]) {
          used[gethash(P(y1, x1), P(y2, x2))] = 1;
          q.push(mk(p.f + 1, mk(mk(y1, x1), mk(y2, x2))));
        }
      }
    }
    cout << "NA" << endl;
  L:;
  }
}