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

#include <bits/stdc++.h>
using namespace std;

#define dump(n) cout << "# " << #n << '=' << (n) << endl
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define peri(i, a, b) for (int i = int(b); i-- > int(a);)
#define rep(i, n) repi(i, 0, n)
#define per(i, n) peri(i, 0, n)
#define all(c) begin(c), end(c)
#define mp make_pair
#define mt make_tuple

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;

const int INF = 1e9;
const int MOD = 1e9 + 7;
const double EPS = 1e-9;

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  return os << '(' << p.first << ',' << p.second << ')';
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &a) {
  os << '[';
  rep(i, a.size()) os << (i ? " " : "") << a[i];
  return os << ']';
}

template <typename Functor> struct functor_traits {
  template <typename C, typename Ret, typename Arg, typename... Args>
  static Arg helper(Ret (C::*)(Arg, Args...));
  template <typename C, typename Ret, typename Arg, typename... Args>
  static Arg helper(Ret (C::*)(Arg, Args...) const);
  // using first_argument_type=decltype(helper(&Functor::operator()));
  typedef decltype(helper(&Functor::operator())) first_argument_type;
};
template <typename Compare,
          typename T = typename functor_traits<Compare>::first_argument_type>
priority_queue<T, vector<T>, Compare> make_priority_queue(Compare comp) {
  return priority_queue<T, vector<T>, Compare>(move(comp));
}

int main() {
  const int di[] = {-1, 0, 1, 0}, dj[] = {0, -1, 0, 1};

  for (int h, w, n; cin >> h >> w >> n && h | w | n;) {
    string t;
    cin >> t;
    vs grid(h);
    rep(i, h) cin >> grid[i];

    vi ds(n + 1);
    rep(i, n) {
      if (t[i] == 'L')
        ds[i + 1] = (ds[i] + 1) % 4;
      if (t[i] == 'R')
        ds[i + 1] = (ds[i] + 3) % 4;
    }
    vvi next(n + 1, vi(4));
    {
      vi tmp(4, INF);
      per(i, n + 1) {
        tmp[ds[i]] = i;
        rep(j, 4) next[i][j] = tmp[j];
      }
    }

    // (i,j,d,p)
    auto pq = make_priority_queue(
        [](tuple<int, int, int, int> a, tuple<int, int, int, int> b) {
          return get<3>(a) > get<3>(b);
        });
    rep(i, h) rep(j, w) if (grid[i][j] == 'S') pq.emplace(i, j, 0, 0);
    vvi vis(h, vi(w));
    bool res = false;
    while (pq.size()) {
      int i, j, d, p;
      tie(i, j, d, p) = pq.top();
      pq.pop();
      if (i < 0 || h <= i || j < 0 || w <= j || p == INF || grid[i][j] == '#' ||
          (vis[i][j] >> d & 1))
        continue;
      vis[i][j] |= 1 << d;
      if (grid[i][j] == 'G') {
        res = true;
        break;
      }
      rep(k, 4) pq.emplace(i + di[k], j + dj[k], k, next[p][k]);
    }
    cout << (res ? "Yes" : "No") << endl;
  }
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define dump(n) cout << "# " << #n << '=' << (n) << endl
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define peri(i, a, b) for (int i = int(b); i-- > int(a);)
#define rep(i, n) repi(i, 0, n)
#define per(i, n) peri(i, 0, n)
#define all(c) begin(c), end(c)
#define mp make_pair
#define mt make_tuple

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;

const int INF = 1e9;
const int MOD = 1e9 + 7;
const double EPS = 1e-9;

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  return os << '(' << p.first << ',' << p.second << ')';
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &a) {
  os << '[';
  rep(i, a.size()) os << (i ? " " : "") << a[i];
  return os << ']';
}

template <typename Functor> struct functor_traits {
  template <typename C, typename Ret, typename Arg, typename... Args>
  static Arg helper(Ret (C::*)(Arg, Args...));
  template <typename C, typename Ret, typename Arg, typename... Args>
  static Arg helper(Ret (C::*)(Arg, Args...) const);
  // using first_argument_type=decltype(helper(&Functor::operator()));
  typedef decltype(helper(&Functor::operator())) first_argument_type;
};
template <typename Compare,
          typename T = typename functor_traits<Compare>::first_argument_type>
priority_queue<T, vector<T>, Compare> make_priority_queue(Compare comp) {
  return priority_queue<T, vector<T>, Compare>(move(comp));
}

int main() {
  typedef vector<vvi> vvvi;
  const int di[] = {-1, 0, 1, 0}, dj[] = {0, -1, 0, 1};

  for (int h, w, n; cin >> h >> w >> n && h | w | n;) {
    string t;
    cin >> t;
    vs grid(h);
    rep(i, h) cin >> grid[i];

    vi ds(n + 1);
    rep(i, n) {
      if (t[i] == 'L')
        ds[i + 1] = (ds[i] + 1) % 4;
      if (t[i] == 'R')
        ds[i + 1] = (ds[i] + 3) % 4;
    }
    vvi next(n + 1, vi(4));
    {
      vi tmp(4, INF);
      per(i, n + 1) {
        tmp[ds[i]] = i;
        rep(j, 4) next[i][j] = tmp[j];
      }
    }

    // (i,j,d,p)
    auto pq = make_priority_queue(
        [](tuple<int, int, int, int> a, tuple<int, int, int, int> b) {
          return get<3>(a) > get<3>(b);
        });
    rep(i, h) rep(j, w) if (grid[i][j] == 'S') pq.emplace(i, j, 0, 0);
    vvvi vis(h, vvi(w, vi(4)));
    bool res = false;
    while (pq.size()) {
      int i, j, d, p;
      tie(i, j, d, p) = pq.top();
      pq.pop();
      if (i < 0 || h <= i || j < 0 || w <= j || p == INF || grid[i][j] == '#' ||
          vis[i][j][d])
        continue;
      vis[i][j][d] = true;
      if (grid[i][j] == 'G') {
        res = true;
        break;
      }
      rep(k, 4) pq.emplace(i + di[k], j + dj[k], k, next[p][k]);
    }
    cout << (res ? "Yes" : "No") << endl;
  }
}