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

// Remeber Choose Compiler: GCC9
#include <bits/stdc++.h>

using namespace std;

// Debug Output Utility
#ifdef LOCAL
#define dbg(args...)                                                           \\
  do {                                                                         \\
    cout << #args << " -> ";                                                   \\
    err(args);                                                                 \\
  } while (0)
void err() { std::cout << std::endl; }
template <typename T, typename... Args> void err(T a, Args... args) {
  std::cout << a << ' ';
  err(args...);
}
template <template <typename...> class T, typename t, typename... A>
void err(const T<t> &arg, const A &...args) {
  for (auto &v : arg)
    std::cout << v << ' ';
  err(args...);
}
#else
#define dbg(...)
#endif

// Macro Definitions:
#define mp make_pair
#define fi first
#define se second
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()

// Type Alias:
using int64 = long long;
using PII = pair<int, int>;

// Global Const Variable
const int64 INF = 0x3f3f3f3f3f3f3f3f;
const int MOD = 1000000007;
const int MAXN = 200005;
const int M = 1000005;
const int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

void run() {
  int h, w, k;
  cin >> h >> w >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  vector<vector<char>> mat(h + 1, vector<char>(w + 1));
  vector<vector<int>> dis(h + 1, vector<int>(w + 1, -1));
  string s;
  for (int i = 1; i <= h; i++) {
    cin >> s;
    for (int j = 1; j <= w; j++)
      mat[i][j] = s[j - 1];
  }
  if (mat[x1][y1] == '@' || mat[x2][y2] == '@') {
    cout << -1 << endl;
    return;
  }
  queue<PII> q;
  q.emplace(x1, y1);
  dis[x1][y1] = 0;
  while (!q.empty()) {
    int x = q.front().fi, y = q.front().se;
    q.pop();
    for (int p = 0; p < 4; p++) {
      for (int j = 1; j <= k; j++) {
        int xx = x + j * dir[p][0], yy = y + j * dir[p][1];
        if (1 <= xx && xx <= h && 1 <= yy && yy <= w) {
          if (mat[xx][yy] == '@')
            break;
          if (dis[xx][yy] != -1) {
            if (dis[xx][yy] > dis[x][y])
              continue;
            else
              break;
          }
          q.emplace(xx, yy);
          dis[xx][yy] = dis[x][y] + 1;
          if (xx == x2 && yy == y2) {
            cout << dis[xx][yy] << endl;
            return;
          }
        } else {
          break;
        }
      }
    }
  }
  cout << -1 << endl;
}

// Debug Input Redirection
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
#ifdef LOCAL
  freopen("in.txt", "r", stdin);
#endif
  run();
}

Error: TLE

Fixed submission:

// Remeber Choose Compiler: GCC9
#include <bits/stdc++.h>

using namespace std;

// Debug Output Utility
#ifdef LOCAL
#define dbg(args...)                                                           \\
  do {                                                                         \\
    cout << #args << " -> ";                                                   \\
    err(args);                                                                 \\
  } while (0)
void err() { std::cout << std::endl; }
template <typename T, typename... Args> void err(T a, Args... args) {
  std::cout << a << ' ';
  err(args...);
}
template <template <typename...> class T, typename t, typename... A>
void err(const T<t> &arg, const A &...args) {
  for (auto &v : arg)
    std::cout << v << ' ';
  err(args...);
}
#else
#define dbg(...)
#endif

// Macro Definitions:
#define mp make_pair
#define fi first
#define se second
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()

// Type Alias:
using int64 = long long;
using PII = pair<int, int>;

// Global Const Variable
const int64 INF = 0x3f3f3f3f3f3f3f3f;
const int MOD = 1000000007;
const int MAXN = 200005;
const int M = 1000005;
const int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

void run() {
  int h, w, k;
  cin >> h >> w >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  vector<vector<char>> mat(h + 1, vector<char>(w + 1));
  vector<vector<int>> dis(h + 1, vector<int>(w + 1, -1));
  string s;
  for (int i = 1; i <= h; i++) {
    cin >> s;
    for (int j = 1; j <= w; j++)
      mat[i][j] = s[j - 1];
  }
  if (mat[x1][y1] == '@' || mat[x2][y2] == '@') {
    cout << -1 << endl;
    return;
  }
  queue<PII> q;
  q.emplace(x1, y1);
  dis[x1][y1] = 0;
  while (!q.empty()) {
    int x = q.front().fi, y = q.front().se;
    q.pop();
    for (int p = 0; p < 4; p++) {
      for (int j = 1; j <= k; j++) {
        int xx = x + j * dir[p][0], yy = y + j * dir[p][1];
        if (1 <= xx && xx <= h && 1 <= yy && yy <= w) {
          if (mat[xx][yy] == '@')
            break;
          if (dis[xx][yy] != -1)
            continue;
          q.emplace(xx, yy);
          dis[xx][yy] = dis[x][y] + 1;
          if (xx == x2 && yy == y2) {
            cout << dis[xx][yy] << endl;
            return;
          }
        } else {
          break;
        }
      }
    }
  }
  cout << -1 << endl;
}

// Debug Input Redirection
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
#ifdef LOCAL
  freopen("in.txt", "r", stdin);
#endif
  run();
}