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

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toStr(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define REP(i, n) FOR(i, 0, (n)-1)
const double EPS = 1e-12;
const double PI = acos(-1.0);
const int INF = INT_MAX / 10;

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

struct state {
  int x, y, d, p, q;
  state(int x, int y, int d, int p, int q) : x(x), y(y), d(d), p(p), q(q){};
};

typedef vector<vvi> vvvi;
typedef vector<vvvi> vvvvi;

int main() {
  int N, M, A;
  cin >> N >> M >> A;
  vs field(N);
  int sx, sy, gx, gy;
  REP(i, N) {
    cin >> field[i];
    REP(j, M) {
      if (field[i][j] == 'S') {
        sx = j;
        sy = i;
      }
      if (field[i][j] == 'G') {
        gx = j;
        gy = i;
      }
    }
  }

  if (sy == N - 1) {
    cout << -1 << endl;
    return 0;
  }

  vector<vvvvi> visited(N, vvvvi(M, vvvi(4, vvi(A + 1, vi(A + 1)))));
  stack<state> S;
  S.push(state(sx, sy, 2, 0, 0));
  visited[sy][sx][2][0][0] = 1;
  int ans = INF;
  while (!S.empty()) {
    state st = S.top();
    S.pop();

    if (st.x == gx && st.y == gy) {
      ans = min(ans, st.p + st.q);
    }

    if (st.x == sx && st.y == sy && st.d == 0) {
      continue;
    }

    REP(d, 4) {
      if (abs(st.d - d) == 2) {
        continue;
      }

      int nx = st.x + dx[d];
      int ny = st.y + dy[d];
      int np = st.p;
      int nq = st.q;
      if (nx < 0 || M <= nx || ny < 0 || N <= ny || field[ny][nx] == '#') {
        continue;
      }

      if (st.d == d) {

      } else if (st.d + d == 3) {
        if (st.x == sx && st.y == sy) {
          continue;
        }
        np++;
      } else {
        if (st.x == sx && st.y == sy) {
          continue;
        }
        nq++;
      }

      if (A < np || A < nq) {
        continue;
      }

      if (!visited[ny][nx][d][np][nq]) {
        visited[ny][nx][d][np][nq] = 1;
        S.push(state(nx, ny, d, np, nq));
      }
    }
  }

  if (ans == INF) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EACH(t, i, c) for (t::iretator i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define REP(i, n) FOR(i, 0, (n)-1)
const double EPS = 1e-10;
const double PI = acos(-1.0);

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

struct state {
  char x, y, d, p, q, t;
  state(char x, char y, char d, char p, char q, char t)
      : x(x), y(y), d(d), p(p), q(q), t(t){};
};

int main() {
  int N, M, A;
  cin >> N >> M >> A;

  vs field(N + 2);
  string s;
  REP(i, M + 2) { s += '#'; }
  field[0] = field[N + 1] = s;

  REP(i, N) {
    cin >> s;
    field[i + 1] = '#' + s + '#';
  }

  char sx, sy, gx, gy;
  REP(i, N + 2) {
    REP(j, M + 2) {
      if (field[i][j] == 'S') {
        sy = i;
        sx = j;
      }
      if (field[i][j] == 'G') {
        gy = i;
        gx = j;
      }
    }
  }

  queue<state> Q;
  if (field[sy + 1][sx] == '#') {
    cout << -1 << endl;
    return 0;
  } else {
    Q.push(state(sx, sy + 1, 1, 0, 0, 0));
  }

  vvi turn(N + 2, vi(M + 2, INT_MAX));
  turn[sy + 1][sx] = 0;

  bool goal = false;
  int ans = INT_MAX;
  while (!Q.empty()) {
    state st = Q.front();
    Q.pop();
    char cx = st.x;
    char cy = st.y;

    if (cx == gx && cy == gy) {
      if (max(st.p, st.q) <= A) {
        goal = true;
        ans = min(ans, st.p + st.q);
      }
    } else {
      REP(d, 4) {
        char nx = cx + dx[d];
        char ny = cy + dy[d];
        if (turn[ny][nx] < st.t + 1) {
          continue;
        }
        if ((st.d + 2) % 4 == d) {
          continue;
        }
        if (cx == sx && cy == sy && st.d != d) {
          continue;
        }
        if (field[ny][nx] != '#') {
          turn[ny][nx] = st.t + 1;
          if (st.d != d) {
            if (st.d + d == 3) {
              Q.push(state(nx, ny, d, st.p, st.q + 1, st.t + 1));
            } else {
              Q.push(state(nx, ny, d, st.p + 1, st.q, st.t + 1));
            }
          } else {
            Q.push(state(nx, ny, d, st.p, st.q, st.t + 1));
          }
        }
      }
    }
  }

  if (goal) {
    cout << ans << endl;
  } else {
    cout << -1 << endl;
  }

  return 0;
}