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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
typedef struct P {
  int tx, ty, kx, ky, c;
};
int dx[4] = {0, 1, 0, -1};
int dy[4] = {-1, 0, 1, 0};
int d[55][55];
bool used[55][55][55][55];
int X, Y;
P fa;
int main(void) {
  int ans;
  while (1) {

    scanf("%d %d", &X, &Y);
    if (!X && !Y)
      break;
    scanf("%d %d %d %d", &fa.tx, &fa.ty, &fa.kx, &fa.ky);
    fa.c = 0;
    for (int i = 1; i <= Y; i++) {
      for (int j = 1; j <= X; j++) {
        scanf("%d", &d[j][i]);
      }
    }

    ans = -1;
    memset(used, 0, sizeof(used));
    queue<P> que;
    que.push(fa);

    while (!que.empty()) {
      P p = que.front();
      que.pop();
      used[p.tx][p.ty][p.kx][p.ky] = true;
      if (p.c > 100)
        break;
      if (p.tx == p.kx && p.ty == p.ky) {
        ans = p.c;
        break;
      }
      if ((abs(p.tx - p.kx) + abs(p.ty - p.ky) + 1) / 2 > 100 - p.c)
        break;
      for (int i = 0; i < 4; i++) {
        int f = 0;
        int x[2] = {p.tx, p.kx};
        int y[2] = {p.ty, p.ky};
        for (int j = 0; j < 2; j++) {
          int t = 1;
          if (j)
            t = -1;
          if (0 < x[j] + dx[i] * t && x[j] + dx[i] * t <= X &&
              0 < y[j] + dy[i] * t && y[j] + dy[i] * t <= Y) {
            if (!d[x[j] + dx[i] * t][y[j] + dy[i] * t]) {
              x[j] += dx[i] * t;
              y[j] += dy[i] * t;
              f++;
            }
          }
        }
        if (!f) {
          continue;
        }
        if (used[x[0]][y[0]][x[1]][y[1]]) {
          continue;
        }
        P pu = {x[0], y[0], x[1], y[1], p.c + 1};
        used[x[0]][y[0]][x[1]][y[1]] = true;
        que.push(pu);
      }
    }

    if (ans == -1)
      puts("NA");
    else
      printf("%d\\n", ans);
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
typedef struct P {
  int tx, ty, kx, ky, c;
};
int dx[4] = {0, 1, 0, -1};
int dy[4] = {-1, 0, 1, 0};
int d[55][55];
int used[55][55][55][55];
int X, Y;
P fa;
int main(void) {
  int ans;
  while (1) {

    scanf("%d %d", &X, &Y);
    if (!X && !Y)
      break;
    scanf("%d %d %d %d", &fa.tx, &fa.ty, &fa.kx, &fa.ky);
    fa.c = 0;
    for (int i = 1; i <= Y; i++) {
      for (int j = 1; j <= X; j++) {
        scanf("%d", &d[j][i]);
      }
    }

    ans = -1;
    memset(used, 0, sizeof(used));
    queue<P> que;
    que.push(fa);

    while (!que.empty()) {
      P p = que.front();
      que.pop();
      used[p.tx][p.ty][p.kx][p.ky] = 1;
      if (p.c > 100)
        break;
      if (p.tx == p.kx && p.ty == p.ky) {
        ans = p.c;
        break;
      }
      if ((abs(p.tx - p.kx) + abs(p.ty - p.ky) + 1) / 2 > 100 - p.c)
        break;
      for (int i = 0; i < 4; i++) {
        int f = 0;
        int x[2] = {p.tx, p.kx};
        int y[2] = {p.ty, p.ky};
        for (int j = 0; j < 2; j++) {
          int t = 1;
          if (j)
            t = -1;
          if (0 < x[j] + dx[i] * t && x[j] + dx[i] * t <= X &&
              0 < y[j] + dy[i] * t && y[j] + dy[i] * t <= Y) {
            if (!d[x[j] + dx[i] * t][y[j] + dy[i] * t]) {
              x[j] += dx[i] * t;
              y[j] += dy[i] * t;
              f += 1;
            }
          }
        }
        if (!f)
          continue;
        if (used[x[0]][y[0]][x[1]][y[1]]) {
          continue;
        }
        P pu = {x[0], y[0], x[1], y[1], p.c + 1};
        que.push(pu);
      }
    }

    if (ans == -1)
      puts("NA");
    else
      printf("%d\\n", ans);
  }
}