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

#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define mk make_pair
#define fi first
#define se second
#define vll vector<ll>
#define pii pair<ll, ll>
#define vvll vector<vector<ll>>
#define pb push_back
#define sz(v) (ll)(v).size()
#define inf 1e18
#define md 1e9 + 7
#define all(v) (v).begin(), (v).end()
#define rep(i, a, b) for (ll i = a; i < b; ++i)
#define tel(a)                                                                 \\
  { cout << a << "\\n"; }
#define tell(a, b)                                                             \\
  { cout << a << " | " << b << "\\n"; }
#define telll(a, b, c)                                                         \\
  { cout << a << " | " << b << " | " << c << "\\n"; }
#define teln(v, n)                                                             \\
  {                                                                            \\
    cout << "v- ";                                                             \\
    rep(o, 0, n) cout << v[o] << " ";                                          \\
    cout << "\\n";                                                              \\
  }
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
using namespace std;
#define TRACE

#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << std::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(...)
#endif

#define M 100010

ll dx[] = {1, -1, 0, 0};
ll dy[] = {0, 0, 1, -1};
int main() {
  IOS;

  ll h, w, k;
  cin >> h >> w >> k;
  ll x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--, y1--, x2--, y2--;
  ll a[h][w], d[h][w];
  rep(i, 0, h) rep(j, 0, w) {
    char c;
    cin >> c;
    a[i][j] = (c == '.');
    d[i][j] = inf;
  }
  queue<pii> q;
  q.push({x1, y1}), d[x1][y1] = 0;
  while (!q.empty()) {
    pii u = q.front();
    q.pop();
    ll x = u.fi, y = u.se;
    rep(i, 0, 4) rep(j, 1, k + 1) {
      ll nx = x + dx[i] * j, ny = y + dy[i] * j;
      if (nx < 0 or ny < 0 or nx >= h or ny >= w)
        continue;
      if (!a[nx][ny] or d[x][y] + 1 > d[nx][ny])
        break;
      if (d[x][y] + 1 < d[nx][ny]) {
        d[nx][ny] = d[x][y] + 1;
        q.push({nx, ny});
      }
    }
  }
  ll ans = d[x2][y2];
  cout << ((ans == inf) ? -1 : ans);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define mk make_pair
#define fi first
#define se second
#define vll vector<ll>
#define pii pair<ll, ll>
#define vvll vector<vector<ll>>
#define pb push_back
#define sz(v) (ll)(v).size()
#define inf 1e18
#define md 1e9 + 7
#define all(v) (v).begin(), (v).end()
#define rep(i, a, b) for (ll i = a; i < b; ++i)
#define tel(a)                                                                 \\
  { cout << a << "\\n"; }
#define tell(a, b)                                                             \\
  { cout << a << " | " << b << "\\n"; }
#define telll(a, b, c)                                                         \\
  { cout << a << " | " << b << " | " << c << "\\n"; }
#define teln(v, n)                                                             \\
  {                                                                            \\
    cout << "v- ";                                                             \\
    rep(o, 0, n) cout << v[o] << " ";                                          \\
    cout << "\\n";                                                              \\
  }
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
using namespace std;
#define TRACE

#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << std::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(...)
#endif

#define M 100010

ll dx[] = {1, -1, 0, 0};
ll dy[] = {0, 0, 1, -1};
int main() {
  IOS;

  ll h, w, k;
  cin >> h >> w >> k;
  ll x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--, y1--, x2--, y2--;
  ll a[h][w], d[h][w];
  rep(i, 0, h) rep(j, 0, w) {
    char c;
    cin >> c;
    a[i][j] = (c == '.');
    d[i][j] = inf;
  }
  queue<pii> q;
  q.push({x1, y1}), d[x1][y1] = 0;
  while (!q.empty()) {
    pii u = q.front();
    q.pop();
    ll x = u.fi, y = u.se;
    rep(i, 0, 4) rep(j, 1, k + 1) {
      ll nx = x + dx[i] * j, ny = y + dy[i] * j;
      if (nx < 0 or ny < 0 or nx >= h or ny >= w)
        continue;
      if (!a[nx][ny])
        break;
      if (d[x][y] + 1 < d[nx][ny]) {
        d[nx][ny] = d[x][y] + 1;
        q.push({nx, ny});
      }
    }
  }
  ll ans = d[x2][y2];
  cout << ((ans == inf) ? -1 : ans);
  return 0;
}