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

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < int(n); i++)
#define reps(i, n) for (int i = 1; i <= int(n); i++)
#define sint(i) scanf("%d", &i);
#define sintt(i, j) scanf("%d%d", &i, &j);
#define sinttt(i, j, k) scanf("%d%d%d", &i, &j, &k);
#define sintttt(i, j, k, m) scanf("%d%d%d%d", &i, &j, &k, &m);

#define INF 1010000000
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

class S {
public:
  int a, b, c, d;
  int n;
  S(int p, int n, int t, int k, int i) : a(p), b(n), c(t), d(k), n(i) {}
};

char visit[51][51][51][51];

int main() {
  while (1) {
    int n, m;
    sintt(n, m);
    if (n == 0)
      break;
    int a, b, c, d;
    sintttt(a, b, c, d);

    int masu[51][51];

    rep(i, 51) rep(j, 51) masu[j][i] = 0;
    reps(i, m) {
      reps(j, n) { sint(masu[j][i]); }
    }

    queue<S> que;
    que.push(S(a, b, c, d, 0));
    rep(i, 51) rep(j, 51) rep(k, 51) rep(p, 51) visit[i][j][k][p] = 0;
    int ans = 0;
    while (1) {
      if (que.empty() == 1) {
        break;
      }

      S u = que.front();
      que.pop();

      // printf("%d %d  %d %d  %d\\n",u.a,u.b,u.c,u.d,u.n);

      if (visit[u.a][u.b][u.c][u.d] == 1) {
        continue;
      }
      visit[u.a][u.b][u.c][u.d] = 1;

      if (u.a == u.c && u.b == u.d) {
        ans = u.n;
        break;
      }

      rep(i, 4) {
        int dx1 = u.a + dx[i];
        int dy1 = u.b + dy[i];

        int dx2 = u.c + dx[i] * -1;
        int dy2 = u.d + dy[i] * -1;

        // printf("push %d %d %d %d\\n",dx1,dy1,dx2,dy2);

        if (dx1 <= 0 || dy1 <= 0 || dx1 > n || dy1 > m || masu[dx1][dy1] == 1) {
          dx1 -= dx[i];
          dy1 -= dy[i];
        }
        if (dx2 <= 0 || dy2 <= 0 || dx2 > n || dy2 > m || masu[dx2][dy2] == 1) {
          dx2 -= dx[i] * -1;
          dy2 -= dy[i] * -1;
        }

        // printf("push %d %d %d %d\\n",dx1,dy1,dx2,dy2);
        que.push(S(dx1, dy1, dx2, dy2, u.n + 1));
      }
    }
    if (ans == 0) {
      puts("NA");
    } else {
      printf("%d\\n", ans);
    }
  }
}

Error: MLE

Fixed submission:

#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>

using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define reps(i, n) for (int i = 1; i <= n; i++)

class S {
public:
  int a, b, c, d;
  int val;
  S(int a, int b, int c, int d, int val) : a(a), b(b), c(c), d(d), val(val) {}
};

int ch[55][55][55][55];

int main() {
  while (1) {
    int n, m;
    cin >> n >> m;
    if (n == 0)
      break;

    int sa, sb, sc, sd;
    cin >> sa >> sb >> sc >> sd;

    int masu[55][55];
    rep(i, n) {
      rep(j, m) { cin >> masu[i][j]; }
    }

    rep(i, 55) rep(j, 55) rep(k, 55) rep(p, 55) ch[i][j][k][p] = 0;

    queue<S> que;
    que.push(S(sb, sa, sd, sc, 0));

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

    int ans = -1;
    while (1) {
      if (que.empty())
        break;

      S u = que.front();
      que.pop();

      if (u.a <= 0 || u.b <= 0 || u.c <= 0 || u.d <= 0)
        continue;
      if (u.a > m || u.c > m || u.b > n || u.d > n)
        continue;

      if (ch[u.a][u.b][u.c][u.d] == 1)
        continue;
      ch[u.a][u.b][u.c][u.d] = 1;

      if (u.a == u.c && u.b == u.d) {
        ans = u.val;
        break;
      }

      S e = u;
      e.val++;
      rep(i, 4) {
        e.a += dx[i];
        e.b += dy[i];
        rep(j, 4) {
          e.c += dx[j];
          e.d += dy[j];

          if (masu[e.a][e.b] == 1 || masu[e.c][e.d] == 1) {
          } else if (ch[u.a][u.b][u.c][u.d] == 1) {
          } else {
            que.push(e);
          }

          e.c -= dx[j];
          e.d -= dy[j];
        }
        e.a -= dx[i];
        e.b -= dy[i];
      }
    }
    printf("%d\\n", ans);
  }
}