Problem p01459 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int N, M, A;
string s[111];
int dp[11][11][4][111][111];
bool in(int y, int x) { return 0 <= y && y < N && 0 <= x && x < M; }
int P(int d) {
  if (d & 1)
    return (d + 1) % 4;
  return (d + 3) % 4;
}
int Q(int d) {
  if (d & 1)
    return (d + 3) % 4;
  return (d + 1) % 4;
}
struct st {
  int y, x, d, p, q;
  st() {}
  st(int y, int x, int d, int p, int q) : y(y), x(x), d(d), p(p), q(q) {}
};
signed main() {
  cin >> N >> M >> A;
  for (int i = 0; i < N; i++)
    cin >> s[i];
  int ans = A * 2 + 1;
  int sy, sx;
  for (int i = 0; i < N; i++)
    for (int j = 0; j < M; j++)
      if (s[i][j] == 'S')
        sy = i, sx = j;
  memset(dp, -1, sizeof(dp));
  int ax[] = {0, 1, 0, -1};
  int ay[] = {-1, 0, 1, 0};
  deque<st> dq;
  dq.push_back(st(sy, sx, 2, 0, 0));
  dp[sy][sx][2][0][0] = 0;
  while (!dq.empty()) {
    st r = dq.front();
    dq.pop_front();
    int y = r.y, x = r.x, d = r.d, p = r.p, q = r.q;
    // cout<<y<<" "<<x<<" "<<d<<" "<<p<<" "<<q<<endl;
    if (p + q >= ans)
      break;
    if (s[y][x] == 'G') {
      ans = min(ans, p + q);
      break;
    }
    if (s[y][x] == 'S' && d == 0)
      continue;
    {
      int ny = y + ay[d], nx = x + ax[d], nd = d;
      if (in(ny, nx) && s[ny][nx] != '#') {
        if (dp[ny][nx][nd][p][q] < 0 ||
            dp[ny][nx][nd][p][q] > dp[y][x][d][p][q]) {
          dp[ny][nx][nd][p][q] = dp[y][x][d][p][q];
          dq.push_front(st(ny, nx, nd, p, q));
        }
      }
    }
    if (s[y][x] != 'S' && p < A) {
      int nd = P(d);
      int ny = y + ay[nd], nx = x + ax[nd];
      if (in(ny, nx) && s[ny][nx] != '#') {
        if (dp[ny][nx][nd][p + 1][q] < 0 ||
            dp[ny][nx][nd][p + 1][q] > dp[y][x][d][p][q] + 1) {
          dp[ny][nx][nd][p + 1][q] = dp[y][x][d][p][q] + 1;
          // cout<<p+1<<" "<<q<<endl;
          dq.push_back(st(ny, nx, nd, p + 1, q));
        }
      }
    }
    if (s[y][x] != 'S' && q < A) {
      int nd = Q(d);
      int ny = y + ay[nd], nx = x + ax[nd];
      if (in(ny, nx) && s[ny][nx] != '#') {
        if (dp[ny][nx][nd][p][q + 1] < 0 ||
            dp[ny][nx][nd][p][q + 1] > dp[y][x][d][p][q] + 1) {
          dp[ny][nx][nd][p][q + 1] = dp[y][x][d][p][q] + 1;
          dq.push_back(st(ny, nx, nd, p, q + 1));
        }
      }
    }
  }
  cout << (ans > A * 2 ? -1 : ans) << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int N, M, A;
string s[111];
int dp[111][111][4][11][11];
bool in(int y, int x) { return 0 <= y && y < N && 0 <= x && x < M; }
int P(int d) {
  if (d & 1)
    return (d + 1) % 4;
  return (d + 3) % 4;
}
int Q(int d) {
  if (d & 1)
    return (d + 3) % 4;
  return (d + 1) % 4;
}
struct st {
  int y, x, d, p, q;
  st() {}
  st(int y, int x, int d, int p, int q) : y(y), x(x), d(d), p(p), q(q) {}
};
signed main() {
  cin >> N >> M >> A;
  for (int i = 0; i < N; i++)
    cin >> s[i];
  int ans = A * 2 + 1;
  int sy, sx;
  for (int i = 0; i < N; i++)
    for (int j = 0; j < M; j++)
      if (s[i][j] == 'S')
        sy = i, sx = j;
  memset(dp, -1, sizeof(dp));
  int ax[] = {0, 1, 0, -1};
  int ay[] = {-1, 0, 1, 0};
  deque<st> dq;
  dq.push_back(st(sy, sx, 2, 0, 0));
  dp[sy][sx][2][0][0] = 0;
  while (!dq.empty()) {
    st r = dq.front();
    dq.pop_front();
    int y = r.y, x = r.x, d = r.d, p = r.p, q = r.q;
    // cout<<y<<" "<<x<<" "<<d<<" "<<p<<" "<<q<<endl;
    if (p + q >= ans)
      break;
    if (s[y][x] == 'G') {
      ans = min(ans, p + q);
      break;
    }
    if (s[y][x] == 'S' && d == 0)
      continue;
    {
      int ny = y + ay[d], nx = x + ax[d], nd = d;
      if (in(ny, nx) && s[ny][nx] != '#') {
        if (dp[ny][nx][nd][p][q] < 0 ||
            dp[ny][nx][nd][p][q] > dp[y][x][d][p][q]) {
          dp[ny][nx][nd][p][q] = dp[y][x][d][p][q];
          dq.push_front(st(ny, nx, nd, p, q));
        }
      }
    }
    if (s[y][x] != 'S' && p < A) {
      int nd = P(d);
      int ny = y + ay[nd], nx = x + ax[nd];
      if (in(ny, nx) && s[ny][nx] != '#') {
        if (dp[ny][nx][nd][p + 1][q] < 0 ||
            dp[ny][nx][nd][p + 1][q] > dp[y][x][d][p][q] + 1) {
          dp[ny][nx][nd][p + 1][q] = dp[y][x][d][p][q] + 1;
          // cout<<p+1<<" "<<q<<endl;
          dq.push_back(st(ny, nx, nd, p + 1, q));
        }
      }
    }
    if (s[y][x] != 'S' && q < A) {
      int nd = Q(d);
      int ny = y + ay[nd], nx = x + ax[nd];
      if (in(ny, nx) && s[ny][nx] != '#') {
        if (dp[ny][nx][nd][p][q + 1] < 0 ||
            dp[ny][nx][nd][p][q + 1] > dp[y][x][d][p][q] + 1) {
          dp[ny][nx][nd][p][q + 1] = dp[y][x][d][p][q] + 1;
          dq.push_back(st(ny, nx, nd, p, q + 1));
        }
      }
    }
  }
  cout << (ans > A * 2 ? -1 : ans) << endl;
  return 0;
}