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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define eprintf(...) 42
#endif

#define rep_(i, a_, b_, a, b, ...)                                             \\
  for (int i = (a), i##_len = (b); i < i##_len; ++i)
#define rep(i, ...) rep_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define reprev_(i, a_, b_, a, b, ...)                                          \\
  for (int i = (b - 1), i##_min = (a); i >= i##_min; --i)
#define reprev(i, ...) reprev_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define all(x) (x).begin(), (x).end()
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
// template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<int, P> PP;
typedef long double ld;

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int h, w, k;
  int INF = 1e8;
  cin >> h >> w >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  vector<string> s(h);
  rep(i, h) cin >> s[i];
  vector<vector<P>> d(h, vector<P>(w, P{INF, -1}));

  x1--;
  y1--;
  x2--;
  y2--;
  d[x1][y1].first = 0;
  queue<PP> que;
  que.emplace(0, P{x1, y1});
  while (!que.empty()) {
    auto [dist, p] = que.front();
    que.pop();
    auto [x, y] = p;
    eprintf("(%d,%d)\\n", x, y);
    if (dist > d[x][y].first)
      continue;
    // int count;
    // count = 0;
    for (int i = 1; i <= k; i++) {
      int xx = x + i;
      if (xx >= h)
        break;
      if (d[xx][y].first <= dist || s[xx][y] == '@')
        break;
      // if (d[xx][y].first == dist + 1) count++;
      // if (count == 2) break;
      if (d[xx][y].second == 0)
        break;
      d[xx][y] = P{dist + 1, 0};
      que.emplace(d[xx][y].first, P{xx, y});
    }
    for (int i = 1; i <= k; i++) {
      int xx = x - i;
      if (xx < 0)
        break;
      if (d[xx][y].first <= dist || s[xx][y] == '@')
        break;
      // if (d[xx][y].first == dist + 1) count++;
      // if (count == 2) break;
      if (d[xx][y].second == 0)
        break;
      d[xx][y] = P{dist + 1, 0};
      que.emplace(d[xx][y].first, P{xx, y});
    }
    for (int i = 1; i <= k; i++) {
      int yy = y - i;
      if (yy < 0)
        break;
      if (d[x][yy].first <= dist || s[x][yy] == '@')
        break;
      // if (d[x][yy].first == dist + 1) count++;
      if (d[x][yy].second == 1)
        break;
      d[x][yy] = P{dist + 1, 1};
      que.emplace(d[x][yy].first, P{x, yy});
    }
    for (int i = 1; i <= k; i++) {
      int yy = y + i;
      if (yy >= w)
        break;
      if (d[x][yy].first <= dist || s[x][yy] == '@')
        break;
      // if (d[x][yy].first == dist + 1) count++;
      if (d[x][yy].second == 1)
        break;
      d[x][yy] = P{dist + 1, 1};
      que.emplace(d[x][yy].first, P{x, yy});
    }
  }

  // rep (i, h) {
  //     rep (j, w) {
  //         if (d[i][j] >= INF) eprintf("- ");
  //         else eprintf("%d ", d[i][j]);
  //     }
  //     eprintf("\\n");
  // }
  if (d[x2][y2].first >= INF)
    cout << -1 << "\\n";
  else
    cout << d[x2][y2].first << "\\n";
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define eprintf(...) 42
#endif

#define rep_(i, a_, b_, a, b, ...)                                             \\
  for (int i = (a), i##_len = (b); i < i##_len; ++i)
#define rep(i, ...) rep_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define reprev_(i, a_, b_, a, b, ...)                                          \\
  for (int i = (b - 1), i##_min = (a); i >= i##_min; --i)
#define reprev(i, ...) reprev_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define all(x) (x).begin(), (x).end()
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
// template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<int, P> PP;
typedef long double ld;

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int h, w, k;
  int INF = 1e8;
  cin >> h >> w >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  vector<string> s(h);
  rep(i, h) cin >> s[i];
  vector<vector<int>> d(h, vector<int>(w, INF));

  x1--;
  y1--;
  x2--;
  y2--;
  d[x1][y1] = 0;
  // queue<PP> que;
  priority_queue<PP, vector<PP>, greater<PP>> que;
  que.emplace(0, P{x1, y1});
  while (!que.empty()) {
    auto [dist, p] = que.top();
    que.pop();
    auto [x, y] = p;
    eprintf("(%d,%d)\\n", x, y);
    if (dist > d[x][y])
      continue;
    for (int i = 1; i <= k; i++) {
      int xx = x + i;
      if (xx >= h)
        break;
      if (d[xx][y] <= dist || s[xx][y] == '@')
        break;
      d[xx][y] = dist + 1;
      que.emplace(d[xx][y], P{xx, y});
    }
    for (int i = 1; i <= k; i++) {
      int xx = x - i;
      if (xx < 0)
        break;
      if (d[xx][y] <= dist || s[xx][y] == '@')
        break;
      d[xx][y] = dist + 1;
      que.emplace(d[xx][y], P{xx, y});
    }
    for (int i = 1; i <= k; i++) {
      int yy = y - i;
      if (yy < 0)
        break;
      if (d[x][yy] <= dist || s[x][yy] == '@')
        break;
      d[x][yy] = dist + 1;
      que.emplace(d[x][yy], P{x, yy});
    }
    for (int i = 1; i <= k; i++) {
      int yy = y + i;
      if (yy >= w)
        break;
      if (d[x][yy] <= dist || s[x][yy] == '@')
        break;
      d[x][yy] = dist + 1;
      que.emplace(d[x][yy], P{x, yy});
    }
  }

  rep(i, h) {
    rep(j, w) {
      if (d[i][j] >= INF)
        eprintf("- ");
      else
        eprintf("%d ", d[i][j]);
    }
    eprintf("\\n");
  }
  if (d[x2][y2] >= INF)
    cout << -1 << "\\n";
  else
    cout << d[x2][y2] << "\\n";
  return 0;
}