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

#include <algorithm>
#include <iostream>
#include <queue>
#define f first
#define s second
#define INF (1e9)
#define N 7
using namespace std;
typedef pair<int, int> P;
int bfs(int, int, int, int, int, int);
bool check(int, int, int, int, int, int);
int w, h, data[N][N], d[N][N], cnt[N][N];
int dy[4] = {-1, 0, 1, 0};
int dx[4] = {0, 1, 0, -1};
typedef struct data {
  int cy, cx, wy, wx;
} dat;

int main() {
  int sy, sx, gy, gx, wy, wx, ans;
  while (1) {
    cin >> w >> h;
    if (!w && !h)
      break;
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++) {
        cin >> data[i][j];
        if (data[i][j] == 2)
          sy = i, sx = j;
        else if (data[i][j] == 3)
          gy = i, gx = j;
        else if (data[i][j] == 4)
          wy = i, wx = j;
        d[i][j] = INF;
      }
    ans = bfs(sy, sx, wy, wx, gy, gx);
    cout << ans << endl;
  }
  return 0;
}

int bfs(int sy, int sx, int wy, int wx, int gy, int gx) {
  queue<dat> q;
  int flag;
  dat k = {sy, sx, wy, wx};
  q.push(k);
  d[sy][sx] = 0;
  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++)
      cnt[i][j] = 0;
  while (!q.empty()) {
    dat u = q.front();
    q.pop();
    flag = 0;
    for (int i = 0; i < 4; i++) {
      int ny = u.cy + dy[i], nx = u.cx + dx[i];
      if (ny < 0 || nx < 0 || h <= ny || w <= nx || data[ny][nx] == 1)
        continue;
      if (check(u.wy, u.wx, u.cy - dy[i], u.cx - dx[i], u.cy, u.cx)) {
        d[ny][nx] = d[u.cy][u.cx] + 1;
        cnt[nx][ny]++;
        if (cnt[nx][ny] == 100000) {
          flag = 1;
          break;
        }
        if (d[gy][gx] != INF) {
          flag = 2;
          break;
        }
        dat t = {ny, nx, u.cy - dy[i], u.cx - dx[i]};
        q.push(t);
      }
    }
    if (flag)
      break;
  }
  if (flag == 2)
    return d[gy][gx];
  if (flag)
    return -1;
  if (d[gy][gx] != INF)
    return d[gy][gx];
  return -1;
}

bool check(int sy, int sx, int gy, int gx, int xy, int xx) {
  if (gy < 0 || gx < 0 || h <= gy || w <= gx)
    return false;
  if (data[gy][gx] == 1)
    return false;
  bool c[N][N];
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      c[i][j] = false;
  queue<P> q;
  q.push(make_pair(sy, sx));
  c[sy][sx] = true;
  while (!q.empty()) {
    P u = q.front();
    q.pop();
    for (int i = 0; i < 4; i++) {
      int ny = u.f + dy[i], nx = u.s + dx[i];
      if (ny < 0 || nx < 0 || h <= ny || w <= nx || data[ny][nx] == 1 ||
          (ny == xy && nx == xx))
        continue;
      if (c[ny][nx] == true)
        continue;
      c[ny][nx] = true;
      q.push(make_pair(ny, nx));
    }
  }
  if (c[gy][gx] == true)
    return true;
  return false;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#define f first
#define s second
#define INF (1e9)
#define N 7
using namespace std;
typedef pair<int, int> P;
int bfs(int, int, int, int, int, int);
bool check(int, int, int, int, int, int);
int w, h, data[N][N], d[N][N];
int dy[4] = {-1, 0, 1, 0};
int dx[4] = {0, 1, 0, -1};
typedef struct data {
  int cy, cx, wy, wx;
} dat;

int main() {
  int sy, sx, gy, gx, wy, wx, ans;
  while (1) {
    cin >> w >> h;
    if (!w && !h)
      break;
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++) {
        cin >> data[i][j];
        if (data[i][j] == 2)
          sy = i, sx = j;
        else if (data[i][j] == 3)
          gy = i, gx = j;
        else if (data[i][j] == 4)
          wy = i, wx = j;
        d[i][j] = INF;
      }
    ans = bfs(sy, sx, wy, wx, gy, gx);
    cout << ans << endl;
  }
  return 0;
}

int bfs(int sy, int sx, int wy, int wx, int gy, int gx) {
  queue<dat> q;
  int flag;
  dat k = {sy, sx, wy, wx};
  q.push(k);
  d[sy][sx] = 0;
  while (!q.empty()) {
    dat u = q.front();
    q.pop();
    flag = 0;
    for (int i = 0; i < 4; i++) {
      int ny = u.cy + dy[i], nx = u.cx + dx[i];
      if (ny < 0 || nx < 0 || h <= ny || w <= nx || data[ny][nx] == 1)
        continue;
      if (check(u.wy, u.wx, u.cy - dy[i], u.cx - dx[i], u.cy, u.cx)) {
        d[ny][nx] = d[u.cy][u.cx] + 1;
        if (ny == gy && nx == gx) {
          flag = 1;
          break;
        }
        dat t = {ny, nx, u.cy - dy[i], u.cx - dx[i]};
        q.push(t);
      }
    }
    if (flag)
      break;
  }
  if (d[gy][gx] != INF)
    return d[gy][gx];
  return -1;
}

bool check(int sy, int sx, int gy, int gx, int xy, int xx) {
  if (gy < 0 || gx < 0 || h <= gy || w <= gx)
    return false;
  if (data[gy][gx] == 1)
    return false;
  bool c[N][N];
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      c[i][j] = false;
  queue<P> q;
  q.push(make_pair(sy, sx));
  c[sy][sx] = true;
  while (!q.empty()) {
    P u = q.front();
    q.pop();
    for (int i = 0; i < 4; i++) {
      int ny = u.f + dy[i], nx = u.s + dx[i];
      if (ny < 0 || nx < 0 || h <= ny || w <= nx || data[ny][nx] == 1 ||
          (ny == xy && nx == xx))
        continue;
      if (c[ny][nx] == true)
        continue;
      c[ny][nx] = true;
      q.push(make_pair(ny, nx));
    }
  }
  if (c[gy][gx] == true)
    return true;
  return false;
}