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

#include <bits/stdc++.h>
#define LOCAL
using namespace std;
template <typename A, typename B>
ostream &operator<<(ostream &out, const pair<A, B> &a) {
  out << "(" << a.first << "," << a.second << ")";
  return out;
}
template <typename T, size_t N>
ostream &operator<<(ostream &out, const array<T, N> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T> ostream &operator<<(ostream &out, const vector<T> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T, class Cmp>
ostream &operator<<(ostream &out, const set<T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "}";
  return out;
}
template <typename U, typename T, class Cmp>
ostream &operator<<(ostream &out, const map<U, T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &p : a) {
    out << (first ? "" : ", ");
    out << p.first << ":" << p.second;
    first = 0;
  }
  out << "}";
  return out;
}
#ifdef LOCAL
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 42
#endif
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
#define int long long
using P = pair<int, int>;
// #########################################
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
signed main() {
  int h, w, k;
  cin >> h >> w >> k;
  int sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;
  sx--;
  sy--;
  gx--;
  gy--;
  vector<string> f(h);
  rep(i, h) cin >> f[i];
  vector<vector<int>> dist(h, vector<int>(w, -1));
  queue<P> q;
  dist[sx][sy] = 0;
  q.emplace(sx, sy);
  while (q.size()) {
    auto now = q.front();
    q.pop();
    int x = now.first, y = now.second;
    rep(i, 4) {
      for (int j = 1; j <= k; j++) {
        int nx = x + dx[i] * j, ny = y + dy[i] * j;
        if (nx < 0 || ny < 0 || nx > h - 1 || ny > w - 1)
          break;
        if (f[nx][ny] == '@')
          break;
        if (dist[nx][ny] != -1 && dist[nx][ny] != dist[x][y] + 1)
          break;
        if (dist[nx][ny] != -1)
          continue;
        dist[nx][ny] = dist[x][y] + 1;
        q.emplace(nx, ny);
      }
    }
  }
  cout << dist[gx][gy] << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define LOCAL
using namespace std;
template <typename A, typename B>
ostream &operator<<(ostream &out, const pair<A, B> &a) {
  out << "(" << a.first << "," << a.second << ")";
  return out;
}
template <typename T, size_t N>
ostream &operator<<(ostream &out, const array<T, N> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T> ostream &operator<<(ostream &out, const vector<T> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T, class Cmp>
ostream &operator<<(ostream &out, const set<T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "}";
  return out;
}
template <typename U, typename T, class Cmp>
ostream &operator<<(ostream &out, const map<U, T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &p : a) {
    out << (first ? "" : ", ");
    out << p.first << ":" << p.second;
    first = 0;
  }
  out << "}";
  return out;
}
#ifdef LOCAL
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 42
#endif
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
#define int long long
using P = pair<int, int>;
// #########################################
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
signed main() {
  int h, w, k;
  cin >> h >> w >> k;
  int sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;
  sx--;
  sy--;
  gx--;
  gy--;
  vector<string> f(h);
  rep(i, h) cin >> f[i];
  vector<vector<int>> dist(h, vector<int>(w, -1));
  queue<P> q;
  dist[sx][sy] = 0;
  q.emplace(sx, sy);
  while (q.size()) {
    auto now = q.front();
    q.pop();
    int x = now.first, y = now.second;
    if (x == gx && y == gy) {
      cout << dist[gx][gy] << endl;
      return 0;
    }
    rep(i, 4) {
      for (int j = 1; j <= k; j++) {
        int nx = x + dx[i] * j, ny = y + dy[i] * j;
        if (nx < 0 || ny < 0 || nx > h - 1 || ny > w - 1)
          break;
        if (f[nx][ny] == '@')
          break;
        if (dist[nx][ny] != -1 && dist[nx][ny] != dist[x][y] + 1)
          break;
        dist[nx][ny] = dist[x][y] + 1;
        q.emplace(nx, ny);
      }
    }
  }
  cout << -1 << endl;
  return 0;
}