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

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = n - 1; i >= 0; i--)

#define PB push_back
#define INF INT_MAX / 3
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define CLR(a) memset(a, 0, sizeof(a))

typedef long long int ll;

using namespace std;
int dx[] = {-1, 0, 0, 1};
int dy[] = {0, 1, -1, 0};
int xs, ys, tx, ty, kx, ky;

int main() {
  while (true) {
    cin >> xs >> ys;
    if (xs == 0 && ys == 0)
      break;
    cin >> tx >> ty;
    cin >> kx >> ky;
    tx--;
    ty--;
    kx--;
    ky--;
    if (tx == kx && ty == ky) {
      cout << 0 << endl;
      continue;
    }
    vector<vector<int>> map(ys, vector<int>(xs));
    REP(i, ys) REP(j, xs) cin >> map[i][j];

    queue<int> qtx;
    queue<int> qty;
    queue<int> qkx;
    queue<int> qky;
    queue<int> qn;
    set<pair<pair<int, int>, pair<int, int>>> used;
    int ans = 100;
    qtx.push(tx);
    qty.push(ty);
    qkx.push(kx);
    qky.push(ky);
    qn.push(1);
    while (!qtx.empty()) {
      int x1 = qtx.front();
      int y1 = qty.front();
      int x2 = qkx.front();
      int y2 = qky.front();
      int n = qn.front();
      if (n >= 100) {
        ans = min(ans, 100);
        break;
      }
      qtx.pop();
      qty.pop();
      qkx.pop();
      qky.pop();
      qn.pop();
      REP(i, 4) {
        int ntx = x1 + dx[i];
        int nty = y1 + dy[i];
        int nkx = x2 - dx[i];
        int nky = y2 - dy[i];
        if (ntx < 0 || xs <= ntx)
          ntx = x1;
        if (nty < 0 || ys <= nty)
          nty = y1;
        if (nkx < 0 || xs <= nkx)
          nkx = x2;
        if (nky < 0 || ys <= nky)
          nky = y2;
        if (map[nty][ntx] == 1) {
          ntx = x1;
          nty = y1;
        }
        if (map[nky][nkx] == 1) {
          nkx = x2;
          nky = y2;
        }
        pair<pair<int, int>, pair<int, int>> pp(pair<int, int>(ntx, nty),
                                                pair<int, int>(nkx, nky));
        if (used.find(pp) != used.end())
          continue;
        used.insert(pp);

        if (ntx == nkx && nty == nky) {
          ans = min(ans, n);
          break;
        }
        qtx.push(ntx);
        qty.push(nty);
        qkx.push(nkx);
        qky.push(nky);
        qn.push(n + 1);
      }
      if (ans < 100)
        break;
    }
    if (ans >= 100)
      cout << "NA" << endl;
    else
      cout << ans << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = n - 1; i >= 0; i--)

#define PB push_back
#define INF INT_MAX / 3
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define CLR(a) memset(a, 0, sizeof(a))

typedef long long int ll;

using namespace std;
int dx[] = {-1, 0, 0, 1};
int dy[] = {0, 1, -1, 0};
int xs, ys, tx, ty, kx, ky;

int main() {
  while (true) {
    cin >> xs >> ys;
    if (xs == 0 && ys == 0)
      break;
    cin >> tx >> ty;
    cin >> kx >> ky;
    tx--;
    ty--;
    kx--;
    ky--;
    if (tx == kx && ty == ky) {
      cout << 0 << endl;
      continue;
    }
    vector<vector<int>> map(ys, vector<int>(xs));
    REP(i, ys) REP(j, xs) cin >> map[i][j];

    queue<int> qtx;
    queue<int> qty;
    queue<int> qkx;
    queue<int> qky;
    queue<int> qn;
    set<pair<pair<int, int>, pair<int, int>>> used;
    int ans = 100;
    qtx.push(tx);
    qty.push(ty);
    qkx.push(kx);
    qky.push(ky);
    qn.push(1);
    while (!qtx.empty()) {
      int x1 = qtx.front();
      int y1 = qty.front();
      int x2 = qkx.front();
      int y2 = qky.front();
      int n = qn.front();
      if (n >= 100) {
        ans = min(ans, 100);
        break;
      }
      qtx.pop();
      qty.pop();
      qkx.pop();
      qky.pop();
      qn.pop();
      REP(i, 4) {
        int ntx = x1 + dx[i];
        int nty = y1 + dy[i];
        int nkx = x2 - dx[i];
        int nky = y2 - dy[i];
        if (ntx < 0 || xs <= ntx)
          ntx = x1;
        if (nty < 0 || ys <= nty)
          nty = y1;
        if (nkx < 0 || xs <= nkx)
          nkx = x2;
        if (nky < 0 || ys <= nky)
          nky = y2;
        if (map[nty][ntx] == 1) {
          ntx = x1;
          nty = y1;
        }
        if (map[nky][nkx] == 1) {
          nkx = x2;
          nky = y2;
        }
        pair<pair<int, int>, pair<int, int>> pp(pair<int, int>(ntx, nty),
                                                pair<int, int>(nkx, nky));
        if (used.find(pp) != used.end())
          continue;
        used.insert(pp);

        if (ntx == nkx && nty == nky) {
          ans = min(ans, n);
          break;
        }
        qtx.push(ntx);
        qty.push(nty);
        qkx.push(nkx);
        qky.push(nky);
        qn.push(n + 1);
      }
    }
    if (ans >= 100)
      cout << "NA" << endl;
    else
      cout << ans << endl;
  }
  return 0;
}