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

#include "bits/stdc++.h"
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
typedef pair<ll, int> LP;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const ll INF = 1e18;
const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, -1, 0, 1};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int h, w, k;
  cin >> h >> w >> k;
  int si, sj, ti, tj;
  cin >> si >> sj >> ti >> tj;
  --si;
  --sj;
  --ti;
  --tj;
  vector<string> g(h);
  rep(i, h) cin >> g[i];

  auto toId = [&](int i, int j, int v) { return (i * w + j) * 4 + v; };
  auto modCeil = [&](ll x) { return (x + k - 1) / k * k; };

  vector<ll> dist(h * w * 4, INF);
  priority_queue<LP, vector<LP>, greater<LP>> q;
  auto push = [&](int i, int j, int v, ll x) {
    int id = toId(i, j, v);
    if (dist[id] <= x)
      return;
    dist[id] = x;
    q.emplace(x, id);
  };
  rep(v, 4) push(si, sj, v, 0);
  while (!q.empty()) {
    ll d = q.top().first;
    int id = q.top().second;
    q.pop();
    if (dist[id] != d)
      continue;
    int i = id / w / 4;
    int j = id / 4 % w;
    int v = id % 4;
    rep(nv, 4) push(i, j, nv, modCeil(d));
    {
      int ni = i + di[v], nj = j + dj[v];
      if (0 <= ni && ni < h && 0 <= nj && nj < w) {
        if (g[ni][nj] == '.') {
          push(ni, nj, v, d + 1);
        }
      }
    }
  }
  ll ans = INF;
  rep(v, 4) chmin(ans, dist[toId(ti, tj, v)]);
  if (ans == INF) {
    cout << -1 << endl;
    return 0;
  }
  ans = modCeil(ans) / k;
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const int INF = 1e9;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int h, w, k;
  cin >> h >> w >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  --x1;
  --y1;
  --x2;
  --y2;
  vector<string> s(h);
  vector<vector<int>> dp(h, vector<int>(w, INF));
  rep(i, h) cin >> s[i];
  queue<P> que;
  que.push(P(x1, y1));
  dp[x1][y1] = 0;
  while (!que.empty()) {
    int x = que.front().first;
    int y = que.front().second;
    que.pop();
    // cout << x << " " << y << endl;
    if (x == x2 && y == y2)
      break;
    for (int dx = 1; dx <= k; ++dx) {
      int nx = x + dx;
      int ny = y;
      if (h <= nx)
        break;
      if (s[nx][ny] == '@')
        break;
      if (dp[nx][ny] <= dp[x][y] + 1)
        continue;
      dp[nx][ny] = dp[x][y] + 1;
      que.push(P(nx, ny));
    }
    for (int dx = 1; dx <= k; ++dx) {
      int nx = x - dx;
      int ny = y;
      if (nx < 0)
        break;
      if (s[nx][ny] == '@')
        break;
      if (dp[nx][ny] <= dp[x][y] + 1)
        continue;
      dp[nx][ny] = dp[x][y] + 1;
      que.push(P(nx, ny));
    }
    for (int dy = 1; dy <= k; ++dy) {
      int nx = x;
      int ny = y + dy;
      if (w <= ny)
        break;
      if (s[nx][ny] == '@')
        break;
      if (dp[nx][ny] <= dp[x][y] + 1)
        continue;
      dp[nx][ny] = dp[x][y] + 1;
      que.push(P(nx, ny));
    }
    for (int dy = 1; dy <= k; ++dy) {
      int nx = x;
      int ny = y - dy;
      if (ny < 0)
        break;
      if (s[nx][ny] == '@')
        break;
      if (dp[nx][ny] <= dp[x][y] + 1)
        continue;
      dp[nx][ny] = dp[x][y] + 1;
      que.push(P(nx, ny));
    }
  }
  if (dp[x2][y2] == INF)
    cout << -1 << endl;
  else
    cout << dp[x2][y2] << endl;
  /*
  rep(i,h){
      rep(j,w) cout << dp[i][j] << " ";
      cout << endl;
  }
  */
  return 0;
}