Problem p02644 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
#define FI first.first
#define SE first.second
#define TH second
#define fi first
#define se second
#define th second

using namespace std;

template <typename T> string to_string(const vector<T> &vc, int w) {
  if (vc.empty())
    return "";
  if (w + 1 == vc.size())
    return to_string(vc[w]);
  return to_string(vc[w]) + "," + to_string(vc, w + 1);
}

template <typename T> string to_string(const vector<T> &vc) {
  return "{" + to_string(vc, 0) + "}";
}

void debug_out() { cerr << endl; }

template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}

#ifdef DEBUG
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

class DebugStream {
} LOG;
template <typename T> DebugStream &operator<<(DebugStream &s, const T &) {
  return s;
}
#ifdef DEBUG
#define LOG clog
#endif

typedef long long ll;
typedef pair<ll, ll> ii;
typedef long double ld;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

const int maxN = 1e5 + 9, maxV = 1e6 + 9, MOD = 1e9 + 7, SQ = 335, lg = 20,
          bs = 29;

int dr[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
#ifdef DEBUG
  freopen("input.txt", "r", stdin);
#endif

  int n, m, k;
  cin >> n >> m >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--, y1--, x2--, y2--;
  vector<string> mat(n);
  vector<vector<int>> shr(n, vector<int>(m, -1));
  for (auto &el : mat)
    cin >> el;
  auto ins = [&](int x, int y) { return x >= 0 && y >= 0 && x < n && y < m; };

  queue<ii> q;
  q.push(ii(x1, y1));
  shr[x1][y1] = 0;
  while (q.size()) {
    auto cr = q.front();
    q.pop();
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k; j++) {
        int x = cr.fi + j * dr[i][0], y = cr.se + j * dr[i][1];
        if (!ins(x, y) || mat[x][y] == '@')
          break;
        if (shr[x][y] == -1) {
          shr[x][y] = shr[cr.fi][cr.se] + 1;
          q.push(ii(x, y));
        } else if (shr[x][y] < shr[cr.fi][cr.se])
          break;
      }
    }
  }
  int ky = shr[x2][y2];
  cout << ky << '\n';
}


Fixed submission:
#include "bits/stdc++.h"
#define FI first.first
#define SE first.second
#define TH second
#define fi first
#define se second
#define th second

using namespace std;

template <typename T> string to_string(const vector<T> &vc, int w) {
  if (vc.empty())
    return "";
  if (w + 1 == vc.size())
    return to_string(vc[w]);
  return to_string(vc[w]) + "," + to_string(vc, w + 1);
}

template <typename T> string to_string(const vector<T> &vc) {
  return "{" + to_string(vc, 0) + "}";
}

void debug_out() { cerr << endl; }

template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}

#ifdef DEBUG
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

class DebugStream {
} LOG;
template <typename T> DebugStream &operator<<(DebugStream &s, const T &) {
  return s;
}
#ifdef DEBUG
#define LOG clog
#endif

typedef long long ll;
typedef pair<ll, ll> ii;
typedef long double ld;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

const int maxN = 1e5 + 9, maxV = 1e6 + 9, MOD = 1e9 + 7, SQ = 335, lg = 20,
          bs = 29;

int dr[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
#ifdef DEBUG
  freopen("input.txt", "r", stdin);
#endif

  int n, m, k;
  cin >> n >> m >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--, y1--, x2--, y2--;
  vector<string> mat(n);
  vector<vector<int>> shr(n, vector<int>(m, -1));
  for (auto &el : mat)
    cin >> el;
  auto ins = [&](int x, int y) { return x >= 0 && y >= 0 && x < n && y < m; };

  queue<ii> q;
  q.push(ii(x1, y1));
  shr[x1][y1] = 0;
  while (q.size()) {
    auto cr = q.front();
    q.pop();
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k; j++) {
        int x = cr.fi + j * dr[i][0], y = cr.se + j * dr[i][1];
        if (!ins(x, y) || mat[x][y] == '@')
          break;
        if (shr[x][y] == -1) {
          shr[x][y] = shr[cr.fi][cr.se] + 1;
          q.push(ii(x, y));
        } else if (shr[x][y] <= shr[cr.fi][cr.se])
          break;
      }
    }
  }
  int ky = shr[x2][y2];
  cout << ky << '\n';
}