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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
using PLL = pair<ll, ll>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repn(i, n) for (int i = 0; i <= (int)(n); i++)
#define srep(i, l, n) for (int i = l; i < (int)(n); i++)
#define srepn(i, l, n) for (int i = l; i <= (int)(n); i++)
#define drep(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define drepn(i, n) for (int i = (int)(n); i >= 0; i--)
#define size(s) (int)s.size()
#define debug(var)                                                             \\
  do {                                                                         \\
    std::cout << #var << " : ";                                                \\
    view(var);                                                                 \\
  } while (0)
template <typename T> void view(T e) { std::cout << e << std::endl; }
template <typename T> void view(const std::vector<T> &v) {
  for (const auto &e : v) {
    std::cout << e << " ";
  }
  std::cout << std::endl;
}
template <typename T> void view(const std::vector<std::vector<T>> &vv) {
  for (const auto &v : vv) {
    view(v);
  }
}
template <typename T> inline istream &operator>>(istream &i, vector<T> &v) {
  rep(j, size(v)) i >> v[j];
  return i;
}
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) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template <class T = int> using V = vector<T>;
template <class T = int> using VV = vector<V<T>>;
bool isIn(int i, int j, int h, int w) {
  return i >= 0 && i < h && j >= 0 && j < w;
}
void Yes() { cout << "Yes" << endl; }
void No() { cout << "No" << endl; }
void YES() { cout << "YES" << endl; }
void NO() { cout << "NO" << endl; }
void err() { cout << -1 << endl; }
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define pb push_back
#define ep emplace_back

const int MOD = 1000000007;
const int INF = 1e9;

#define PI acos(-1);
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int ddx[8] = {1, 1, 1, -1, -1, -1, 0, 0};
int ddy[8] = {0, 1, -1, 0, 1, -1, 1, -1};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int h, w, k;
  int x1, y1, x2, y2;
  cin >> h >> w >> k;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--, y1--, x2--, y2--;
  VV<char> grid(h, V<char>(w));
  rep(i, h) rep(j, w) cin >> grid[i][j];
  VV<> dis(h, V<>(w, INF));
  dis[x1][y1] = 0;
  queue<P> q;
  q.push(P(x1, y1));
  while (!q.empty()) {
    P p = q.front();
    q.pop();

    int x = p.first;
    int y = p.second;

    rep(i, 4) srepn(j, 1, k) {
      int nx = x + dx[i] * j;
      int ny = y + dy[i] * j;
      if (!isIn(nx, ny, h, w))
        break;
      if (isIn(nx, ny, h, w)) {
        if (grid[nx][ny] == '@')
          break;
        else if (dis[x][y] >= dis[nx][ny])
          break;
        else if (dis[nx][ny] == INF) {
          dis[nx][ny] = dis[x][y] + 1;
          q.push(P(nx, ny));
        }
      }
    }
  }
  cout << (dis[x2][y2] == INF ? -1 : dis[x2][y2]) << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
using PLL = pair<ll, ll>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repn(i, n) for (int i = 0; i <= (int)(n); i++)
#define srep(i, l, n) for (int i = l; i < (int)(n); i++)
#define srepn(i, l, n) for (int i = l; i <= (int)(n); i++)
#define drep(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define drepn(i, n) for (int i = (int)(n); i >= 0; i--)
#define size(s) (int)s.size()
#define debug(var)                                                             \\
  do {                                                                         \\
    std::cout << #var << " : ";                                                \\
    view(var);                                                                 \\
  } while (0)
template <typename T> void view(T e) { std::cout << e << std::endl; }
template <typename T> void view(const std::vector<T> &v) {
  for (const auto &e : v) {
    std::cout << e << " ";
  }
  std::cout << std::endl;
}
template <typename T> void view(const std::vector<std::vector<T>> &vv) {
  for (const auto &v : vv) {
    view(v);
  }
}
template <typename T> inline istream &operator>>(istream &i, vector<T> &v) {
  rep(j, size(v)) i >> v[j];
  return i;
}
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) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template <class T = int> using V = vector<T>;
template <class T = int> using VV = vector<V<T>>;
bool isIn(int i, int j, int h, int w) {
  return i >= 0 && i < h && j >= 0 && j < w;
}
void Yes() { cout << "Yes" << endl; }
void No() { cout << "No" << endl; }
void YES() { cout << "YES" << endl; }
void NO() { cout << "NO" << endl; }
void err() { cout << -1 << endl; }
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define pb push_back
#define ep emplace_back

const int MOD = 1000000007;
const int INF = 1e9;

#define PI acos(-1);
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int ddx[8] = {1, 1, 1, -1, -1, -1, 0, 0};
int ddy[8] = {0, 1, -1, 0, 1, -1, 1, -1};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int h, w, k;
  int x1, y1, x2, y2;
  cin >> h >> w >> k;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--, y1--, x2--, y2--;
  VV<char> grid(h, V<char>(w));
  rep(i, h) rep(j, w) cin >> grid[i][j];
  VV<> dis(h, V<>(w, INF));
  dis[x1][y1] = 0;
  queue<P> q;
  q.push(P(x1, y1));
  while (!q.empty()) {
    P p = q.front();
    q.pop();

    int x = p.first;
    int y = p.second;

    rep(i, 4) srepn(j, 1, k) {
      int nx = x + dx[i] * j;
      int ny = y + dy[i] * j;
      if (!isIn(nx, ny, h, w))
        break;
      if (isIn(nx, ny, h, w)) {
        if (grid[nx][ny] == '@')
          break;
        else if (dis[x][y] + 1 > dis[nx][ny])
          break;
        else {
          dis[nx][ny] = dis[x][y] + 1;
          q.push(P(nx, ny));
        }
      }
    }
  }
  cout << (dis[x2][y2] == INF ? -1 : dis[x2][y2]) << endl;
}