Problem p02644 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, m, n) for (ll i = m; i < (ll)(n); i++)
#define rrep(i, n, m) for (ll i = n; i >= (ll)(m); i--)
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll MOD = 1000000007;
const ll INF = 1000000000000000000L;
#ifdef __DEBUG

/**
 * For DEBUG
 * https://github.com/ta7uw/cpp-pyprint
 */
#include "cpp-pyprint/pyprint.h"

#endif

void solve() {
  ll H, W, K;
  cin >> H >> W >> K;
  ll x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  x2--;
  y1--;
  y2--;
  vector<string> grid(H);
  rep(i, H) cin >> grid[i];
  vector<vector<ll>> cost(H, vector<ll>(W, 0));

  queue<pair<P, ll>> que;
  cost[x1][y1] = INF;
  que.push({{x1, y1}, 0});
  while (!que.empty()) {
    auto q = que.front();
    que.pop();
    P p = q.first;
    ll c = q.second;
    rep(i, 4) {
      rep2(k, 1, K + 1) {
        ll x = p.first + k * dx[i];
        ll y = p.second + k * dy[i];
        if (x >= 0 && x < H && y >= 0 && y < W) {
          if (grid[x][y] == '.') {
            if (cost[x][y] == 0) {
              cost[x][y] = c + 1;
              que.push({{x, y}, cost[x][y]});
            }
          } else {
            break;
          }
        } else {
          break;
        }
      }
    }
  }
  ll ans = cost[x2][y2];
  if (ans == 0) {
    cout << -1 << '\n';
  } else {
    cout << ans << '\n';
  }
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  solve();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, m, n) for (ll i = m; i < (ll)(n); i++)
#define rrep(i, n, m) for (ll i = n; i >= (ll)(m); i--)
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll MOD = 1000000007;
const ll INF = 1000000000000000000L;
#ifdef __DEBUG

/**
 * For DEBUG
 * https://github.com/ta7uw/cpp-pyprint
 */
#include "cpp-pyprint/pyprint.h"

#endif

void solve() {
  ll H, W, K;
  cin >> H >> W >> K;
  ll x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  x2--;
  y1--;
  y2--;
  vector<string> grid(H);
  rep(i, H) cin >> grid[i];
  vector<vector<ll>> cost(H, vector<ll>(W, 0));

  queue<pair<P, ll>> que;
  cost[x1][y1] = INF;
  que.push({{x1, y1}, 0});
  while (!que.empty()) {
    auto q = que.front();
    que.pop();
    P p = q.first;
    ll c = q.second;
    rep(i, 4) {
      rep2(k, 1, K + 1) {
        ll x = p.first + k * dx[i];
        ll y = p.second + k * dy[i];
        if (x >= 0 && x < H && y >= 0 && y < W) {
          if (grid[x][y] == '.') {
            if (cost[x][y] == 0) {
              cost[x][y] = c + 1;
              que.push({{x, y}, cost[x][y]});
            } else {
              if (cost[x][y] < q.second + 1) {
                break;
              }
            }
          } else {
            break;
          }
        } else {
          break;
        }
      }
    }
  }
  ll ans = cost[x2][y2];
  if (ans == 0) {
    cout << -1 << '\n';
  } else {
    cout << ans << '\n';
  }
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  solve();
  return 0;
}