Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> ii;
typedef tuple<ll, ll, ll> iii;
typedef vector<ll> vi;
typedef vector<ii> vii;
typedef vector<iii> viii;
typedef vector<vi> vvi;
typedef vector<vii> vvii;
#define REP(i, n) for (ll i = 0; i < n; ++i)
#define REPR(i, n) for (ll i = n - 1; i >= 0; --i)
#define FOR(i, m, n) for (ll i = m; i < n; ++i)
#define FORR(i, m, n) for (ll i = n - 1; i >= m; --i)
#define FORE(x, xs) for (const auto &x : xs)
#define FORI(i, v) for (auto i = v.begin(); i != v.end(); i++)
#define ALL(v) v.begin(), v.end()
#define CHMIN(x, y) x = min(x, y)
#define CHMAX(x, y) x = max(x, y)
#define YES(b) cout << ((b) ? "YES" : "NO") << endl
#define Yes(b) cout << ((b) ? "Yes" : "No") << endl

const int MAX = 1e6 + 10;
const ll INF = 1e18;

const int DX[4] = {-1, 0, 1, 0};
const int DY[4] = {0, -1, 0, 1};

int H, W, K;
int X1, Y1, X2, Y2;
vector<vector<bool>> C;

ll solve() {
  vvi dist(H, vi(W, INF));
  queue<ii> que;
  que.push(ii(X1, Y1));
  dist[X1][Y1] = 0;

  while (!que.empty()) {
    ll x, y;
    tie(x, y) = que.front();
    que.pop();

    REP(d, 4) {
      FOR(k, 1, K + 1) {
        int nx = x + DX[d] * k, ny = y + DY[d] * k;
        if (nx < 0 || H <= nx || ny < 0 || W <= ny || !C[nx][ny] ||
            dist[nx][ny] <= dist[x][y])
          break;
        if (dist[nx][ny] == INF) {
          dist[nx][ny] = dist[x][y] + 1;
          que.push(ii(nx, ny));
        }
      }
    }
  }

  if (dist[X2][Y2] == INF)
    return -1;
  return dist[X2][Y2];
}

int main() {
  cout << fixed << setprecision(15);
  cin >> H >> W >> K >> X1 >> Y1 >> X2 >> Y2;
  X1--;
  Y1--;
  X2--;
  Y2--;
  REP(h, H) {
    string s;
    cin >> s;
    C.push_back(vector<bool>(W));
    REP(w, W) if (s[w] == '.') C[h][w] = true;
  }
  cout << solve() << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> ii;
typedef tuple<ll, ll, ll> iii;
typedef vector<ll> vi;
typedef vector<ii> vii;
typedef vector<iii> viii;
typedef vector<vi> vvi;
typedef vector<vii> vvii;
#define REP(i, n) for (ll i = 0; i < n; ++i)
#define REPR(i, n) for (ll i = n - 1; i >= 0; --i)
#define FOR(i, m, n) for (ll i = m; i < n; ++i)
#define FORR(i, m, n) for (ll i = n - 1; i >= m; --i)
#define FORE(x, xs) for (const auto &x : xs)
#define FORI(i, v) for (auto i = v.begin(); i != v.end(); i++)
#define ALL(v) v.begin(), v.end()
#define CHMIN(x, y) x = min(x, y)
#define CHMAX(x, y) x = max(x, y)
#define YES(b) cout << ((b) ? "YES" : "NO") << endl
#define Yes(b) cout << ((b) ? "Yes" : "No") << endl

const int MAX = 1e6 + 10;
const ll INF = 1e18;

const int DX[4] = {-1, 0, 1, 0};
const int DY[4] = {0, -1, 0, 1};

int H, W, K;
int X1, Y1, X2, Y2;
vector<vector<bool>> C;

ll solve() {
  vvi dist(H, vi(W, INF));
  priority_queue<iii> que;
  que.push(iii(0, X1, Y1));

  while (!que.empty()) {
    ll c, x, y;
    tie(c, x, y) = que.top();
    que.pop();
    c *= -1;
    if (dist[x][y] != INF)
      continue;
    dist[x][y] = c;

    REP(d, 4) {
      FOR(k, 1, K + 1) {
        int nx = x + DX[d] * k, ny = y + DY[d] * k;
        if (nx < 0 || H <= nx || ny < 0 || W <= ny || !C[nx][ny] ||
            dist[nx][ny] <= dist[x][y])
          break;
        que.push(iii(-(c + 1), nx, ny));
      }
    }
  }

  if (dist[X2][Y2] == INF)
    return -1;
  return dist[X2][Y2];
}

int main() {
  cout << fixed << setprecision(15);
  cin >> H >> W >> K >> X1 >> Y1 >> X2 >> Y2;
  X1--;
  Y1--;
  X2--;
  Y2--;
  REP(h, H) {
    string s;
    cin >> s;
    C.push_back(vector<bool>(W));
    REP(w, W) if (s[w] == '.') C[h][w] = true;
  }
  cout << solve() << endl;
}