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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, srt, end) for (long long i = (srt); i < (long long)(end); i++)
#define VL vector<ll>
#define VS vector<string>
#define VB vector<bool>
#define VP vector<pair<ll, ll>>
#define VVL vector<vector<ll>>
#define VVP vector<vector<pair<ll, ll>>>
#define PL pair<ll, ll>
#define ALL(v) (v).begin(), (v).end()
ll d1[4] = {1, -1, 0, 0};
ll d2[4] = {0, 0, 1, -1};

int main() {

  // input
  ll h, w, k;
  cin >> h >> w >> k;
  ll x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  VS s(h);
  rep(i, 0, h) cin >> s[i];

  const ll inf = 1e18;
  VVL d(h, VL(w, inf));

  queue<PL> que;
  que.push({x1, y1});
  d[x1][y1] = 0;
  while (!que.empty()) {
    auto q = que.front();
    que.pop();
    rep(i, 0, 4) {
      rep(j, 1, k + 1) {
        ll nx = q.first + j * d1[i];
        ll ny = q.second + j * d2[i];
        if (nx < 0 || nx >= h)
          break;
        if (ny < 0 || ny >= w)
          break;
        if (s[nx][ny] == '@')
          break;
        if (d[nx][ny] <= d[q.first][q.second])
          break;
        if (d[nx][ny] == inf) {
          d[nx][ny] = d[q.first][q.second] + 1;
          que.push({nx, ny});
        }
      }
    }
  }

  ll ans = d[x2][y2];
  cout << (ans == inf ? -1 : ans) << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, srt, end) for (long long i = (srt); i < (long long)(end); i++)
#define VL vector<ll>
#define VS vector<string>
#define VB vector<bool>
#define VP vector<pair<ll, ll>>
#define VVL vector<vector<ll>>
#define VVP vector<vector<pair<ll, ll>>>
#define PL pair<ll, ll>
#define ALL(v) (v).begin(), (v).end()
ll d1[4] = {1, -1, 0, 0};
ll d2[4] = {0, 0, 1, -1};

ll H, W, K;

ll ch(PL p) { return W * p.second + p.first; }

int main() {
  cin >> H >> W >> K;
  ll x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  PL srt = {y1, x1};
  vector<ll> d(H * W + 1, 1e18);
  VS c(H);
  rep(i, 0, H) { cin >> c[i]; }

  bool f = true;
  rep(i, 0, H) {
    rep(j, 0, W) {
      if (c[i][j] == '@')
        f = false;
    }
  }
  if (f) {
    if (y1 == y2)
      cout << (abs(x1 - x2) + K - 1) / K << endl;
    else if (x1 == x2)
      cout << (abs(y1 - y2) + K - 1) / K << endl;
    else
      cout << (abs(x1 - x2) + K - 1) / K + (abs(y1 - y2) + K - 1) / K << endl;
    return 0;
  }
  /* dijkstra's scope */ {
    priority_queue<pair<ll, PL>, vector<pair<ll, PL>>, greater<pair<ll, PL>>>
        que;
    que.push({0, srt});
    d[ch(srt)] = 0;
    while (!que.empty()) {
      auto q = que.top();
      // cerr << q.first << " " << q.second.first << " " << q.second.second <<
      // endl;
      que.pop();
      if (q.first > d[ch(q.second)])
        continue;
      rep(i, 0, 4) {
        ll nx = q.second.first + d1[i];
        ll ny = q.second.second + d2[i];
        // cerr << "n: " << nx << " " << ny << endl;
        ll itr = 1;
        while (itr <= K) {
          if (nx < 0 || ny < 0 || nx >= W || ny >= H)
            break;
          if (c[ny][nx] == '@')
            break;
          if (d[ch({nx, ny})] <= q.first + 1) {
            ;
          } else {
            d[ch({nx, ny})] = q.first + 1;
            que.push({d[ch({nx, ny})], {nx, ny}});
          }
          nx += d1[i];
          ny += d2[i];
          itr++;
        }
      }
    }
  }

  if (d[ch({y2, x2})] == 1e18)
    cout << -1 << "\\n";
  else
    cout << d[ch({y2, x2})] << "\\n";

  /*
  cerr << "----------" << endl;
  rep(y, 0, H){
      rep(x, 0, W) cerr << d[ch({x, y})] << " ";
      cerr << endl;
  }
  */
  return 0;
}