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

#include <bits/stdc++.h>

using namespace std;

const int vy[] = {0, 1, 0, -1}, vx[] = {1, 0, -1, 0};
const string temp = "RDLU";

const int mask = (1 << 18) - 1;

int H, W, P, sx, sy;
string S[50], V[10];

int solve() {

  queue<tuple<int, int, int, int>> que;
  int min_cost[50][50][10][10];
  memset(min_cost, -1, sizeof(min_cost));
  que.emplace(sx, sy, 0, 0);
  min_cost[sx][sy][0][0] = 0;

  while (!que.empty()) {
    int x, y, a, b;
    tie(x, y, a, b) = que.front();
    que.pop();
    string pat = V[a].substr(0, b);
    bool f = true;
    for (int i = 0; i < P; i++)
      f &= pat.find(V[i]) == string::npos;
    if (!f)
      continue;
    if (S[y][x] == 'G')
      return (min_cost[x][y][a][b]);
    for (int i = 0; i < 4; i++) {
      int nx = x + vx[i], ny = y + vy[i];
      if (nx < 0 || ny < 0 || nx >= W || ny >= H)
        continue;
      if (S[ny][nx] == '#')
        continue;
      string nextpat = pat + string(1, temp[i]);
      bool match = false;
      for (int j = 0; j < nextpat.size(); j++) {
        string st = nextpat.substr(j);
        for (int k = 0; k < P; k++) {
          if (V[k].find(st) == 0) {
            if (min_cost[nx][ny][k][st.size()] == -1) {
              que.emplace(nx, ny, k, st.size());
              min_cost[nx][ny][k][st.size()] = min_cost[x][y][a][b] + 1;
            }
            match = true;
            break;
          }
        }
        if (match)
          break;
      }
      if (!match && min_cost[nx][ny][0][0] == -1) {
        que.emplace(nx, ny, 0, 0);
        min_cost[nx][ny][0][0] = min_cost[x][y][a][b] + 1;
      }
    }
  }
  return (-1);
}

int main() {

  while (cin >> H >> W, H) {
    for (int i = 0; i < H; i++) {
      cin >> S[i];
      for (int j = 0; j < W; j++) {
        if (S[i][j] == 'S')
          sx = j, sy = i;
      }
    }
    cin >> P;
    for (int i = 0; i < P; i++) {
      cin >> V[i];
    }
    cout << solve() << endl;
  }
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int vy[] = {0, 1, 0, -1}, vx[] = {1, 0, -1, 0};
const string temp = "RDLU";

const int mask = (1 << 18) - 1;

int H, W, P, sx, sy;
string S[50], V[10];
bool bits[1 << 20];

int solve() {
  memset(bits, false, sizeof(bits));

  for (int i = 0; i < P; i++) {
    int curr = 0;
    for (char &c : V[i]) {
      curr = (curr << 2) | temp.find(c);
    }
    int massk = pow(4, V[i].size()) - 1;
    for (int j = 0; j < 1 << 20; j++) {
      if ((j & massk) == curr)
        bits[j] = true;
    }
  }

  queue<tuple<int, int, int>> que;
  unordered_map<int, int> min_cost[50][50];
  que.emplace(sx, sy, 0);
  min_cost[sx][sy][0] = 0;

  while (!que.empty()) {
    int x, y, bit;
    tie(x, y, bit) = que.front();
    que.pop();
    if (S[y][x] == 'G')
      return (min_cost[x][y][bit]);
    for (int i = 0; i < 4; i++) {
      int nx = x + vx[i], ny = y + vy[i];
      if (nx < 0 || ny < 0 || nx >= W || ny >= H)
        continue;
      if (S[ny][nx] == '#')
        continue;
      if (bits[(bit << 2) | i])
        continue;
      int nextbit = ((bit << 2) | i) & mask;
      if (min_cost[nx][ny].count(nextbit))
        continue;
      min_cost[nx][ny][nextbit] = min_cost[x][y][bit] + 1;
      que.emplace(nx, ny, nextbit);
    }
  }

  return (-1);
}

int main() {

  while (cin >> H >> W, H) {
    for (int i = 0; i < H; i++) {
      cin >> S[i];
      for (int j = 0; j < W; j++) {
        if (S[i][j] == 'S')
          sx = j, sy = i;
      }
    }
    cin >> P;
    for (int i = 0; i < P; i++) {
      cin >> V[i];
    }
    cout << solve() << endl;
  }
}