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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<bool>;
using vvi = vector<vi>;
using pii = pair<int, int>;
#define rep(i, n) for (int i = 0; i < n; i++)
#define range(i, a, n) for (int i = a; i < n; i++)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define INF 1e9
#define EPS 1e-9
#define MOD (1e9 + 7)
void put(string d) {}
template <class H, class... T> void put(string d, H &h, T &...t) {
  cout << h;
  if (sizeof...(t))
    cout << d;
  put(d, t...);
}
template <class T> void puti(T &a, string d = " ") {
  bool f = 1;
  for (auto &_ : a)
    cout << (exchange(f, 0) ? "" : d) << _;
  cout << endl;
}
template <class T> void putii(T &a, string d = " ") {
  for (auto &_ : a)
    puti(_, d);
}

int vx[] = {-1, 0, 1, 0};
int vy[] = {0, 1, 0, -1};

int main() {
  int w, h;
  while (cin >> w >> h, w) {
    int sx, sy, gx, gy;
    vvi a;
    a = vvi(h, vi(w, 0));
    rep(i, h) rep(j, w) {
      int t;
      cin >> t;
      if (t == 2)
        sy = i, sx = j;
      if (t == 3)
        gy = i, gx = j;
      if (t == 1)
        a[i][j] = 1;
    }
    using data = tuple<int, int, int, vvi>;
    queue<data> q;
    q.emplace(0, sy, sx, a);
    while (!q.empty()) {
      int t, y, x, nx, ny;
      vvi f;
      tie(t, y, x, f) = q.front();
      q.pop();
      // cout<<t<<","<<y<<","<<x<<endl;
      // putii(f);
      if (t == 10) {
        cout << -1 << endl;
        goto fin;
      }
      rep(i, 4) {
        nx = x + vx[i], ny = y + vy[i];
        if (!(ny >= h or ny < 0 or nx >= w or nx < 0) and f[ny][nx] == 1)
          continue;
        while (1) {
          if (gx == nx and gy == ny) {
            cout << t + 1 << endl;
            goto fin;
          }
          if (ny >= h or ny < 0 or nx >= w or nx < 0)
            break;
          if (f[ny][nx] == 1) {
            auto nf = f;
            nf[ny][nx] = 0, ny -= vy[i], nx -= vx[i];
            q.emplace(t + 1, ny, nx, nf);
            break;
          }
          nx += vx[i], ny += vy[i];
        }
      }
    }
    cout << -1 << endl;
  fin:;
  }
  return 0;
}


Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
#define rep(i, n) for (int i = 0; i < n; i++)
#define range(i, a, n) for (int i = a; i < n; i++)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define INF 1e9
#define EPS 1e-9
#define MOD (1e9 + 7)
void put(string d) {}
template <class H, class... T> void put(string d, H &h, T &...t) {
  cout << h;
  if (sizeof...(t))
    cout << d;
  put(d, t...);
}
template <class T> void puti(T &a, string d = " ") {
  bool f = 1;
  for (auto &_ : a)
    cout << (exchange(f, 0) ? "" : d) << _;
  cout << endl;
}
template <class T> void putii(T &a, string d = " ") {
  for (auto &_ : a)
    puti(_, d);
}

int vx[] = {-1, 0, 1, 0};
int vy[] = {0, 1, 0, -1};

int main() {
  int w, h;
  while (cin >> w >> h, w) {
    int sx, sy, gx, gy;
    vvi a;
    a = vvi(h, vi(w, 0));
    rep(i, h) rep(j, w) {
      int t;
      cin >> t;
      if (t == 2)
        sy = i, sx = j;
      if (t == 3)
        gy = i, gx = j;
      if (t == 1)
        a[i][j] = 1;
    }
    using data = tuple<int, int, int, vvi>;
    queue<data> q;
    q.emplace(0, sy, sx, a);
    while (!q.empty()) {
      int t, y, x, nx, ny;
      vvi f;
      tie(t, y, x, f) = q.front();
      q.pop();
      // cout<<t<<","<<y<<","<<x<<endl;
      // putii(f);
      if (t == 10) {
        cout << -1 << endl;
        goto fin;
      }
      rep(i, 4) {
        nx = x + vx[i], ny = y + vy[i];
        if (!(ny >= h or ny < 0 or nx >= w or nx < 0) and f[ny][nx] == 1)
          continue;
        while (1) {
          if (gx == nx and gy == ny) {
            cout << t + 1 << endl;
            goto fin;
          }
          if (ny >= h or ny < 0 or nx >= w or nx < 0)
            break;
          if (f[ny][nx] == 1) {
            auto nf = f;
            nf[ny][nx] = 0, ny -= vy[i], nx -= vx[i];
            q.emplace(t + 1, ny, nx, nf);
            break;
          }
          nx += vx[i], ny += vy[i];
        }
      }
    }
    cout << -1 << endl;
  fin:;
  }
  return 0;
}