Problem p02644 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

/*
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
*/

typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef pair<long long, long long> pll;
typedef vector<pll> vpll;

typedef long double ld;
typedef vector<ld> vld;

typedef vector<bool> vb;

#define rep(i, n) for (ll i = 0; i < (n); i++)
#define reps(i, n) for (ll i = 1; i <= (n); i++)
#define rrep(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rreps(i, n) for (ll i = (n); i >= 1; i--)
#define all(v) (v).begin(), (v).end()

template <class T> void chmin(T &a, T b) { a = min(a, b); }
template <class T> void chmax(T &a, T b) { a = max(a, b); }

constexpr int INF = 1 << 30;
constexpr ll INFL = 1LL << 60;
constexpr ll MOD = 1000000007;
constexpr ld EPS = 1e-12;
ld PI = acos(-1.0);

void solve() {
  ll h, w, k;
  cin >> h >> w >> k;
  ll x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  --x1, --y1, --x2, --y2;
  vector<string> c(h);
  rep(i, h) cin >> c[i];

  vvll d(h, vll(w, INFL));
  d[x1][y1] = 0;

  queue<pll> q;
  q.push(make_pair(x1, y1));
  ll dx[] = {1, -1, 0, 0};
  ll dy[] = {0, 0, 1, -1};
  while (!q.empty()) {
    pll p = q.front();
    q.pop();
    for (ll i = 0; i < 4; i++) {
      for (ll j = 1; j <= k; j++) {
        ll x = p.first + j * dx[i];
        if (x < 0 || x >= h)
          break;
        ll y = p.second + j * dy[i];
        if (y < 0 || y >= w)
          break;
        if (c[x][y] == '@')
          break;
        if (d[x][y] <= d[p.first][p.second])
          break;
        d[x][y] = d[p.first][p.second] + 1;
        q.push(make_pair(x, y));
      }
    }
  }
  ll ans = d[x2][y2];
  if (ans == INFL)
    ans = -1;
  cout << ans << endl;
  return;
}

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

Fixed submission:
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

/*
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
*/

typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef pair<long long, long long> pll;
typedef vector<pll> vpll;

typedef long double ld;
typedef vector<ld> vld;

typedef vector<bool> vb;

#define rep(i, n) for (ll i = 0; i < (n); i++)
#define reps(i, n) for (ll i = 1; i <= (n); i++)
#define rrep(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rreps(i, n) for (ll i = (n); i >= 1; i--)
#define all(v) (v).begin(), (v).end()

template <class T> void chmin(T &a, T b) { a = min(a, b); }
template <class T> void chmax(T &a, T b) { a = max(a, b); }

constexpr int INF = 1 << 30;
constexpr ll INFL = 1LL << 60;
constexpr ll MOD = 1000000007;
constexpr ld EPS = 1e-12;
ld PI = acos(-1.0);

void solve() {
  ll h, w, k;
  cin >> h >> w >> k;
  ll x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  --x1, --y1, --x2, --y2;
  vector<string> c(h);
  rep(i, h) cin >> c[i];

  vvll d(h, vll(w, INFL));
  d[x1][y1] = 0;

  queue<pll> q;
  q.push(make_pair(x1, y1));
  ll dx[] = {1, -1, 0, 0};
  ll dy[] = {0, 0, 1, -1};
  while (!q.empty()) {
    pll p = q.front();
    q.pop();
    for (ll i = 0; i < 4; i++) {
      for (ll j = 1; j <= k; j++) {
        ll x = p.first + j * dx[i];
        if (x < 0 || x >= h)
          break;
        ll y = p.second + j * dy[i];
        if (y < 0 || y >= w)
          break;
        if (c[x][y] == '@')
          break;
        if (d[x][y] <= d[p.first][p.second])
          break;
        else if (d[x][y] <= d[p.first][p.second] + 1)
          continue;
        d[x][y] = d[p.first][p.second] + 1;
        q.push(make_pair(x, y));
      }
    }
  }
  ll ans = d[x2][y2];
  if (ans == INFL)
    ans = -1;
  cout << ans << endl;
  return;
}

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