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

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

// h, w, dir, P, Q
typedef struct _State {
  int h, w, dir;
  vector<int> pq;
} State;

int N, M, A;
int Sh, Sw, Gh, Gw;
vector<string> field;

// south is 0
int dh[] = {1, -1, 0, 0};
int dw[] = {0, 0, 1, -1};

int table[2][4] = {{2, 3, 0, 1}, {3, 2, 1, 0}};

int dist[110][110][4][11][11];

int bfs() {
  queue<State> que;
  // map<State, int> dist;
  memset(dist, -1, sizeof(dist));
  State s = State({Sh, Sw, 0, {A, A}});
  que.push(s);
  dist[s.h][s.w][s.dir][s.pq[0]][s.pq[1]] = 0;

  while (!que.empty()) {
    State u = que.front();
    que.pop();
    for (int nh = u.h + dh[u.dir], nw = u.w + dw[u.dir];
         0 <= nh && nh < N && 0 <= nw && nw < M && field[nh][nw] != '#';
         nh += dh[u.dir], nw += dw[u.dir]) {
      if (nh == Sh && nw == Sw && u.dir == 1)
        break;

      for (int i = -1; i < 2; i++) {
        if (nh == Sh && nw == Sw && i >= 0)
          break;
        State v({nh, nw, u.dir, u.pq});
        if (i >= 0) {
          if (u.pq[i] == 0)
            continue;
          v.pq[i]--;
          v.dir = table[i][u.dir];
        }

        if (dist[v.h][v.w][v.dir][v.pq[0]][v.pq[1]] < 0) {
          dist[v.h][v.w][v.dir][v.pq[0]][v.pq[1]] =
              dist[u.h][u.w][u.dir][u.pq[0]][u.pq[1]];
          if (i >= 0)
            dist[v.h][v.w][v.dir][v.pq[0]][v.pq[1]]++;
          if (v.h == Gh && v.w == Gw)
            return dist[v.h][v.w][v.dir][v.pq[0]][v.pq[1]];

          // cout << v.h << " " << v.w << " " <<
          // dist[v.h][v.w][v.dir][v.pq[0]][v.pq[1]] << endl;
          que.push(v);
        }
      }
    }
  }
  return -1;
}

int main() {
  cin >> N >> M >> A;
  field.resize(N);
  for (auto &s : field)
    cin >> s;

  for (int i = 0; i < N; i++)
    for (int j = 0; j < M; j++) {
      if (field[i][j] == 'S')
        Sh = i, Sw = j;
      if (field[i][j] == 'G')
        Gh = i, Gw = j;
    }

  cout << bfs() << endl;

  return 0;
}

Error: MLE

Fixed submission:

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

// h, w, dir, P, Q
typedef struct _State {
  int h, w, dir;
  vector<int> pq;
} State;

int N, M, A;
int Sh, Sw, Gh, Gw;
vector<string> field;

// south is 0
int dh[] = {1, -1, 0, 0};
int dw[] = {0, 0, 1, -1};

int table[2][4] = {{2, 3, 0, 1}, {3, 2, 1, 0}};

int dist[310][310][4][11][11];

int bfs() {
  queue<State> que;
  // map<State, int> dist;
  memset(dist, -1, sizeof(dist));
  State s = State({Sh, Sw, 0, {A, A}});
  que.push(s);
  dist[s.h][s.w][s.dir][s.pq[0]][s.pq[1]] = 0;

  while (!que.empty()) {
    State u = que.front();
    que.pop();
    for (int nh = u.h + dh[u.dir], nw = u.w + dw[u.dir];
         0 <= nh && nh < N && 0 <= nw && nw < M && field[nh][nw] != '#';
         nh += dh[u.dir], nw += dw[u.dir]) {
      if (nh == Sh && nw == Sw && u.dir == 1)
        break;

      for (int i = -1; i < 2; i++) {
        if (nh == Sh && nw == Sw && i >= 0)
          break;
        State v({nh, nw, u.dir, u.pq});
        if (i >= 0) {
          if (u.pq[i] == 0)
            continue;
          v.pq[i]--;
          v.dir = table[i][u.dir];
        }

        if (dist[v.h][v.w][v.dir][v.pq[0]][v.pq[1]] < 0) {
          dist[v.h][v.w][v.dir][v.pq[0]][v.pq[1]] =
              dist[u.h][u.w][u.dir][u.pq[0]][u.pq[1]];
          if (i >= 0)
            dist[v.h][v.w][v.dir][v.pq[0]][v.pq[1]]++;
          if (v.h == Gh && v.w == Gw)
            return dist[v.h][v.w][v.dir][v.pq[0]][v.pq[1]];

          // cout << v.h << " " << v.w << " " <<
          // dist[v.h][v.w][v.dir][v.pq[0]][v.pq[1]] << endl;
          que.push(v);
        }
      }
    }
  }
  return -1;
}

int main() {
  cin >> N >> M >> A;
  field.resize(N);
  for (auto &s : field)
    cin >> s;

  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++) {
      if (field[i][j] == 'S')
        Sh = i, Sw = j;
      if (field[i][j] == 'G')
        Gh = i, Gw = j;
    }

  cout << bfs() << endl;

  return 0;
}