Problem p02644 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
#define fast_io                                                                \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define ll long long int
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, n) for (int i = 1; i <= (int)(n); i++)
#define REP(i, n) for (int i = n - 1; i >= 0; i--)
#define REPS(i, n) for (int i = n; i > 0; i--)
#define MOD (long long int)(1e9 + 7)
#define INF (int)(1e9)
#define LINF (long long int)(1e18)
#define chmax(a, b) a = (((a) < (b)) ? (b) : (a))
#define chmin(a, b) a = (((a) > (b)) ? (b) : (a))
#define all(v) v.begin(), v.end()
typedef pair<int, int> Pii;
typedef pair<ll, ll> Pll;

ll mpow(ll a, ll b) {
  if (b == 0)
    return 1;
  else if (b % 2 == 0) {
    ll memo = mpow(a, b / 2);
    return memo * memo % MOD;
  } else
    return mpow(a, b - 1) * a % MOD;
}
ll lpow(ll a, ll b) {
  if (b == 0)
    return 1;
  else if (b % 2 == 0) {
    ll memo = lpow(a, b / 2);
    return memo * memo;
  } else
    return lpow(a, b - 1) * a;
}
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
vector<ll> kaijo_memo;
ll kaijo(ll n) {
  if (kaijo_memo.size() > n)
    return kaijo_memo[n];
  if (kaijo_memo.size() == 0)
    kaijo_memo.push_back(1);
  while (kaijo_memo.size() <= n)
    kaijo_memo.push_back(kaijo_memo[kaijo_memo.size() - 1] * kaijo_memo.size() %
                         MOD);
  return kaijo_memo[n];
}
vector<ll> gyaku_kaijo_memo;
ll gyaku_kaijo(ll n) {
  if (gyaku_kaijo_memo.size() > n)
    return gyaku_kaijo_memo[n];
  if (gyaku_kaijo_memo.size() == 0)
    gyaku_kaijo_memo.push_back(1);
  while (gyaku_kaijo_memo.size() <= n)
    gyaku_kaijo_memo.push_back(gyaku_kaijo_memo[gyaku_kaijo_memo.size() - 1] *
                               mpow(gyaku_kaijo_memo.size(), MOD - 2) % MOD);
  return gyaku_kaijo_memo[n];
}

ll nCr(ll n, ll r) {
  if (n == r)
    return 1; // 0個の丸と-1個の棒みたいな時に時に効く？不安.
  if (n < r || r < 0)
    return 0;
  ll ret = 1;
  if (n <= 1e7) {
    ret *= kaijo(n);
    ret %= MOD;
    ret *= gyaku_kaijo(r);
    ret %= MOD;
    ret *= gyaku_kaijo(n - r);
    ret %= MOD;
  } else {
    rep(i, r) {
      ret *= n - i;
      ret %= MOD;
      ret *= mpow(r - i, MOD - 2);
      ret %= MOD;
    }
  }
  return ret;
}

struct hoge {
  ll y, x, t;
  pair<ll, ll> d;
};

int main(void) {
  fast_io cout << fixed << setprecision(15);

  ll h, w, k;
  cin >> h >> w >> k;
  ll x1, y1, x2, y2;
  cin >> y1 >> x1 >> y2 >> x2;
  x1--;
  y1--;
  x2--;
  y2--;
  vector<string> S(h);
  rep(y, h) { cin >> S[y]; }
  // dp[y][x][j]:
  // (y,x)に行く方法のうち最後の移動がUDLR[j]であるもののうち、最小手数,
  // 最小歩数
  vector<vector<vector<pair<ll, ll>>>> dp(h);
  rep(y, h) {
    dp[y] = vector<vector<pair<ll, ll>>>(w);
    rep(x, w) { dp[y][x] = vector<pair<ll, ll>>(4, {LINF, LINF}); }
  }

  auto cmp = [](const hoge &l, const hoge &r) { return l.d < r.d; };
  priority_queue<hoge, vector<hoge>, decltype(cmp)> q(cmp);
  rep(t, 4) { q.push({y1, x1, t, {0, 0}}); }

  while (q.size() > 0) {
    hoge fuga = q.top();
    q.pop();
    ll y = fuga.y;
    ll x = fuga.x;
    if (y < 0 || y >= h || x < 0 || x >= w)
      continue;
    if (S[y][x] == '@')
      continue;
    ll t = fuga.t;
    auto d = fuga.d;
    if (dp[y][x][t] < d)
      continue;
    dp[y][x][t] = d;
    rep(ti, 4) {
      auto c = d;
      if (ti == t) {
        c.second++;
        if (c.second == k + 1) {
          c.first++;
          c.second = 1;
        }
      } else {
        c.first++;
        c.second = 1;
      }

      ll ny = y, nx = x;
      if (ti == 0) {
        ny = y - 1;
      } else if (ti == 1) {
        ny = y + 1;
      } else if (ti == 2) {
        nx = x - 1;
      } else {
        nx = x + 1;
      }

      if (ny < 0 || ny >= h || nx < 0 || nx >= w)
        continue;

      if (dp[ny][nx][ti] <= c)
        continue;
      dp[ny][nx][ti] = c;
      q.push({ny, nx, ti, c});
    }
  }

  pair<ll, ll> ans = {LINF, LINF};
  rep(t, 4) {
    pair<ll, ll> fuga = dp[y2][x2][t];
    if (fuga.first == LINF)
      continue;
    if (fuga.second > 0) {
      fuga.first++;
      fuga.second = 0;
    }
    chmin(ans, fuga);
  }
  if (ans.first == LINF) {
    cout << -1 << endl;
    return 0;
  }
  cout << ans.first << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
#define fast_io                                                                \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define ll long long int
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, n) for (int i = 1; i <= (int)(n); i++)
#define REP(i, n) for (int i = n - 1; i >= 0; i--)
#define REPS(i, n) for (int i = n; i > 0; i--)
#define MOD (long long int)(1e9 + 7)
#define INF (int)(1e9)
#define LINF (long long int)(1e18)
#define chmax(a, b) a = (((a) < (b)) ? (b) : (a))
#define chmin(a, b) a = (((a) > (b)) ? (b) : (a))
#define all(v) v.begin(), v.end()
typedef pair<int, int> Pii;
typedef pair<ll, ll> Pll;

ll mpow(ll a, ll b) {
  if (b == 0)
    return 1;
  else if (b % 2 == 0) {
    ll memo = mpow(a, b / 2);
    return memo * memo % MOD;
  } else
    return mpow(a, b - 1) * a % MOD;
}
ll lpow(ll a, ll b) {
  if (b == 0)
    return 1;
  else if (b % 2 == 0) {
    ll memo = lpow(a, b / 2);
    return memo * memo;
  } else
    return lpow(a, b - 1) * a;
}
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
vector<ll> kaijo_memo;
ll kaijo(ll n) {
  if (kaijo_memo.size() > n)
    return kaijo_memo[n];
  if (kaijo_memo.size() == 0)
    kaijo_memo.push_back(1);
  while (kaijo_memo.size() <= n)
    kaijo_memo.push_back(kaijo_memo[kaijo_memo.size() - 1] * kaijo_memo.size() %
                         MOD);
  return kaijo_memo[n];
}
vector<ll> gyaku_kaijo_memo;
ll gyaku_kaijo(ll n) {
  if (gyaku_kaijo_memo.size() > n)
    return gyaku_kaijo_memo[n];
  if (gyaku_kaijo_memo.size() == 0)
    gyaku_kaijo_memo.push_back(1);
  while (gyaku_kaijo_memo.size() <= n)
    gyaku_kaijo_memo.push_back(gyaku_kaijo_memo[gyaku_kaijo_memo.size() - 1] *
                               mpow(gyaku_kaijo_memo.size(), MOD - 2) % MOD);
  return gyaku_kaijo_memo[n];
}

ll nCr(ll n, ll r) {
  if (n == r)
    return 1; // 0個の丸と-1個の棒みたいな時に時に効く？不安.
  if (n < r || r < 0)
    return 0;
  ll ret = 1;
  if (n <= 1e7) {
    ret *= kaijo(n);
    ret %= MOD;
    ret *= gyaku_kaijo(r);
    ret %= MOD;
    ret *= gyaku_kaijo(n - r);
    ret %= MOD;
  } else {
    rep(i, r) {
      ret *= n - i;
      ret %= MOD;
      ret *= mpow(r - i, MOD - 2);
      ret %= MOD;
    }
  }
  return ret;
}

struct hoge {
  ll y, x, t;
  pair<ll, ll> d;
};

int main(void) {
  fast_io cout << fixed << setprecision(15);

  ll h, w, k;
  cin >> h >> w >> k;
  ll x1, y1, x2, y2;
  cin >> y1 >> x1 >> y2 >> x2;
  x1--;
  y1--;
  x2--;
  y2--;
  vector<string> S(h);
  rep(y, h) { cin >> S[y]; }
  // dp[y][x][j]:
  // (y,x)に行く方法のうち最後の移動がUDLR[j]であるもののうち、最小手数,
  // 最小歩数
  vector<vector<vector<pair<ll, ll>>>> dp(h);
  rep(y, h) {
    dp[y] = vector<vector<pair<ll, ll>>>(w);
    rep(x, w) { dp[y][x] = vector<pair<ll, ll>>(4, {LINF, LINF}); }
  }

  auto cmp = [](const hoge &l, const hoge &r) { return l.d > r.d; };
  priority_queue<hoge, vector<hoge>, decltype(cmp)> q(cmp);
  rep(t, 4) { q.push({y1, x1, t, {0, 0}}); }

  while (q.size() > 0) {
    hoge fuga = q.top();
    q.pop();
    ll y = fuga.y;
    ll x = fuga.x;
    if (y < 0 || y >= h || x < 0 || x >= w)
      continue;
    if (S[y][x] == '@')
      continue;
    ll t = fuga.t;
    auto d = fuga.d;
    if (dp[y][x][t] < d)
      continue;
    dp[y][x][t] = d;
    rep(ti, 4) {
      auto c = d;
      if (ti == t) {
        c.second++;
        if (c.second == k + 1) {
          c.first++;
          c.second = 1;
        }
      } else {
        c.first++;
        c.second = 1;
      }

      ll ny = y, nx = x;
      if (ti == 0) {
        ny = y - 1;
      } else if (ti == 1) {
        ny = y + 1;
      } else if (ti == 2) {
        nx = x - 1;
      } else {
        nx = x + 1;
      }

      if (ny < 0 || ny >= h || nx < 0 || nx >= w)
        continue;

      if (dp[ny][nx][ti] <= c)
        continue;
      dp[ny][nx][ti] = c;
      q.push({ny, nx, ti, c});
    }
  }

  pair<ll, ll> ans = {LINF, LINF};
  rep(t, 4) {
    pair<ll, ll> fuga = dp[y2][x2][t];
    if (fuga.first == LINF)
      continue;
    if (fuga.second > 0) {
      fuga.first++;
      fuga.second = 0;
    }
    chmin(ans, fuga);
  }
  if (ans.first == LINF) {
    cout << -1 << endl;
    return 0;
  }
  cout << ans.first << endl;
  return 0;
}