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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define INF (2000000000)
#define MOD (1000000007)
using namespace std;

typedef vector<int> VI;
typedef vector<int>::iterator VII;
typedef vector<double> VD;
typedef vector<double>::iterator VDI;
typedef pair<int, int> P;

typedef struct {
  int tx, ty, kx, ky;
} POS;

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

int maze[100][100];
int X, Y;
POS f;
int d[52][52][52][52];

int search() {
  queue<POS> que;

  for (int i = 0; i < 52; i++) {
    for (int j = 0; j < 52; j++) {
      for (int k = 0; k < 52; k++) {
        for (int l = 0; l < 52; l++) {
          d[i][j][k][l] = INF;
        }
      }
    }
  }

  que.push(f);
  d[f.tx][f.ty][f.kx][f.ky] = 0;

  while (que.size()) {
    POS now = que.front();
    que.pop();

    // printf("%d %d %d %d\\n", now.tx, now.ty, now.kx, now.ky);

    if (now.tx == now.kx && now.ty == now.ky) {
      return (d[now.tx][now.ty][now.kx][now.ky]);
    }

    for (int i = 0; i < 4; i++) {
      POS to;
      // if (maze[now.tx + dx[i]][now.ty + dy[i]] && maze[now.kx - dx[i]][now.ky
      // - dy[i]]){ 	continue;
      // }

      if (maze[now.tx + dx[i]][now.ty + dy[i]]) {
        to.tx = now.tx;
        to.ty = now.ty;
      } else {
        to.tx = now.tx + dx[i];
        to.ty = now.ty + dy[i];
      }

      if (maze[now.kx - dx[i]][now.ky - dy[i]]) {
        to.kx = now.kx;
        to.ky = now.ky;
      } else {
        to.kx = now.kx - dx[i];
        to.ky = now.ky - dy[i];
      }

      // to.tx = now.tx + dx[i];
      // to.ty = now.ty + dy[i];
      // to.kx = now.kx - dx[i];
      // to.ky = now.ky - dy[i];

      // printf("%d %d %d %d\\n", to.tx, to.ty, to.kx, to.ky);

      if (d[to.tx][to.ty][to.kx][to.ky] == INF &&
          d[now.tx][now.ty][now.kx][now.ky] < 99) {
        que.push(to);
        d[to.tx][to.ty][to.kx][to.ky] = d[now.tx][now.ty][now.kx][now.ky] + 1;
      }
    }
  }

  return (-1);
}

int main() {
  while (scanf("%d %d", &X, &Y), X + Y) {
    scanf("%d %d", &f.tx, &f.ty);
    scanf("%d %d", &f.kx, &f.ky);

    for (int i = 0; i < 100; i++) {
      for (int j = 0; j < 100; j++) {
        maze[i][j] = 1;
      }
    }

    for (int i = 1; i <= Y; i++) {
      for (int j = 1; j <= X; j++) {
        scanf("%d", &maze[j][i]);
      }
    }

    // for (int i = 0; i <= Y + 1; i++){
    //	for (int j = 0; j <= X + 1; j++){
    //		printf("%d ", maze[j][i]);
    //	}
    //	puts("");
    // }

    int r = search();
    if (r != -1)
      printf("%d\\n", r);
    else
      puts("NA");
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define INF (2000000000)
#define MOD (1000000007)
using namespace std;

typedef vector<int> VI;
typedef vector<int>::iterator VII;
typedef vector<double> VD;
typedef vector<double>::iterator VDI;
typedef pair<int, int> P;

typedef struct {
  int tx, ty, kx, ky;
} POS;

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

int maze[100][100];
int X, Y;
POS f;
int d[70][70][70][70];

int search() {
  queue<POS> que;

  for (int i = 0; i < 70; i++) {
    for (int j = 0; j < 70; j++) {
      for (int k = 0; k < 70; k++) {
        for (int l = 0; l < 70; l++) {
          d[i][j][k][l] = INF;
        }
      }
    }
  }

  que.push(f);
  d[f.tx][f.ty][f.kx][f.ky] = 0;

  while (que.size()) {
    POS now = que.front();
    que.pop();

    // printf("%d %d %d %d\\n", now.tx, now.ty, now.kx, now.ky);

    if (now.tx == now.kx && now.ty == now.ky) {
      return (d[now.tx][now.ty][now.kx][now.ky]);
    }

    for (int i = 0; i < 4; i++) {
      POS to;
      // if (maze[now.tx + dx[i]][now.ty + dy[i]] && maze[now.kx - dx[i]][now.ky
      // - dy[i]]){ 	continue;
      // }

      if (maze[now.tx + dx[i]][now.ty + dy[i]]) {
        to.tx = now.tx;
        to.ty = now.ty;
      } else {
        to.tx = now.tx + dx[i];
        to.ty = now.ty + dy[i];
      }

      if (maze[now.kx - dx[i]][now.ky - dy[i]]) {
        to.kx = now.kx;
        to.ky = now.ky;
      } else {
        to.kx = now.kx - dx[i];
        to.ky = now.ky - dy[i];
      }

      // to.tx = now.tx + dx[i];
      // to.ty = now.ty + dy[i];
      // to.kx = now.kx - dx[i];
      // to.ky = now.ky - dy[i];

      // printf("%d %d %d %d\\n", to.tx, to.ty, to.kx, to.ky);

      if (d[to.tx][to.ty][to.kx][to.ky] == INF &&
          d[now.tx][now.ty][now.kx][now.ky] < 99) {
        que.push(to);
        d[to.tx][to.ty][to.kx][to.ky] = d[now.tx][now.ty][now.kx][now.ky] + 1;
      }
    }
  }

  return (-1);
}

int main() {
  while (scanf("%d %d", &X, &Y), X + Y) {
    scanf("%d %d", &f.tx, &f.ty);
    scanf("%d %d", &f.kx, &f.ky);

    for (int i = 0; i < 100; i++) {
      for (int j = 0; j < 100; j++) {
        maze[i][j] = 1;
      }
    }

    for (int i = 1; i <= Y; i++) {
      for (int j = 1; j <= X; j++) {
        scanf("%d", &maze[j][i]);
      }
    }

    // for (int i = 0; i <= Y + 1; i++){
    //	for (int j = 0; j <= X + 1; j++){
    //		printf("%d ", maze[j][i]);
    //	}
    //	puts("");
    // }

    int r = search();
    if (r != -1)
      printf("%d\\n", r);
    else
      puts("NA");
  }

  return 0;
}