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

#include <bits/stdc++.h>
using namespace std;
#define REP(i, a, n) for (int i = (a); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define FOR(it, c)                                                             \\
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
typedef long long ll;
typedef unsigned long long ull;

int vy[4] = {-1, 1, 0, 0};
int vx[4] = {0, 0, -1, 1};

struct ST {
  int y, x, a;
  int dist1, dist2;
  ST() {
    dist1 = -1;
    dist2 = -1;
  }
  ST(int y, int x, int a, int dist1, int dist2)
      : y(y), x(x), a(a), dist1(dist1), dist2(dist2) {}
};
bool operator<(const ST &a, const ST &b) {
  if (a.dist1 == b.dist1)
    return a.dist2 > b.dist2;
  return a.dist1 > b.dist1;
}

int main() {
  int H, W, K;
  cin >> H >> W >> K;
  int x1, y1, x2, y2;
  cin >> y1 >> x1 >> y2 >> x2;
  x1--;
  y1--;
  x2--;
  y2--;
  vector<string> field;
  rep(i, H) {
    string s;
    cin >> s;
    field.push_back(s);
  }

  vector<vector<vector<ST>>> ans(H, vector<vector<ST>>(W, vector<ST>(4)));

  priority_queue<ST> que;
  rep(k, 4) { que.push(ST(y1, x1, k, 0, 0)); }

  while (!que.empty()) {
    ST st = que.top();
    que.pop();
    if (ans[st.y][st.x][st.a].dist1 != -1)
      continue;
    ans[st.y][st.x][st.a] = st;
    // cout << st.y << " " << st.x << " " << st.a << " " << st.dist1 << " " <<
    // st.dist2 << endl;

    rep(k, 4) {
      if (k == st.a)
        continue;
      que.push(ST(st.y, st.x, k, st.dist1 + (st.dist2 > 0 ? 1 : 0), 0));
    }

    int nx = st.x + vx[st.a];
    int ny = st.y + vy[st.a];
    if (0 <= nx && nx < W && 0 <= ny && ny < H && field[ny][nx] == '.') {
      int dist1 = st.dist1;
      int dist2 = st.dist2 + 1;
      if (dist2 >= K) {
        dist1++;
        dist2 = 0;
      }
      que.push(ST(ny, nx, st.a, dist1, dist2));
    }
  }

  int ret = 99999999;
  rep(k, 4) {
    if (ans[y2][x2][k].dist1 != -1) {
      ret = min(ret, ans[y2][x2][k].dist1 + (ans[y2][x2][k].dist2 > 0 ? 1 : 0));
    }
  }
  if (ret == 99999999)
    ret = -1;

  cout << ret << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define REP(i, a, n) for (int i = (a); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define FOR(it, c)                                                             \\
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
typedef long long ll;
typedef unsigned long long ull;

struct ST {
  int dist;
  int y, x;
};

int vy[4] = {-1, 1, 0, 0};
int vx[4] = {0, 0, -1, 1};

int main() {
  int H, W, K;
  cin >> H >> W >> K;
  int x1, y1, x2, y2;
  cin >> y1 >> x1 >> y2 >> x2;
  x1--;
  y1--;
  x2--;
  y2--;
  vector<string> field;
  rep(i, H) {
    string s;
    cin >> s;
    field.push_back(s);
  }

  vector<vector<int>> dp(H, vector<int>(W, -1));
  vector<vector<int>> angle(H, vector<int>(W, 0));
  queue<ST> que_now, que_next;
  que_now.push((ST){0, y1, x1});
  dp[y1][x1] = 0;
  rep(turn, 1000000) {
    while (!que_now.empty()) {
      ST st = que_now.front();
      que_now.pop();
      rep(k, 4) {
        int xx = st.x + vx[k];
        int yy = st.y + vy[k];
        if (0 <= xx && xx < W && 0 <= yy && yy < H && field[yy][xx] == '.') {
          if (dp[yy][xx] == -1) {
            que_next.push((ST){st.dist + 1, yy, xx});
            angle[yy][xx] |= (1 << k);
          }
        }
      }
    }
    while (!que_next.empty()) {
      ST st = que_next.front();
      que_next.pop();
      // cout << st.y << " " << st.x << " " << st.dist << endl;
      que_now.push(st);
      dp[st.y][st.x] = st.dist;
      rep(k, 4) {
        if ((angle[st.y][st.x] >> k) & 1) {
          int xx = st.x + vx[k];
          int yy = st.y + vy[k];
          rep(ii, K - 1) {
            // cout << " " << yy << " " << xx << endl;
            if (0 <= xx && xx < W && 0 <= yy && yy < H &&
                field[yy][xx] == '.') {
              if (dp[yy][xx] == -1) {
                que_now.push((ST){st.dist, yy, xx});
                dp[yy][xx] = st.dist;
              }
            }
            if (field[yy][xx] != '.') {
              break;
            }
            xx += vx[k];
            yy += vy[k];
          }
        }
      }
      angle[st.y][st.x] = 0;
    }
  }

  cout << dp[y2][x2] << endl;

  return 0;
}