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

#include <cstring>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;
class State {
public:
  int x1, y1, x2, y2, cost;

  State(int tx1, int ty1, int tx2, int ty2, int tcost) {
    x1 = tx1;
    y1 = ty1;
    x2 = tx2;
    y2 = ty2;
    cost = tcost;
  }
};

int w, h;
bool t[52][52];
bool used[52][52][52][52];

int dx1[] = {-1, 1, 0, 0};
int dx2[] = {1, -1, 0, 0};
int dy1[] = {0, 0, 1, -1};
int dy2[] = {0, 0, -1, 1};

void solve() {
  while (cin >> w >> h && (w || h)) {
    int x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    x1--;
    y1--;
    x2--;
    y2--;

    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> t[i][j];
      }
    }

    int ans = -1;
    queue<State> open;

    memset(used, 0, sizeof(used));
    used[x1][y1][x2][y2] = true;
    open.push(State(x1, y1, x2, y2, 0));

    while (!open.empty()) {
      State st = open.front();
      open.pop();
      if (st.cost >= 100)
        break;

      if (st.x1 == st.x2 && st.y1 == st.y2) {
        ans = st.cost;
        break;
      }

      for (int i = 0; i < 4; i++) {
        int nx1 = st.x1 + dx1[i];
        int ny1 = st.y1 + dy1[i];
        int nx2 = st.x2 + dx2[i];
        int ny2 = st.y2 + dy2[i];
        State nst = st;
        nst.cost++;

        if (nx1 >= 0 && nx1 < w && ny1 >= 0 && ny1 < h && !t[ny1][nx1]) {
          nst.x1 = nx1;
          nst.y1 = ny1;
        }
        if (nx2 >= 0 && nx2 < w && ny2 >= 0 && ny2 < h && !t[ny2][nx2]) {
          nst.x2 = nx2;
          nst.y2 = ny2;
        }

        if (!used[nst.x1][nst.y1][nst.x2][nst.y2]) {
          used[nst.x1][nst.y1][nst.x2][nst.y2] = true;
          open.push(nst);
        }
      }
    }

    if (ans == -1)
      cout << "NA\\n";
    else
      cout << ans << endl;
  }
}

int main() {
  solve();
  return (0);
}

Error: MLE

Fixed submission:

#include <cstring>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;
const int INF = 99999999;
typedef pair<int, int> P;
typedef pair<P, P> PP;
typedef pair<PP, int> PPP;
vector<vector<char>> Field;
bool used1[51][51];
bool used2[51][51];
int dx1[] = {-1, 0, 0, 1};
int dy1[] = {0, -1, 1, 0};
int dx2[] = {1, 0, 0, -1};
int dy2[] = {0, 1, -1, 0};

void solve() {
  int x, y;
  while (cin >> x >> y, x || y) {
    memset(used1, 0, sizeof(used1));
    memset(used2, 0, sizeof(used2));
    P t, k;
    cin >> t.first >> t.second >> k.first >> k.second;
    Field.resize(y + 2);
    for (int i = 0; i < y + 2; ++i) {
      Field[i].resize(x + 2);
      for (int j = 0; j < x + 2; ++j) {
        Field[i][j] = '1';
      }
    }
    for (int i = 1; i <= y; ++i) {
      for (int j = 1; j <= x; ++j) {
        cin >> Field[i][j];
      }
    }
    queue<PPP> Que;
    PPP ppp;
    ppp.first.first = t;
    ppp.first.second = k;
    ppp.second = 0;
    int count = 0;
    Que.push(ppp);
    while (!Que.empty()) {
      PPP front = Que.front();
      Que.pop();
      if (front.second >= 100) {
        break;
      }
      if (front.first.first.first == front.first.second.first &&
          front.first.first.second == front.first.second.second) {
        cout << front.second << endl;
        goto NEXT;
      }
      used1[front.first.first.second][front.first.first.first] = true;
      used2[front.first.second.second][front.first.second.first] = true;
      for (int i = 0; i < 4; ++i) {
        int nx1 = front.first.first.first + dx1[i];
        int ny1 = front.first.first.second + dy1[i];
        int nx2 = front.first.second.first + dx2[i];
        int ny2 = front.first.second.second + dy2[i];
        if (Field[ny1][nx1] == '0' && Field[ny2][nx2] == '0' &&
            !used1[ny1][nx1] && !used2[ny2][nx2]) {
          PPP next;
          next.first.first.first = nx1;
          next.first.first.second = ny1;
          next.first.second.first = nx2;
          next.first.second.second = ny2;
          next.second = front.second + 1;
          Que.push(next);
        } else if (Field[ny1][nx1] == '1' && Field[ny2][nx2] == '0' &&
                   !used2[ny2][nx2]) {
          PPP next;
          next.first.first.first = front.first.first.first;
          next.first.first.second = front.first.first.second;
          next.first.second.first = nx2;
          next.first.second.second = ny2;
          next.second = front.second + 1;
          Que.push(next);
        } else if (Field[ny1][nx1] == '0' && Field[ny2][nx2] == '1' &&
                   !used1[ny1][nx1]) {
          PPP next;
          next.first.first.first = nx1;
          next.first.first.second = ny1;
          next.first.second.first = front.first.second.first;
          next.first.second.second = front.first.second.second;
          next.second = front.second + 1;
          Que.push(next);
        }
      }
    }
    cout << "NA" << endl;
  NEXT:;
  }
}

int main() {
  solve();
  return (0);
}