Problem p01442 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
ld eps = 1e-9;

struct aa {
  int x;
  int y;
  int n;
  int w;
};
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, 1, 0, -1};
class Compare {
public:
  // aa?????????????????¶
  bool operator()(const aa &l, const aa &r) { return l.n > r.n; }
};

vector<vector<int>> wheres(4);
vector<vector<int>> anext;
int main() {
  while (1) {
    int H, W, N;
    cin >> H >> W >> N;
    anext.clear();
    wheres.resize(4);
    if (!H)
      break;
    string st;
    cin >> st;
    int way = 3;
    vector<int> ways;
    ways.push_back(way);
    wheres[way].push_back(0);
    for (int i = 0; i < N; ++i) {
      if (st[i] == 'L') {
        way = (way + 1) % 4;
      } else {
        way = (way + 3) % 4;
      }
      ways.push_back(way);
      wheres[way].push_back(i + 1);
    }
    for (int i = 0; i < N; ++i) {
      vector<int> v;
      for (int j = 0; j < 4; ++j) {
        auto it = upper_bound(wheres[j].begin(), wheres[j].end(), i);
        if (it == wheres[j].end())
          v.push_back(1e8);
        else
          v.push_back(*it);
      }
      for (int j = 0; j < ways[i]; ++j) {
        rotate(v.begin(), v.end(), v.end());
      }
      anext.push_back(v);
    }
    wheres.clear();
    vector<vector<int>> field(H + 2, vector<int>(W + 2, 0));
    for (int x = 0; x <= W + 1; ++x) {
      int y = 0;
      field[y][x] = 1;
      y = H + 1;
      field[y][x] = 1;
    }
    for (int y = 0; y <= H + 1; ++y) {
      int x = 0;
      field[y][x] = 1;
      x = W + 1;
      field[y][x] = 1;
    }
    vector<vector<vector<int>>> memo(
        H + 1, vector<vector<int>>(W + 1, vector<int>(4, 1e8)));
    int gx, gy;
    int sx, sy;
    for (int i = 0; i < H; ++i) {
      string ast;
      cin >> ast;
      for (int j = 0; j < W; ++j) {
        if (ast[j] == 'G') {
          gx = j + 1;
          gy = i + 1;
        } else if (ast[j] == 'S') {
          sx = j + 1;
          sy = i + 1;
        } else if (ast[j] == '#') {
          field[i + 1][j + 1] = true;
        }
      }
    }
    priority_queue<aa, vector<aa>, Compare> que;
    que.push(aa{sx, sy, 0, 3});
    memo[sy][sx][3] = 0;
    string ans = "No";
    while (!que.empty()) {
      aa atop(que.top());
      que.pop();
      const int px = atop.x;
      const int py = atop.y;
      const int pn = atop.n;
      const int pw = atop.w;
      if (px == gx && py == gy) {
        ans = "Yes";
        break;
      }
      {
        const int nx = px + dx[pw];
        const int ny = py + dy[pw];
        if (!field[ny][nx]) {
          if (memo[ny][nx][pw] > pn) {
            memo[ny][nx][pw] = pn;
            que.push(aa{nx, ny, pn, pw});
          }
        }
      }

      if (pn != N) {
        for (int i = 0; i < 4; ++i) {
          if (anext[pn][i] < 1e8) {
            if (memo[py][px][i] > anext[pn][i]) {
              memo[py][px][i] = anext[pn][i];
              que.push(aa{px, py, anext[pn][i], i});
            }
          }
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
ld eps = 1e-9;

struct aa {
  int x;
  int y;
  int n;
  int w;
};
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, 1, 0, -1};
class Compare {
public:
  // aa?????????????????¶
  bool operator()(const aa &l, const aa &r) { return l.n > r.n; }
};

vector<vector<int>> wheres(4);
vector<vector<int>> anext;
int main() {
  while (1) {
    int H, W, N;
    cin >> H >> W >> N;
    anext.clear();
    wheres.resize(4);
    if (!H)
      break;
    string st;
    cin >> st;
    int way = 3;
    vector<int> ways;
    ways.push_back(way);
    wheres[way].push_back(0);
    for (int i = 0; i < N; ++i) {
      if (st[i] == 'L') {
        way = (way + 1) % 4;
      } else {
        way = (way + 3) % 4;
      }
      ways.push_back(way);
      wheres[way].push_back(i + 1);
    }
    for (int i = 0; i < N; ++i) {
      vector<int> v;
      for (int j = 0; j < 4; ++j) {
        auto it = upper_bound(wheres[j].begin(), wheres[j].end(), i);
        if (it == wheres[j].end())
          v.push_back(1e8);
        else
          v.push_back(*it);
      }
      for (int j = 0; j < ways[i]; ++j) {
        rotate(v.begin(), v.end(), v.end());
      }
      anext.push_back(v);
    }
    wheres.clear();
    vector<vector<int>> field(H + 2, vector<int>(W + 2, 0));
    for (int x = 0; x <= W + 1; ++x) {
      int y = 0;
      field[y][x] = 1;
      y = H + 1;
      field[y][x] = 1;
    }
    for (int y = 0; y <= H + 1; ++y) {
      int x = 0;
      field[y][x] = 1;
      x = W + 1;
      field[y][x] = 1;
    }
    int memo[1001][1001][4];
    for (int i = 0; i < 1001; ++i) {
      for (int j = 0; j < 1001; ++j) {
        for (int k = 0; k < 4; ++k) {
          memo[i][j][k] = 1e8;
        }
      }
    }
    int gx, gy;
    int sx, sy;
    for (int i = 0; i < H; ++i) {
      string ast;
      cin >> ast;
      for (int j = 0; j < W; ++j) {
        if (ast[j] == 'G') {
          gx = j + 1;
          gy = i + 1;
        } else if (ast[j] == 'S') {
          sx = j + 1;
          sy = i + 1;
        } else if (ast[j] == '#') {
          field[i + 1][j + 1] = true;
        }
      }
    }
    priority_queue<aa, vector<aa>, Compare> que;
    que.push(aa{sx, sy, 0, 3});
    memo[sy][sx][3] = 0;
    string ans = "No";
    while (!que.empty()) {
      aa atop(que.top());
      que.pop();
      const int px = atop.x;
      const int py = atop.y;
      const int pn = atop.n;
      const int pw = atop.w;
      if (px == gx && py == gy) {
        ans = "Yes";
        break;
      }
      {
        const int nx = px + dx[pw];
        const int ny = py + dy[pw];
        if (!field[ny][nx]) {
          if (memo[ny][nx][pw] > pn) {
            memo[ny][nx][pw] = pn;
            que.push(aa{nx, ny, pn, pw});
          }
        }
      }

      if (pn != N) {
        for (int i = 0; i < 4; ++i) {
          if (anext[pn][i] < 1e8) {
            if (memo[py][px][i] > anext[pn][i]) {
              memo[py][px][i] = anext[pn][i];
              que.push(aa{px, py, anext[pn][i], i});
            }
          }
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}