Problem p01459 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
using namespace std;

int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};

class State {
public:
  int x, y, d, p, q, c;
  State(int x, int y, int d, int p, int q, int c)
      : x(x), y(y), d(d), p(p), q(q), c(c) {}

  bool operator<(const State &s) const { return c > s.c; }
};

int N, M, A;
bool memo[100][100][4][15][15];
string field[100];

bool invaild(int x, int y) {
  if (x < 0 || y < 0 || x >= M || y >= N)
    return true;
  if (field[y][x] == '#')
    return true;

  return false;
}

bool newState(State &s) { return !memo[s.x][s.y][s.d][s.p][s.q]; }

State straight(State s) {

  int tx = s.x + dx[s.d];
  int ty = s.y + dy[s.d];

  if (invaild(tx, ty))
    return s;

  return State(tx, ty, s.d, s.p, s.q, s.c);
}

State useP(State s) {
  if (s.p == 0)
    return s;

  int nd = (s.d + 1) % 2 + (s.d > 1 ? 2 : 0);
  int tx = s.x + dx[nd];
  int ty = s.y + dy[nd];

  if (invaild(tx, ty))
    return s;

  return State(tx, ty, nd, s.p - 1, s.q, s.c + 1);
}

int refQ(int d) {
  if (d == 0)
    return 3;
  if (d == 3)
    return 0;
  if (d == 1)
    return 2;

  return 1;
}

State useQ(State s) {
  if (s.q == 0)
    return s;

  int nd = refQ(s.d);
  int tx = s.x + dx[nd];
  int ty = s.y + dy[nd];

  if (invaild(tx, ty))
    return s;

  return State(tx, ty, nd, s.p, s.q - 1, s.c + 1);
}

int solve() {
  memset(memo, 0, sizeof(memo));

  int sx, sy;
  for (int i = 0; i < N; i++)
    for (int j = 0; j < M; j++) {
      if (field[i][j] == 'S') {
        sx = j;
        sy = i;
      }
    }

  for (int i = 0; i <= A; i++)
    for (int j = 0; j <= A; j++)
      memo[sx][sy][2][i][j] = true;

  priority_queue<State> q;
  q.push(State(sx, sy, 0, A, A, 0));

  while (!q.empty()) {
    State s = q.top();
    q.pop();
    if (!newState(s))
      continue;
    memo[s.x][s.y][s.d][s.p][s.q] = true;

    if (field[s.y][s.x] == 'G')
      return s.c;

    State ns = straight(s);
    if (newState(ns))
      q.push(ns);

    if (s.x != sx || s.y != sy) {
      ns = useP(s);
      if (newState(ns))
        q.push(ns);

      ns = useQ(s);
      if (newState(ns))
        q.push(ns);
    }
  }

  return -1;
}

int main() {
  cin >> N >> M >> A;
  for (int i = 0; i < N; i++)
    cin >> field[i];

  cout << solve() << endl;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
using namespace std;

int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};

class State {
public:
  int x, y, d, p, q, c;
  State(int x, int y, int d, int p, int q, int c)
      : x(x), y(y), d(d), p(p), q(q), c(c) {}

  bool operator<(const State &s) const { return c > s.c; }
};

int N, M, A;
bool memo[100][100][4][15][15];
string field[100];

bool invaild(int x, int y) {
  if (x < 0 || y < 0 || x >= M || y >= N)
    return true;
  if (field[y][x] == '#')
    return true;

  return false;
}

bool newState(State &s) { return !memo[s.x][s.y][s.d][s.p][s.q]; }

State straight(State s) {

  int tx = s.x + dx[s.d];
  int ty = s.y + dy[s.d];

  if (invaild(tx, ty))
    return s;

  return State(tx, ty, s.d, s.p, s.q, s.c);
}

State useP(State s) {
  if (s.p == 0)
    return s;

  int nd = (s.d + 1) % 2 + (s.d > 1 ? 2 : 0);
  int tx = s.x + dx[nd];
  int ty = s.y + dy[nd];

  if (invaild(tx, ty))
    return s;

  return State(tx, ty, nd, s.p - 1, s.q, s.c + 1);
}

int refQ(int d) {
  if (d == 0)
    return 3;
  if (d == 3)
    return 0;
  if (d == 1)
    return 2;

  return 1;
}

State useQ(State s) {
  if (s.q == 0)
    return s;

  int nd = refQ(s.d);
  int tx = s.x + dx[nd];
  int ty = s.y + dy[nd];

  if (invaild(tx, ty))
    return s;

  return State(tx, ty, nd, s.p, s.q - 1, s.c + 1);
}

int solve() {
  memset(memo, 0, sizeof(memo));

  int sx, sy;
  for (int i = 0; i < N; i++)
    for (int j = 0; j < M; j++) {
      if (field[i][j] == 'S') {
        sx = j;
        sy = i;
      }
    }

  priority_queue<State> q;
  q.push(State(sx, sy, 0, A, A, 0));

  while (!q.empty()) {
    State s = q.top();
    q.pop();
    if (!newState(s))
      continue;
    memo[s.x][s.y][s.d][s.p][s.q] = true;

    if (field[s.y][s.x] == 'G')
      return s.c;

    State ns = straight(s);
    if (newState(ns))
      q.push(ns);

    ns = useP(s);
    if (newState(ns))
      q.push(ns);

    ns = useQ(s);
    if (newState(ns))
      q.push(ns);
  }

  return -1;
}

int main() {

  for (int i = 0; i < N; i++)
    cin >> field[i];

  cout << solve() << endl;
}