Problem p02644 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <vector>
// #include <boost/multiprecision/cpp_int.hpp>
#include <bitset>
#include <complex>
#include <cstdio>
#include <list>

//< in.txt > out.txt
using namespace std;
// std::ios::sync_with_stdio(false);
// std::cin.tie(0);
const long long MOD = 998244353;
const long long INF = 1e18;
typedef long long LL;
typedef long double LD;
// typedef boost::multiprecision::cpp_int bigint;
typedef pair<LL, LL> PLL;
typedef pair<LD, LL> pdl;
typedef pair<LD, LD> pdd;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef unsigned long long ULL;

// TT(T,T):=モノイドの乗算
// require Monoid
template <class T> class Segtree {
private:
  vector<T> seg;
  LL RN;
  typedef function<T(T, T)> TT;
  TT f;
  T te;

public:
  Segtree(LL N, TT _f, T e) : te(e) {
    RN = 1;
    while (RN < N)
      RN *= 2;
    seg.resize(2 * RN, te);
    f = _f;
  }
  Segtree(vector<T> &V, TT _f, T e) : te(e) {
    LL N = V.size();
    RN = 1;
    while (RN < N)
      RN *= 2;
    seg.resize(2 * RN, te);
    f = _f;
    for (LL n = 0; n < N; n++)
      seg[RN + n] = V[n];
    for (LL k = RN - 1; k >= 1; k--) {
      seg[k] = f(seg[2 * k], seg[2 * k + 1]);
    }
  }
  // set n-th as x(0 index!!!!!)
  void set(LL n, T x) {
    seg[RN + n] = x;
    n = (RN + n) / 2;
    while (n >= 1) {
      seg[n] = f(seg[2 * n], seg[2 * n + 1]);
      n /= 2;
    }
  }
  // change n-th number p to x*p(0 index!!!!!)
  void addl(LL n, T x) {
    seg[RN + n] = f(x, seg[RN + n]);
    n = (RN + n) / 2;
    while (n >= 1) {
      seg[n] = f(seg[2 * n], seg[2 * n + 1]);
      n /= 2;
    }
  }
  // change n-th number p to p*x(0 index!!!!!)
  void addr(LL n, T x) {
    seg[RN + n] = f(seg[RN + n], x);
    n = (RN + n) / 2;
    while (n >= 1) {
      seg[n] = f(seg[2 * n], seg[2 * n + 1]);
      n /= 2;
    }
  }
  // get [l,r] (0 index!!!!!)
  T get(LL l, LL r) {
    T ansl = te, ansr = te;
    r++;
    l += RN;
    r += RN;
    while (l < r) {
      if (l & 1) {
        ansl = f(ansl, seg[l]);
        l++;
      }
      if (r & 1) {
        r--;
        ansr = f(seg[r], ansr);
      }
      l >>= 1;
      r >>= 1;
    }
    return f(ansl, ansr);
  }
  // get n-th number(0 index!!!!!)
  T get(LL n) { return seg[RN + n]; }
  T operator[](LL n) { return seg[RN + n]; }
};

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  LL H, W, K;
  cin >> H >> W >> K;
  LL x0, y0, x1, y1;
  cin >> y0 >> x0 >> y1 >> x1;
  vector<vector<bool>> map;
  map.resize(H + 2, vector<bool>(W + 2, false));
  for (LL y = 1; y <= H; y++) {
    string S;
    cin >> S;
    for (LL x = 1; x <= W; x++) {
      if (S[x - 1] == '.')
        map[y][x] = true;
    }
  }
  VVLL res;
  res.resize(H + 2, VLL(W + 2, 1e10));
  res[y0][x0] = 0;
  queue<PLL> q;
  q.push(PLL(y0, x0));
  LL vx[4] = {0, 1, 0, -1};
  LL vy[4] = {1, 0, -1, 0};
  while (!q.empty()) {
    LL cy = q.front().first;
    LL cx = q.front().second;
    q.pop();
    LL d = res[cy][cx];
    for (LL v = 0; v < 4; v++) {
      for (LL k = 1; k <= K; k++) {
        LL nx = cx + vx[v] * k;
        LL ny = cy + vy[v] * k;
        if (!map[ny][nx])
          break;
        if (res[ny][nx] <= d)
          break;
        res[ny][nx] = d + 1;
        q.push(PLL(ny, nx));
      }
    }
  }
  if (res[y1][x1] == 1e10)
    cout << -1 << "\n";
  else
    cout << res[y1][x1] << "\n";
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <vector>
// #include <boost/multiprecision/cpp_int.hpp>
#include <bitset>
#include <complex>
#include <cstdio>
#include <list>

//< in.txt > out.txt
using namespace std;
// std::ios::sync_with_stdio(false);
// std::cin.tie(0);
const long long MOD = 998244353;
const long long INF = 1e18;
typedef long long LL;
typedef long double LD;
// typedef boost::multiprecision::cpp_int bigint;
typedef pair<LL, LL> PLL;
typedef pair<LD, LL> pdl;
typedef pair<LD, LD> pdd;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef unsigned long long ULL;

// TT(T,T):=モノイドの乗算
// require Monoid
template <class T> class Segtree {
private:
  vector<T> seg;
  LL RN;
  typedef function<T(T, T)> TT;
  TT f;
  T te;

public:
  Segtree(LL N, TT _f, T e) : te(e) {
    RN = 1;
    while (RN < N)
      RN *= 2;
    seg.resize(2 * RN, te);
    f = _f;
  }
  Segtree(vector<T> &V, TT _f, T e) : te(e) {
    LL N = V.size();
    RN = 1;
    while (RN < N)
      RN *= 2;
    seg.resize(2 * RN, te);
    f = _f;
    for (LL n = 0; n < N; n++)
      seg[RN + n] = V[n];
    for (LL k = RN - 1; k >= 1; k--) {
      seg[k] = f(seg[2 * k], seg[2 * k + 1]);
    }
  }
  // set n-th as x(0 index!!!!!)
  void set(LL n, T x) {
    seg[RN + n] = x;
    n = (RN + n) / 2;
    while (n >= 1) {
      seg[n] = f(seg[2 * n], seg[2 * n + 1]);
      n /= 2;
    }
  }
  // change n-th number p to x*p(0 index!!!!!)
  void addl(LL n, T x) {
    seg[RN + n] = f(x, seg[RN + n]);
    n = (RN + n) / 2;
    while (n >= 1) {
      seg[n] = f(seg[2 * n], seg[2 * n + 1]);
      n /= 2;
    }
  }
  // change n-th number p to p*x(0 index!!!!!)
  void addr(LL n, T x) {
    seg[RN + n] = f(seg[RN + n], x);
    n = (RN + n) / 2;
    while (n >= 1) {
      seg[n] = f(seg[2 * n], seg[2 * n + 1]);
      n /= 2;
    }
  }
  // get [l,r] (0 index!!!!!)
  T get(LL l, LL r) {
    T ansl = te, ansr = te;
    r++;
    l += RN;
    r += RN;
    while (l < r) {
      if (l & 1) {
        ansl = f(ansl, seg[l]);
        l++;
      }
      if (r & 1) {
        r--;
        ansr = f(seg[r], ansr);
      }
      l >>= 1;
      r >>= 1;
    }
    return f(ansl, ansr);
  }
  // get n-th number(0 index!!!!!)
  T get(LL n) { return seg[RN + n]; }
  T operator[](LL n) { return seg[RN + n]; }
};

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  LL H, W, K;
  cin >> H >> W >> K;
  LL x0, y0, x1, y1;
  cin >> y0 >> x0 >> y1 >> x1;
  vector<vector<bool>> map;
  map.resize(H + 2, vector<bool>(W + 2, false));
  for (LL y = 1; y <= H; y++) {
    string S;
    cin >> S;
    for (LL x = 1; x <= W; x++) {
      if (S[x - 1] == '.')
        map[y][x] = true;
    }
  }
  VVLL res;
  res.resize(H + 2, VLL(W + 2, 1e10));
  res[y0][x0] = 0;
  queue<PLL> q;
  q.push(PLL(y0, x0));
  LL vx[4] = {0, 1, 0, -1};
  LL vy[4] = {1, 0, -1, 0};
  while (!q.empty()) {
    LL cy = q.front().first;
    LL cx = q.front().second;
    q.pop();
    LL d = res[cy][cx];
    for (LL v = 0; v < 4; v++) {
      for (LL k = 1; k <= K; k++) {
        LL nx = cx + vx[v] * k;
        LL ny = cy + vy[v] * k;
        if (!map[ny][nx])
          break;
        if (res[ny][nx] <= d)
          break;
        if (res[ny][nx] != d + 1) {
          res[ny][nx] = d + 1;
          q.push(PLL(ny, nx));
        }
      }
    }
  }
  if (res[y1][x1] == 1e10)
    cout << -1 << "\n";
  else
    cout << res[y1][x1] << "\n";
  return 0;
}