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

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) FOR(i, 0, n)
#define ROF(i, a, b) for (int i = a; i >= b; i--)
#define per(i, a) ROF(i, a, 0)
#define pb push_back
using namespace std;
using ll = long long;
using ld = long double;
using ch = char;
typedef pair<ll, ll> P;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<P> vP;
typedef vector<ch> vc;
typedef vector<vc> vvc;
const ll MOD = 1000000007;
const ll MOD2 = 998244353;
const ld PI = acos(-1);
const ll INF = 1e18;
struct edge {
  ll to, cost;
};
struct edge2 {
  ll from, to, cost;
};

template <typename T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <typename T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int H, W, K;
  cin >> H >> W >> K;
  int x0, y0, x1, y1;
  cin >> x0 >> y0 >> x1 >> y1;
  x0--;
  y0--;
  x1--;
  y1--;
  vvc c(H, vc(W));
  rep(i, H) {
    rep(j, W) { cin >> c[i][j]; }
  }
  vvl dist(H, vl(W, INF));
  int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
  dist[x0][y0] = 0;
  queue<P> q;
  q.push({x0, y0});
  while (!q.empty()) {
    P now = q.front();
    q.pop();
    int x = now.first, y = now.second;
    rep(i, 4) {
      int nx = x, ny = y;
      ll now_d = dist[x][y] + 1;
      rep(t, K) {
        nx += dx[i], ny += dy[i];
        if ((min(nx, ny) < 0 || max(nx - H + 1, ny - W + 1) > 0 ||
             (dist[nx][ny] != -1 && now_d > dist[nx][ny]) || c[nx][ny] == '@'))
          break;
        if (now_d < dist[nx][ny]) {
          dist[nx][ny] = now_d;
          q.push({nx, ny});
        }
      }
    }
  }
  cout << (dist[x1][y1] >= INF ? -1 : dist[x1][y1]) << endl;
}
/*
overflow checked?
corner case checked?
boundary checked?
not TLE in worst case checked?
*/

Error: TLE

Fixed submission:

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) FOR(i, 0, n)
#define ROF(i, a, b) for (int i = a; i >= b; i--)
#define per(i, a) ROF(i, a, 0)
#define pb push_back
using namespace std;
using ll = long long;
using ld = long double;
using ch = char;
typedef pair<ll, ll> P;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<P> vP;
typedef vector<ch> vc;
typedef vector<vc> vvc;
const ll MOD = 1000000007;
const ll MOD2 = 998244353;
const ld PI = acos(-1);
const ll INF = 1e18;
struct edge {
  ll to, cost;
};
struct edge2 {
  ll from, to, cost;
};

template <typename T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <typename T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

vector<vvl> d;
int H, W, K;
vvc c;
int x_1, x_2, y_1, y_2;

void dijkstra(int x, int y) {
  priority_queue<pair<P, P>, vector<pair<P, P>>, greater<pair<P, P>>> pq;
  rep(i, 4) { d[x][y][i] = 0; }
  rep(i, 4) { pq.push({{0, i}, {x, y}}); } // cost 無機 座標
  int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
  while (pq.size()) {
    pair<P, P> p = pq.top();
    pq.pop();
    if (p.second.first == x_2 && p.second.second == y_2) {
      break;
    }
    P p0 = p.first, p1 = p.second;
    rep(i, 4) {
      ll nx = p1.first + dx[i], ny = p1.second + dy[i];
      ll nd = INF;
      if (i == p0.second) {
        nd = d[p1.first][p1.second][i] + 1;
      } else {
        nd = d[p1.first][p1.second][p0.second] / K * K + K;
      }
      if (0 <= nx && 0 <= ny && nx <= H - 1 && ny <= W - 1 &&
          nd <= d[nx][ny][i] && c[nx][ny] == '.') {
        d[nx][ny][i] = nd;
        pq.push({{nd, i}, {nx, ny}});
      }
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> H >> W >> K;
  cin >> x_1 >> y_1 >> x_2 >> y_2;
  x_1--;
  x_2--;
  y_1--;
  y_2--;
  c.assign(H, vc(W));
  rep(i, H) {
    rep(j, W) { cin >> c[i][j]; }
  }
  d.assign(H, vvl(W, vl(4, INF)));
  dijkstra(x_1, y_1);
  ll ans = INF;
  rep(i, 4) { chmin(ans, (d[x_2][y_2][i] - 1) / K + 1); }
  // rep(i,4)
  // cout << d[2][0][i] << endl;
  if (ans >= 1e12)
    cout << -1 << endl;
  else
    cout << ans << endl;
}
/*
overflow checked?
corner case checked?
boundary checked?
not TLE in worst case checked?
*/