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

#include <bits/stdc++.h>
using namespace std;
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define mp(a, b) make_pair((a), (b))
#define pb(a) push_back((a))
#define all(x) (x).begin(), (x).end()
#define uniq(x) sort(all(x)), (x).erase(unique(all(x)), end(x))
#define fi first
#define se second
#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
void _dbg(string) { cout << endl; }
template <class H, class... T> void _dbg(string s, H h, T... t) {
  int l = s.find(',');
  cout << s.substr(0, l) << " = " << h << ", ";
  _dbg(s.substr(l + 1), t...);
}
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
  o << "(" << p.fi << "," << p.se << ")";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "[";
  for (T t : v) {
    o << t << ",";
  }
  o << "]";
  return o;
}

#define INF 1120000000

bool d[100 * 100 * 11 * 11 * 4];

int main() {
  int n, m, a;
  cin >> n >> m >> a;
  vector<string> vec(n);
  rep(i, n) cin >> vec[i];

  // pos[i][j], left a[x][y], direction d
  auto f = [&](int i, int j, int x, int y, int dir) {
    return (((i * m + j) * (a + 1) + x) * (a + 1) + y) * 4 + dir;
  };

  int sx, sy, tx, ty;
  rep(i, n) rep(j, m) {
    if (vec[i][j] == 'S') {
      if (i + 1 == n || vec[i + 1][j] == '#') {
        cout << -1 << endl;
        return 0;
      }
      sx = i + 1;
      sy = j;
    }
    if (vec[i][j] == 'G')
      tx = i, ty = j;
  }

  auto ok = [&](int i, int j, int dir) { // dbg(i,j,dir);
    if (i < 0 || i >= n || j < 0 || j >= m)
      return false;
    if (dir % 2 == 0 && vec[i][j] == 'S')
      return false;
    return (vec[i][j] == '.' || vec[i][j] == 'G' || vec[i][j] == 'S');
  };

  const int dx[] = {1, 0, -1, 0};
  const int dy[] = {0, 1, 0, -1};
  const int mirrx[] = {3, 2, 1, 0};
  const int mirry[] = {1, 0, 3, 2};

  int res = INF;

  fill(d, d + (sizeof(d)), false);
  queue<int> qx, qy, qi, qj, qd;
  d[f(sx, sy, 0, 0, 0)] = true;
  qx.push(sx);
  qy.push(sy);
  qi.push(0);
  qj.push(0);
  qd.push(0);

  while (!qx.empty()) {
    int x = qx.front();
    qx.pop();
    int y = qy.front();
    qy.pop();
    int i = qi.front();
    qi.pop();
    int j = qj.front();
    qj.pop();
    int dir = qd.front();
    qd.pop();

    if (x == tx && y == ty) {
      res = min(res, i + j);
      continue;
    }

    int nx, ny;

    // ????????????
    nx = x + dx[dir];
    ny = y + dy[dir];
    if (ok(nx, ny, dir)) {
      int z = f(nx, ny, i, j, dir);
      if (!d[z]) {
        d[z] = true;
        qx.push(nx);
        qy.push(ny);
        qi.push(i);
        qj.push(j);
        qd.push(dir);
      }
    }

    if (vec[x][y] == 'S')
      continue;

    // mirror
    nx = x + dx[mirrx[dir]];
    ny = y + dy[mirrx[dir]];
    if (i < a && ok(nx, ny, mirrx[dir])) {
      int z = f(nx, ny, i + 1, j, mirrx[dir]);
      if (!d[z]) {
        d[z] = true;
        qx.push(nx);
        qy.push(ny);
        qi.push(i + 1);
        qj.push(j);
        qd.push(mirrx[dir]);
      }
    }

    nx = x + dx[mirry[dir]];
    ny = y + dy[mirry[dir]];
    if (j < a && ok(nx, ny, mirry[dir])) {
      int z = f(nx, ny, i, j + 1, mirry[dir]);
      if (!d[z]) {
        d[z] = true;
        qx.push(nx);
        qy.push(ny);
        qi.push(i);
        qj.push(j + 1);
        qd.push(mirry[dir]);
      }
    }
  }

  if (res <= 2 * a)
    cout << res << endl;
  else
    cout << -1 << endl;

  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define mp(a, b) make_pair((a), (b))
#define pb(a) push_back((a))
#define all(x) (x).begin(), (x).end()
#define uniq(x) sort(all(x)), (x).erase(unique(all(x)), end(x))
#define fi first
#define se second
#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
void _dbg(string) { cout << endl; }
template <class H, class... T> void _dbg(string s, H h, T... t) {
  int l = s.find(',');
  cout << s.substr(0, l) << " = " << h << ", ";
  _dbg(s.substr(l + 1), t...);
}
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
  o << "(" << p.fi << "," << p.se << ")";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "[";
  for (T t : v) {
    o << t << ",";
  }
  o << "]";
  return o;
}

#define INF 1120000000

vector<int> v[100 * 100 * 11 * 11 * 4];
bool d[100 * 100 * 11 * 11 * 4];
string vec[101];

int main() {
  int n, m, a;
  cin >> n >> m >> a;
  rep(i, n) cin >> vec[i];

  // pos[i][j], left a[x][y], direction d
  auto f = [&](int i, int j, int x, int y, int dir) {
    if (i < 0 || i >= n || j < 0 || j >= m)
      return -1;
    return (((i * m + j) * (a + 1) + x) * (a + 1) + y) * 4 + dir;
  };

  int s, t;
  rep(i, n) rep(j, m) {
    if (vec[i][j] == 'S') {
      if (i + 1 == n || vec[i + 1][j] == '#') {
        cout << -1 << endl;
        return 0;
      }
      s = f(i + 1, j, 0, 0, 0);
    }
    if (vec[i][j] == 'G')
      t = f(i, j, 0, 0, 0);
  }

  auto ok = [&](int i, int j, bool bs = true) {
    if (i < 0 || i >= n || j < 0 || j >= m)
      return false;
    return (vec[i][j] == '.' || vec[i][j] == 'G' || (bs && vec[i][j] == 'S'));
  };

  rep(i, n) rep(j, m - 1) {
    if (ok(i, j) && ok(i, j + 1)) {
      rep(x, a + 1) rep(y, a + 1) {
        v[f(i, j, x, y, 1)].pb(f(i, j + 1, x, y, 1));
        v[f(i, j + 1, x, y, 3)].pb(f(i, j, x, y, 3));
      }
    }
  }

  rep(j, m) rep(i, n - 1) {
    if (ok(i, j, false) && ok(i + 1, j, false)) {
      rep(x, a + 1) rep(y, a + 1) {
        v[f(i, j, x, y, 0)].pb(f(i + 1, j, x, y, 0));
        v[f(i + 1, j, x, y, 2)].pb(f(i, j, x, y, 2));
      }
    }
  }

  rep(i, n) rep(j, m) if (vec[i][j] == '.') {
    rep(x, a + 1) rep(y, a + 1) {
      int p = f(i, j, x, y, 0);
      if (x < a) {
        if (ok(i, j - 1))
          v[p].pb(f(i, j - 1, x + 1, y, 3));
        if (ok(i - 1, j))
          v[p + 1].pb(f(i - 1, j, x + 1, y, 2));
        if (ok(i, j + 1))
          v[p + 2].pb(f(i, j + 1, x + 1, y, 1));
        if (ok(i + 1, j))
          v[p + 3].pb(f(i + 1, j, x + 1, y, 0));
      }
      if (y < a) {
        if (ok(i, j + 1))
          v[p].pb(f(i, j + 1, x, y + 1, 1));
        if (ok(i + 1, j))
          v[p + 1].pb(f(i + 1, j, x, y + 1, 0));
        if (ok(i, j - 1))
          v[p + 2].pb(f(i, j - 1, x, y + 1, 3));
        if (ok(i - 1, j))
          v[p + 3].pb(f(i - 1, j, x, y + 1, 2));
      }
    }
  }

  queue<int> q;
  fill(d, d + (sizeof(d)), false);
  d[s] = true;
  q.push(s);
  while (!q.empty()) {
    int x = q.front();
    q.pop(); // dbg(x/4/(a+1)/(a+1)/m, x/4/(a+1)/(a+1) % m, x%4);
    for (auto to : v[x]) {
      if (to == -1)
        continue;
      if (d[to])
        continue;
      q.push(to);
      d[to] = true;
    }
  }

  int res = INF;
  rep(i, a + 1) rep(j, a + 1) rep(k, 4) if (d[t + i * (a + 1) * 4 + j * 4 + k])
      res = min(res, i + j); // dbg(res);
  if (res <= 2 * a)
    cout << res << endl;
  else
    cout << -1 << endl;

  return 0;
}