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

#include <algorithm>
#include <cmath>
#include <deque>
#include <iostream>
#include <numeric>
#include <queue>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;
using ll = long long;

void _cin() {}
template <class Head, class... Tail> void _cin(Head &&head, Tail &&...tail) {
  cin >> head;
  _cin(forward<Tail>(tail)...);
}
void _cout() { cout << "\\n"; }
template <class Head, class... Tail> void _cout(Head &&head, Tail &&...tail) {
  cout << head;
  _cout(forward<Tail>(tail)...);
}
ll gcd(ll a, ll b) { return (b == 0) ? a : gcd(b, a % b); }
template <typename S, typename T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
  cout << "[" << p.first << ", " << p.second << "]";
  return os;
}

#define Sq(x) (x) * (x)
#define For(i, n) for (int i = 0; i < (n); i++)
#define Rep(n) For(_, n)
#define Range(c) c.begin(), c.end()
#define RevRange(c) c.rbegin(), c.rend()
#define Contains(c, x) (find(Range(c), x) != c.end())
#define Search(rb, re, x) distance(rb, find(rb, re, x))
#define Sort(a) sort(Range(a))
#define DeSort(a) sort(RevRange(a))
#define Reverse(c) reverse(Range(c))
#define Unique(a) a.erase(unique(Range(a)), a.end())
#define Cusum(T, xs, sxs)                                                      \\
  vector<T> sxs(xs.size() + 1);                                                \\
  For(i, (int)xs.size()) sxs[i + 1] = sxs[i] + xs[i]
#define Cin(T, ...)                                                            \\
  T __VA_ARGS__;                                                               \\
  _cin(__VA_ARGS__)
#define Cins(T, n, xs)                                                         \\
  vector<T> xs(n);                                                             \\
  For(i, n) cin >> xs[i]
#define Cins2(T, n, xs, ys)                                                    \\
  vector<T> xs(n), ys(n);                                                      \\
  For(i, n) cin >> xs[i] >> ys[i]
#define Cins3(T, n, xs, ys, zs)                                                \\
  vector<T> xs(n), ys(n), zs(n);                                               \\
  For(i, n) cin >> xs[i] >> ys[i] >> zs[i]
#define Cinss(T, n, m, xs)                                                     \\
  Vec2(T, n, m, xs);                                                           \\
  For(i, n) For(j, m) cin >> xs[i][j]
#define Cinm(T, n, map)                                                        \\
  unordered_map<T, int> map;                                                   \\
  Rep(n) {                                                                     \\
    Cin(T, x);                                                                 \\
    map[x]++;                                                                  \\
  }
#define Cout(...) _cout(__VA_ARGS__)
#define Couts(xs)                                                              \\
  {                                                                            \\
    for (const auto &e : xs)                                                   \\
      cout << e << " ";                                                        \\
    cout << "\\n";                                                              \\
  }
#define Coutyn(cond) Cout((cond) ? "yes" : "no")
#define CoutYn(cond) Cout((cond) ? "Yes" : "No")
#define CoutYN(cond) Cout((cond) ? "YES" : "NO")
#define Return(expr)                                                           \\
  {                                                                            \\
    Cout(expr);                                                                \\
    return 0;                                                                  \\
  }
#define vc vector
#define Mini(a, x) a = min(a, x)
#define Maxi(a, x) a = max(a, x)

// constexpr int MOD = 1e9+7;

int main(void) {
  Cin(int, h, w, k);
  Cin(int, x1, y1, x2, y2);
  vc<string> s(h + 2);
  s[0] = string(w + 2, '@');
  For(i, h) {
    Cin(string, t);
    s[i + 1] = "@" + t + "@";
  }
  s.back() = string(w + 2, '@');

  constexpr int inf = 1e9;
  vc<vc<int>> v(h + 2, vc<int>(w + 2, inf));
  v[x1][y1] = 0;

  deque<pair<int, int>> q;
  q.emplace_back(x1, y1);
  while (!q.empty()) {
    auto [x, y] = q.front();
    q.pop_front();
    // Cout(x, " ", y);
    if (x == x2 && y == y2) {
      // For(i, h+2) Couts(v[i]);
      Return(v[x][y]);
    }

    for (int i = 1; i <= k; i++) {
      int xt = x + i, yt = y;
      if (s[xt][yt] == '@' || v[xt][yt] < v[x][y] + 1)
        break;
      if (v[xt][yt] == v[x][y] + 1)
        continue;
      v[xt][yt] = v[x][y] + 1;
      q.emplace_back(xt, yt);
    }
    for (int i = 1; i <= k; i++) {
      int xt = x - i, yt = y;
      if (s[xt][yt] == '@' || v[xt][yt] < v[x][y] + 1)
        break;
      if (v[xt][yt] == v[x][y] + 1)
        continue;
      v[xt][yt] = v[x][y] + 1;
      q.emplace_back(xt, yt);
    }
    for (int i = 1; i <= k; i++) {
      int xt = x, yt = y + i;
      if (s[xt][yt] == '@' || v[xt][yt] < v[x][y] + 1)
        break;
      if (v[xt][yt] == v[x][y] + 1)
        continue;
      v[xt][yt] = v[x][y] + 1;
      q.emplace_back(xt, yt);
    }
    for (int i = 1; i <= k; i++) {
      int xt = x, yt = y - i;
      if (s[xt][yt] == '@' || v[xt][yt] < v[x][y] + 1)
        break;
      if (v[xt][yt] == v[x][y] + 1)
        continue;
      v[xt][yt] = v[x][y] + 1;
      q.emplace_back(xt, yt);
    }
  }
  Cout(-1);
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <deque>
#include <iostream>
#include <numeric>
#include <queue>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;
using ll = long long;

void _cin() {}
template <class Head, class... Tail> void _cin(Head &&head, Tail &&...tail) {
  cin >> head;
  _cin(forward<Tail>(tail)...);
}
void _cout() { cout << "\\n"; }
template <class Head, class... Tail> void _cout(Head &&head, Tail &&...tail) {
  cout << head;
  _cout(forward<Tail>(tail)...);
}
ll gcd(ll a, ll b) { return (b == 0) ? a : gcd(b, a % b); }
template <typename S, typename T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
  cout << "[" << p.first << ", " << p.second << "]";
  return os;
}

#define Sq(x) (x) * (x)
#define For(i, n) for (int i = 0; i < (n); i++)
#define Rep(n) For(_, n)
#define Range(c) c.begin(), c.end()
#define RevRange(c) c.rbegin(), c.rend()
#define Contains(c, x) (find(Range(c), x) != c.end())
#define Search(rb, re, x) distance(rb, find(rb, re, x))
#define Sort(a) sort(Range(a))
#define DeSort(a) sort(RevRange(a))
#define Reverse(c) reverse(Range(c))
#define Unique(a) a.erase(unique(Range(a)), a.end())
#define Cusum(T, xs, sxs)                                                      \\
  vector<T> sxs(xs.size() + 1);                                                \\
  For(i, (int)xs.size()) sxs[i + 1] = sxs[i] + xs[i]
#define Cin(T, ...)                                                            \\
  T __VA_ARGS__;                                                               \\
  _cin(__VA_ARGS__)
#define Cins(T, n, xs)                                                         \\
  vector<T> xs(n);                                                             \\
  For(i, n) cin >> xs[i]
#define Cins2(T, n, xs, ys)                                                    \\
  vector<T> xs(n), ys(n);                                                      \\
  For(i, n) cin >> xs[i] >> ys[i]
#define Cins3(T, n, xs, ys, zs)                                                \\
  vector<T> xs(n), ys(n), zs(n);                                               \\
  For(i, n) cin >> xs[i] >> ys[i] >> zs[i]
#define Cinss(T, n, m, xs)                                                     \\
  Vec2(T, n, m, xs);                                                           \\
  For(i, n) For(j, m) cin >> xs[i][j]
#define Cinm(T, n, map)                                                        \\
  unordered_map<T, int> map;                                                   \\
  Rep(n) {                                                                     \\
    Cin(T, x);                                                                 \\
    map[x]++;                                                                  \\
  }
#define Cout(...) _cout(__VA_ARGS__)
#define Couts(xs)                                                              \\
  {                                                                            \\
    for (const auto &e : xs)                                                   \\
      cout << e << " ";                                                        \\
    cout << "\\n";                                                              \\
  }
#define Coutyn(cond) Cout((cond) ? "yes" : "no")
#define CoutYn(cond) Cout((cond) ? "Yes" : "No")
#define CoutYN(cond) Cout((cond) ? "YES" : "NO")
#define Return(expr)                                                           \\
  {                                                                            \\
    Cout(expr);                                                                \\
    return 0;                                                                  \\
  }
#define vc vector
#define Mini(a, x) a = min(a, x)
#define Maxi(a, x) a = max(a, x)

// constexpr int MOD = 1e9+7;

int main(void) {
  Cin(int, h, w, k);
  Cin(int, x1, y1, x2, y2);
  vc<string> s(h + 2);
  s[0] = string(w + 2, '@');
  For(i, h) {
    Cin(string, t);
    s[i + 1] = "@" + t + "@";
  }
  s.back() = string(w + 2, '@');

  constexpr int inf = 1e9;
  vc<vc<int>> v(h + 2, vc<int>(w + 2, inf));
  v[x1][y1] = 0;

  deque<pair<int, int>> q;
  q.emplace_back(x1, y1);
  while (!q.empty()) {
    auto [x, y] = q.front();
    q.pop_front();
    // Cout(x, " ", y);
    if (x == x2 && y == y2) {
      // For(i, h+2) Couts(v[i]);
      Return(v[x][y]);
    }

    for (int i = 1; i <= k; i++) {
      int xt = x + i, yt = y;
      if (s[xt][yt] == '@' || v[xt][yt] < v[x][y] + 1)
        break;
      v[xt][yt] = v[x][y] + 1;
      q.emplace_back(xt, yt);
    }
    for (int i = 1; i <= k; i++) {
      int xt = x - i, yt = y;
      if (s[xt][yt] == '@' || v[xt][yt] < v[x][y] + 1)
        break;
      v[xt][yt] = v[x][y] + 1;
      q.emplace_back(xt, yt);
    }
    for (int i = 1; i <= k; i++) {
      int xt = x, yt = y + i;
      if (s[xt][yt] == '@' || v[xt][yt] < v[x][y] + 1)
        break;
      v[xt][yt] = v[x][y] + 1;
      q.emplace_back(xt, yt);
    }
    for (int i = 1; i <= k; i++) {
      int xt = x, yt = y - i;
      if (s[xt][yt] == '@' || v[xt][yt] < v[x][y] + 1)
        break;
      v[xt][yt] = v[x][y] + 1;
      q.emplace_back(xt, yt);
    }
  }
  Cout(-1);
}