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

#include <bits/stdc++.h>
using namespace std;
template <class T> ostream &operator<<(ostream &os, vector<T> V) {
  os << "[ ";
  for (auto v : V)
    os << v << " ";
  return os << "]";
}
template <class T> ostream &operator<<(ostream &os, set<T> S) {
  os << "{ ";
  for (auto s : S)
    os << s << " ";
  return os << "}";
}
template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) {
  return os << "(" << P.first << "," << P.second << ")";
}
template <class L, class R> ostream &operator<<(ostream &os, map<L, R> M) {
  os << "{ ";
  for (auto m : M)
    os << "(" << m.first << ":" << m.second << ") ";
  return os << "}";
}
#ifdef LMAO
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
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...);
}
#else
#define trace(...) 42
#endif
#define ff first
#define ss second
#define pb push_back
#define eb emplace_back
#define endl "\\n"
#define tcase                                                                  \\
  int tt;                                                                      \\
  cin >> tt;                                                                   \\
  while (tt--)
#define tcase2                                                                 \\
  int tt;                                                                      \\
  cin >> tt;                                                                   \\
  for (int qq = 1; qq <= tt; qq++)
#define all(x) x.begin(), x.end()
typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;
const int MAX = 1000010;
const int MOD2 = 998244353;
const int MOD = 1000000007;
const int INF = 1e8;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;
const ld EPS = 1e-7;
#define forn(i, n) for (int i = 0; i < (int)n; i++)
#define forne(i, a, b) for (int i = a; i <= b; i++)
/*****************start************/
const int mxn = 1e6 + 1;
int dirx[] = {1, 0, -1, 0};
int diry[] = {0, 1, 0, -1};
int h, w, k;
vector<vector<int>> dis, vis;
vector<string> grid;
pair<int, int> s, des;

void bfs(pair<int, int> s) {
  dis[s.first][s.second] = 0;
  queue<pair<int, int>> q;
  q.push(s);
  vis[s.first][s.second] = 1;
  while (!q.empty()) {
    auto v = q.front();
    q.pop();
    // trace(v);
    for (int i = 0; i < 4; i++) {
      for (int d = 1; d <= k; d++) {
        pair<int, int> u = {v.first + d * dirx[i], v.second + d * diry[i]};
        // trace(u);
        if (u.first < 1 or u.first > h or u.second < 1 or u.second > w)
          break;
        if (dis[u.first][u.second] < dis[v.first][v.second] + 1)
          break;
        else if (vis[u.first][u.second])
          continue;
        else if (grid[u.first][u.second] == '@')
          break;
        else {
          dis[u.first][u.second] = dis[v.first][v.second] + 1;
          vis[u.first][u.second] = 1;
          q.push(u);
          if (u == des)
            return;
        }
      }
    }
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cout << fixed << setprecision(25);
  cerr << fixed << setprecision(10);
  // auto start = std::chrono::high_resolution_clock::now();
  cin >> h >> w >> k;
  grid.resize(h + 1);
  dis.assign(h + 1, vector<int>(w + 1, INF));
  vis.assign(h + 1, vector<int>(w + 1, 0));
  cin >> s.first >> s.second >> des.first >> des.second;
  for (int i = 1; i <= h; i++) {
    cin >> grid[i];
    grid[i] = " " + grid[i];
  }
  bfs(s);
  // trace(dis);
  // trace(vis);
  cout << ((vis[des.first][des.second]) ? dis[des.first][des.second] : -1)
       << endl;

  // auto stop = std::chrono::high_resolution_clock::now();
  // auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(stop -
  // start); cerr << "Time taken : " << ((long double)duration.count())/((long
  // double) 1e9) <<"s "<< endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
template <class T> ostream &operator<<(ostream &os, vector<T> V) {
  os << "[ ";
  for (auto v : V)
    os << v << " ";
  return os << "]";
}
template <class T> ostream &operator<<(ostream &os, set<T> S) {
  os << "{ ";
  for (auto s : S)
    os << s << " ";
  return os << "}";
}
template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) {
  return os << "(" << P.first << "," << P.second << ")";
}
template <class L, class R> ostream &operator<<(ostream &os, map<L, R> M) {
  os << "{ ";
  for (auto m : M)
    os << "(" << m.first << ":" << m.second << ") ";
  return os << "}";
}
#ifdef LMAO
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
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...);
}
#else
#define trace(...) 42
#endif
#define ff first
#define ss second
#define pb push_back
#define eb emplace_back
#define endl "\\n"
#define tcase                                                                  \\
  int tt;                                                                      \\
  cin >> tt;                                                                   \\
  while (tt--)
#define tcase2                                                                 \\
  int tt;                                                                      \\
  cin >> tt;                                                                   \\
  for (int qq = 1; qq <= tt; qq++)
#define all(x) x.begin(), x.end()
typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;
const int MAX = 1000010;
const int MOD2 = 998244353;
const int MOD = 1000000007;
const int INF = 1e8;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;
const ld EPS = 1e-7;
#define forn(i, n) for (int i = 0; i < (int)n; i++)
#define forne(i, a, b) for (int i = a; i <= b; i++)
/*****************start************/
const int mxn = 1e6 + 1;
int dirx[] = {1, 0, -1, 0};
int diry[] = {0, 1, 0, -1};
int h, w, k;
vector<vector<int>> dis, vis;
vector<string> grid;
pair<int, int> s, des;

void bfs(pair<int, int> s) {
  dis[s.first][s.second] = 0;
  queue<pair<int, int>> q;
  q.push(s);
  vis[s.first][s.second] = 1;
  while (!q.empty()) {
    auto v = q.front();
    q.pop();
    // trace(v);
    for (int i = 0; i < 4; i++) {
      for (int d = 1; d <= k; d++) {
        pair<int, int> u = {v.first + d * dirx[i], v.second + d * diry[i]};
        // trace(u);
        if (u.first < 1 or u.first > h or u.second < 1 or u.second > w)
          break;
        else if (vis[u.first][u.second])
          continue;
        else if (grid[u.first][u.second] == '@')
          break;
        else {
          dis[u.first][u.second] = dis[v.first][v.second] + 1;
          vis[u.first][u.second] = 1;
          q.push(u);
          if (u == des)
            return;
        }
      }
    }
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cout << fixed << setprecision(25);
  cerr << fixed << setprecision(10);
  // auto start = std::chrono::high_resolution_clock::now();
  cin >> h >> w >> k;
  grid.resize(h + 1);
  dis.assign(h + 1, vector<int>(w + 1, -1));
  vis.assign(h + 1, vector<int>(w + 1, 0));
  cin >> s.first >> s.second >> des.first >> des.second;
  for (int i = 1; i <= h; i++) {
    cin >> grid[i];
    grid[i] = " " + grid[i];
  }
  bfs(s);
  // trace(dis);
  // trace(vis);
  cout << dis[des.first][des.second] << endl;

  // auto stop = std::chrono::high_resolution_clock::now();
  // auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(stop -
  // start); cerr << "Time taken : " << ((long double)duration.count())/((long
  // double) 1e9) <<"s "<< endl;
  return 0;
}