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

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

#define int long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, n) for (int i = 1; i <= (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define uniq(x) (x).erase(unique(all(x)), (x).end()))
#define bit(n) (1LL << (n))
#define cdiv(a, b) (((a)-1) / (b) + 1)
#define dump(x) cerr << #x " = " << (x) << endl
using vint = vector<int>;
using vvint = vector<vint>;
using pint = pair<int, int>;
using vpint = vector<pint>;
template <typename T>
using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>;
constexpr double PI = 3.1415926535897932384626433832795028;
constexpr int DY[8] = {0, 1, 0, -1, 1, 1, -1, -1};
constexpr int DX[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int gcd(int a, int b) {
  while (b) {
    swap(a %= b, b);
  }
  return a;
}
int lcm(int a, int b) { return a / gcd(a, b) * b; }
template <typename T> void fin(T mes) {
  cout << mes << endl;
  exit(0);
}
template <typename T, typename U> bool chmax(T &a, const U &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T, typename U> bool chmin(T &a, const U &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &rhs) {
  os << "(" << rhs.first << ", " << rhs.second << ")";
  return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &rhs) {
  os << "{";
  for (auto itr = rhs.begin(); itr != rhs.end(); itr++) {
    os << *itr << (next(itr) != rhs.end() ? ", " : "");
  }
  os << "}";
  return os;
}
struct setup {
  static constexpr int PREC = 20;
  setup() {
    cout << fixed << setprecision(PREC);
    cerr << fixed << setprecision(PREC);
  };
} setup;

int H, W, K;
string c[1100000];
vvint dp;
signed main() {
  int x1, y1, x2, y2;
  cin >> H >> W >> K;
  cin >> y1 >> x1 >> y2 >> x2;
  x1--, y1--, x2--, y2--;
  rep(i, H) { cin >> c[i]; }
  dp.resize(H);
  fill(all(dp), vint(W, LLONG_MAX));
  dp[y1][x1] = 0;
  queue<pint> q;
  q.push({y1, x1});
  while (!q.empty()) {
    pint cur = q.front();
    int yc = cur.first, xc = cur.second;
    q.pop();
    rep(i, 4) {
      reps(j, K) {
        int yn = yc + DY[i] * j, xn = xc + DX[i] * j;
        if (yn < 0 || yn >= H || xn < 0 || xn >= W || c[yn][xn] == '@' ||
            dp[yn][xn] <= dp[yc][xc]) {
          break;
        }
        if (dp[yn][xn] == LLONG_MAX) {
          q.push({yn, xn});
        }
        dp[yn][xn] = dp[yc][xc] + 1;
      }
    }
  }
  if (dp[y2][x2] != LLONG_MAX) {
    cout << dp[y2][x2] << endl;
  } else {
    cout << -1 << endl;
  }
}


Error: TLE

Fixed submission:

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

#define int long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, n) for (int i = 1; i <= (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define uniq(x) (x).erase(unique(all(x)), (x).end()))
#define bit(n) (1LL << (n))
#define cdiv(a, b) (((a)-1) / (b) + 1)
#define dump(x) cerr << #x " = " << (x) << endl
using vint = vector<int>;
using vvint = vector<vint>;
using pint = pair<int, int>;
using vpint = vector<pint>;
template <typename T>
using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>;
constexpr double PI = 3.1415926535897932384626433832795028;
constexpr int DY[8] = {0, 1, 0, -1, 1, 1, -1, -1};
constexpr int DX[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int gcd(int a, int b) {
  while (b) {
    swap(a %= b, b);
  }
  return a;
}
int lcm(int a, int b) { return a / gcd(a, b) * b; }
template <typename T> void fin(T mes) {
  cout << mes << endl;
  exit(0);
}
template <typename T, typename U> bool chmax(T &a, const U &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T, typename U> bool chmin(T &a, const U &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &rhs) {
  os << "(" << rhs.first << ", " << rhs.second << ")";
  return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &rhs) {
  os << "{";
  for (auto itr = rhs.begin(); itr != rhs.end(); itr++) {
    os << *itr << (next(itr) != rhs.end() ? ", " : "");
  }
  os << "}";
  return os;
}
struct setup {
  static constexpr int PREC = 20;
  setup() {
    cout << fixed << setprecision(PREC);
    cerr << fixed << setprecision(PREC);
  };
} setup;

int H, W, K;
string c[1100000];
vvint dp;
signed main() {
  int x1, y1, x2, y2;
  cin >> H >> W >> K;
  cin >> y1 >> x1 >> y2 >> x2;
  x1--, y1--, x2--, y2--;
  rep(i, H) { cin >> c[i]; }
  dp.resize(H);
  fill(all(dp), vint(W, LLONG_MAX));
  dp[y1][x1] = 0;
  queue<pint> q;
  q.push({y1, x1});
  while (!q.empty()) {
    pint cur = q.front();
    int yc = cur.first, xc = cur.second;
    q.pop();
    rep(i, 4) {
      reps(j, K) {
        int yn = yc + DY[i] * j, xn = xc + DX[i] * j;
        if (yn < 0 || yn >= H || xn < 0 || xn >= W || c[yn][xn] == '@' ||
            dp[yn][xn] <= dp[yc][xc]) {
          break;
        }
        dp[yn][xn] = dp[yc][xc] + 1;
        q.push({yn, xn});
      }
    }
  }
  if (dp[y2][x2] != LLONG_MAX) {
    cout << dp[y2][x2] << endl;
  } else {
    cout << -1 << endl;
  }
}