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

#include <iostream>
#include <queue>
using namespace std;
int w, h, mp[8][8];
int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};
struct po {
  int x, y, xx, yy;
};

po mk(int a, int b, int c, int d) {
  po res;
  res.x = a, res.y = b, res.xx = c, res.yy = d;
  return res;
}

int bfs(po start, po goal, int flg) {
  int dis[8][8] = {}, visited[8][8][8][8] = {};
  queue<po> Q;
  Q.push(mk(start.x, start.y, start.xx, start.yy));

  while (!Q.empty()) {
    po t = Q.front();
    Q.pop();
    visited[t.x][t.y][t.xx][t.yy] = 1;
    if (t.x == goal.x && t.y == goal.y)
      return dis[t.y][t.x];
    for (int i = 0; i < 4; i++) {
      int nx = t.x + dx[i], ny = t.y + dy[i];
      int mx = t.x - dx[i], my = t.y - dy[i];
      po nstart = mk(t.xx, t.yy, t.x, t.y);
      po ngoal = mk(mx, my, 0, 0);
      if (nx < 0 || ny < 0 || nx >= w || ny >= h || mp[ny][nx] == 1)
        continue;
      if ((flg == 0) &&
          (mx < 0 || my < 0 || mx >= w || my >= h || mp[my][mx] == 1 ||
           visited[nx][ny][t.x][t.y] == 1 || (bfs(nstart, ngoal, 1)) == -1))
        continue;
      if (flg == 1 && (dis[ny][nx] != 0 || (start.yy == ny && start.xx == nx)))
        continue;
      if (flg == 1)
        mx = my = 0;
      Q.push(mk(nx, ny, t.x, t.y));
      dis[ny][nx] = dis[t.y][t.x] + 1;
    }
  }
  return -1;
}

int main() {
  while (1) {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;
    po start, goal;
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++) {
        cin >> mp[i][j];
        if (mp[i][j] == 3)
          goal.x = j, goal.y = i, mp[i][j] = 0;
        if (mp[i][j] == 2)
          start.x = j, start.y = i, mp[i][j] = 0;
        if (mp[i][j] == 4)
          start.xx = j, start.yy = i, mp[i][j] = 0;
      }
    cout << bfs(start, goal, 0) << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <queue>
using namespace std;
int w, h, mp[8][8];
int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};
struct po {
  int x, y, xx, yy;
};

po mk(int a, int b, int c, int d) {
  po res;
  res.x = a, res.y = b, res.xx = c, res.yy = d;
  return res;
}

int bfs(po start, po goal, int flg) {
  int dis[8][8] = {}, visited[8][8][8][8] = {};
  queue<po> Q;
  Q.push(mk(start.x, start.y, start.xx, start.yy));

  while (!Q.empty()) {
    po t = Q.front();
    Q.pop();
    visited[t.x][t.y][t.xx][t.yy] = 1;
    if (t.x == goal.x && t.y == goal.y)
      return dis[t.y][t.x];
    for (int i = 0; i < 4; i++) {
      int nx = t.x + dx[i], ny = t.y + dy[i];
      int mx = t.x - dx[i], my = t.y - dy[i];
      po nstart = mk(t.xx, t.yy, t.x, t.y);
      po ngoal = mk(mx, my, 0, 0);
      if (nx < 0 || ny < 0 || nx >= w || ny >= h || mp[ny][nx] == 1)
        continue;
      if ((flg == 0) &&
          (mx < 0 || my < 0 || mx >= w || my >= h || mp[my][mx] == 1 ||
           visited[ny][nx][my][mx] == 1 || (bfs(nstart, ngoal, 1)) == -1))
        continue;
      if (flg == 1 && (dis[ny][nx] != 0 || (start.yy == ny && start.xx == nx)))
        continue;
      if (flg == 1)
        mx = my = 0;
      Q.push(mk(nx, ny, t.x, t.y));
      dis[ny][nx] = dis[t.y][t.x] + 1;
    }
  }
  return -1;
}

int main() {
  while (1) {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;
    po start, goal;
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++) {
        cin >> mp[i][j];
        if (mp[i][j] == 3)
          goal.x = j, goal.y = i, mp[i][j] = 0;
        if (mp[i][j] == 2)
          start.x = j, start.y = i, mp[i][j] = 0;
        if (mp[i][j] == 4)
          start.xx = j, start.yy = i, mp[i][j] = 0;
      }
    cout << bfs(start, goal, 0) << endl;
  }
  return 0;
}