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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, b, n) for (int i = b; i < n; i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1 << 29;
const double EPS = 1e-9;

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

struct point {
  int ptx, pty, pkx, pky, cnt;
  point(int ptx, int pty, int pkx, int pky, int cnt)
      : ptx(ptx), pty(pty), pkx(pkx), pky(pky), cnt(cnt) {}
};

int X, Y;
int tx, ty;
int kx, ky;
int board[55][55];
bool memo[55][55][55][55];
int main() {
  while (cin >> X >> Y, X || Y) {
    queue<point> que;
    while (!que.empty()) {
      que.pop();
    }
    memset(memo, false, sizeof(memo));
    cin >> tx >> ty >> kx >> ky;
    for (int i = 1; i <= Y; i++) {
      for (int j = 1; j <= X; j++) {
        cin >> board[i][j];
      }
    }
    que.push(point(tx, ty, kx, ky, 0));
    int ans = 200;
    while (!que.empty()) {
      point p = que.front();
      que.pop();
      if (p.cnt >= 100) {
        break;
      }
      if (p.ptx == p.pkx && p.pty == p.pky) {
        ans = min(ans, p.cnt);
        break;
      }
      if (memo[p.ptx][p.pty][p.pkx][p.pky])
        continue;
      memo[p.ptx][p.pty][p.pkx][p.pky] = true;
      for (int i = 0; i < 4; i++) {
        int ntx = p.ptx + dx[i];
        int nty = p.pty + dy[i];
        int nkx = p.pkx + dx[(i + 2) % 4];
        int nky = p.pky + dy[(i + 2) % 4];
        if (ntx > X || ntx <= 0 || nty > Y || nty <= 0 ||
            board[nty][ntx] == 1) {
          ntx = p.ptx;
          nty = p.pty;
        }
        if (nkx > X || nkx <= 0 || nky > Y || nky <= 0 ||
            board[nky][nkx] == 1) {
          nkx = p.pkx;
          nky = p.pky;
        }
        que.push(point(ntx, nty, nkx, nky, p.cnt + 1));
      }
    }
    if (ans >= 100) {
      cout << "NA" << endl;
    } else {
      cout << ans << endl;
    }
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, b, n) for (int i = b; i < n; i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1 << 29;
const double EPS = 1e-9;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
int X, Y;
int tx, ty;
int kx, ky;
int board[55][55];
typedef pair<pii, pii> P;
int main() {
  while (cin >> X >> Y, X || Y) {
    queue<P> que;
    queue<int> kai;
    cin >> tx >> ty;
    cin >> kx >> ky;
    rep(i, Y + 1) {
      rep(j, X + 1) { board[i][j] = 0; }
    }

    for (int i = 1; i <= Y; i++) {
      for (int j = 1; j <= X; j++) {
        cin >> board[i][j];
      }
    }
    /*for(int i = 1;i <= Y;i++){
            for(int j  =1;j <= X;j++){
                    cout<<board[i][j];
            }
            cout <<endl;
    }*/
    que.push(P(pii(tx, ty), pii(kx, ky)));
    kai.push(0);
    int ans = 200;
    map<P, int> ma;
    ma.clear();
    while (!que.empty()) {
      int ptx = que.front().first.first;
      int pty = que.front().first.second;
      int pkx = que.front().second.first;
      int pky = que.front().second.second;
      int sum = kai.front();
      que.pop();
      kai.pop();
      // cout <<sum<<endl;
      if (sum >= 100) {
        continue;
      }
      if (ptx == pkx && pty == pky) {
        ans = min(ans, sum);
        continue;
      }
      ma[P(pii(ptx, pty), pii(pkx, pky))] = 1;
      for (int i = 0; i < 4; i++) {
        int ntx = ptx + dx[i];
        int nty = pty + dy[i];
        int nkx = pkx + dx[(i + 2) % 4];
        int nky = pky + dy[(i + 2) % 4];
        if (ma[P(pii(ntx, nty), pii(nkx, nky))] == 1) {
          continue;
        }
        if (ntx > X || ntx <= 0 || nty > Y || nty <= 0 ||
            board[nty][ntx] == 1) {
          nty = pty;
          ntx = ptx;

          // cout <<nty<<" "<<ntx<<endl;
        }
        if (nky > Y || nky <= 0 || nkx > X || nkx <= 0 ||
            board[nky][nkx] == 1) {
          nky = pky;
          nkx = pkx;
        }
        if (ma[P(pii(ntx, nty), pii(nkx, nky))] == 1) {
          continue;
        }
        ma[P(pii(ntx, nty), pii(nkx, nky))] = 1;
        que.push(P(pii(ntx, nty), pii(nkx, nky)));
        kai.push(sum + 1);
      }
    }
    if (ans == 200) {
      cout << "NA" << endl;
    } else {
      cout << ans << endl;
    }
  }
  return 0;
}