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

#include <bits/stdc++.h>
using namespace std;
using pint = pair<int, int>;
using ll = long long;
using ull = unsigned long long;
using vll = vector<long long>;
using pll = pair<ll, ll>;
#define FOR(i, begin, end)                                                     \\
  for (int i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end)                                                    \\
  for (int i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
#define VREP(s, ite) for (auto ite = s.begin(); ite != s.end(); ++ite)
#define FI first
#define SE second
#define ALL(v) v.begin(), v.end()
// #define endl "\\n"
#define ciosup                                                                 \\
  cin.tie(0);                                                                  \\
  ios::sync_with_stdio(false);
#define eb emplace_back
#define vint vector<int>
constexpr ll INF = 1e15 + 7LL;
constexpr ll MOD = 1e9 + 7LL;
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (int i = 0; i < v.size(); ++i) {
    is >> v[i];
  }
  return is;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (int i = 0; i < v.size() - 1; ++i) {
    os << v[i] << " ";
  }
  if (v.size() > 0) {
    os << v[v.size() - 1] << endl;
  }
  return os;
}

int main() {
  ll h, w, k;
  cin >> h >> w >> k;
  pint st, goal;
  cin >> st.FI >> st.SE >> goal.FI >> goal.SE;
  st.FI--;
  st.SE--;
  goal.FI--;
  goal.SE--;
  vector<vector<char>> G(h, vector<char>(w));
  REP(i, h) {
    REP(j, w) { cin >> G[i][j]; }
  }

  vector<vector<int>> dist(h, vector<int>(w, INT_MAX));
  queue<pint> que;
  que.push(st);
  dist[st.FI][st.SE] = 0;

  vint dx{1, 0, -1, 0}, dy{0, 1, 0, -1};
  while (que.size()) {
    pint now = que.front();
    que.pop();
    for (int i = 0; i < 4; ++i) {
      pint next{now.FI, now.SE};
      REP(j, k) {
        next.FI += dx[i];
        next.SE += dy[i];
        if (0 <= next.FI && next.FI <= h - 1 && 0 <= next.SE &&
            next.SE <= w - 1) {
          if (G[next.FI][next.SE] == '.') {
            if (dist[next.FI][next.SE] == INT_MAX) {
              // cout << now.FI << " " << now.SE << endl;
              // cout << next.FI << " " << next.SE << " "<< dist[now.FI][now.SE]
              // + 1 << endl;
              dist[next.FI][next.SE] = dist[now.FI][now.SE] + 1;
              que.push(next);
            } else if (dist[next.FI][next.SE] <= dist[now.FI][now.SE]) {
              break;
            }
          } else {
            break;
          }
        } else {
          break;
        }
      }
    }
  }
  if (dist[goal.FI][goal.SE] == INT_MAX) {
    cout << -1 << endl;
    return 0;
  }
  cout << dist[goal.FI][goal.SE] << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using pint = pair<int, int>;
using ll = long long;
using ull = unsigned long long;
using vll = vector<long long>;
using pll = pair<ll, ll>;
#define FOR(i, begin, end)                                                     \\
  for (int i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end)                                                    \\
  for (int i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
#define VREP(s, ite) for (auto ite = s.begin(); ite != s.end(); ++ite)
#define FI first
#define SE second
#define ALL(v) v.begin(), v.end()
// #define endl "\\n"
#define ciosup                                                                 \\
  cin.tie(0);                                                                  \\
  ios::sync_with_stdio(false);
#define eb emplace_back
#define vint vector<int>
constexpr ll INF = 1e15 + 7LL;
constexpr ll MOD = 1e9 + 7LL;
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (int i = 0; i < v.size(); ++i) {
    is >> v[i];
  }
  return is;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (int i = 0; i < v.size() - 1; ++i) {
    os << v[i] << " ";
  }
  if (v.size() > 0) {
    os << v[v.size() - 1] << endl;
  }
  return os;
}

int main() {
  ll h, w, k;
  cin >> h >> w >> k;
  pint st, goal;
  cin >> st.FI >> st.SE >> goal.FI >> goal.SE;
  st.FI--;
  st.SE--;
  goal.FI--;
  goal.SE--;
  vector<vector<char>> G(h, vector<char>(w));
  REP(i, h) {
    REP(j, w) { cin >> G[i][j]; }
  }

  vector<vector<int>> dist(h, vector<int>(w, INT_MAX));
  queue<pint> que;
  que.push(st);
  dist[st.FI][st.SE] = 0;

  vint dx{1, 0, -1, 0}, dy{0, 1, 0, -1};
  while (que.size()) {
    pint now = que.front();
    que.pop();
    for (int i = 0; i < 4; ++i) {
      pint next{now.FI, now.SE};
      REP(j, k) {
        next.FI += dx[i];
        next.SE += dy[i];
        if (0 <= next.FI && next.FI <= h - 1 && 0 <= next.SE &&
            next.SE <= w - 1) {
          if (G[next.FI][next.SE] == '.') {
            if (dist[next.FI][next.SE] == INT_MAX) {
              dist[next.FI][next.SE] = dist[now.FI][now.SE] + 1;
              que.push(next);
            } else if (dist[next.FI][next.SE] < dist[now.FI][now.SE]) {
              break;
            }
          } else {
            break;
          }
        } else {
          break;
        }
      }
    }
  }
  if (dist[goal.FI][goal.SE] == INT_MAX) {
    cout << -1 << endl;
    return 0;
  }
  cout << dist[goal.FI][goal.SE] << endl;
}