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

#include <bits/stdc++.h>

using namespace std;

#define boost                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define ll long long int
// #define int ll
#define vi vector<ll>
#define vvi vector<vi>
#define pii pair<ll, ll>
#define vii vector<pii>
#define pb push_back
#define B begin()
#define E end()
#define sz(A) (ll)(A).size()
#define F first
#define S second
#define loop(z, v) for (auto &z : v)
#define sp << ' ' <<
#define sps << ' '
#define nl "\\n"
// #define nl endl
#define fat 1000000007
// #define fat 998244353
#define PI_2 1.57079632679489661923
vi exist(1000005, 0);

vii dir = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};

int main() {
  ll tt = 1, n, m, k = 0, i, j, p, q;
  ll a, b, c, d, h, w, u, v, x, y, t = 0;
  ll ans = 0;

  ll l = 0, r = 0;
  bool poss = true;

  boost;
  string s;

  time_t tic, toc;
  tic = clock();

  // cin >> tt;
  for (int _ = 1; _ <= tt; _++) {
    // cout << "Case #" << _ << ": ";
    cin >> h >> w >> k;
    ll x1, x2, y1, y2;

    cin >> x1 >> y1 >> x2 >> y2;
    x1--, x2--, y1--, y2--;

    vector<string> g(h);
    for (i = 0; i < h; i++) {
      cin >> g[i];
    }

    vvi dist(h, vi(w, -1));
    dist[x1][y1] = 0;

    queue<pii> q;
    q.push({x1, y1});
    vvi done(h, vi(w, 0));
    while (!q.empty()) {
      x = q.front().F;
      y = q.front().S;
      done[x][y] = 1;

      q.pop();

      loop(z, dir) {
        for (l = 1; l <= k; l++) {
          int nx = x + z.F * l;
          int ny = y + z.S * l;

          if (nx < 0 or ny < 0 or nx >= h or ny >= w)
            break;
          if (g[nx][ny] == '@')
            break;
          if (done[nx][ny])
            break;
          if (dist[nx][ny] != -1)
            continue;

          dist[nx][ny] = dist[x][y] + 1;
          q.push({nx, ny});
        }
      }
    }

    cout << dist[x2][y2] << nl;
  }
  // cerr << (clock() - tic*1.0) / CLOCKS_PER_SEC << nl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define boost                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define ll long long int
// #define int ll
#define vi vector<ll>
#define vvi vector<vi>
#define pii pair<ll, ll>
#define vii vector<pii>
#define pb push_back
#define B begin()
#define E end()
#define sz(A) (ll)(A).size()
#define F first
#define S second
#define loop(z, v) for (auto &z : v)
#define sp << ' ' <<
#define sps << ' '
#define nl "\\n"
// #define nl endl
#define fat 1000000007
// #define fat 998244353
#define PI_2 1.57079632679489661923
vi exist(1000005, 0);

vii dir = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};

int main() {
  ll tt = 1, n, m, k = 0, i, j, p, q;
  ll a, b, c, d, h, w, u, v, x, y, t = 0;
  ll ans = 0;

  ll l = 0, r = 0;
  bool poss = true;

  boost;
  string s;

  time_t tic, toc;
  tic = clock();

  // cin >> tt;
  for (int _ = 1; _ <= tt; _++) {
    // cout << "Case #" << _ << ": ";
    cin >> h >> w >> k;
    ll x1, x2, y1, y2;

    cin >> x1 >> y1 >> x2 >> y2;
    x1--, x2--, y1--, y2--;

    vector<string> g(h);
    for (i = 0; i < h; i++) {
      cin >> g[i];
    }

    vvi dist(h, vi(w, -1));
    dist[x1][y1] = 0;

    queue<pii> q;
    q.push({x1, y1});
    while (!q.empty()) {
      x = q.front().F;
      y = q.front().S;
      q.pop();

      loop(z, dir) {
        for (l = 1; l <= k; l++) {
          int nx = x + z.F * l;
          int ny = y + z.S * l;

          if (nx < 0 or ny < 0 or nx >= h or ny >= w)
            break;
          if (g[nx][ny] == '@')
            break;
          if (dist[nx][ny] != -1)
            continue;

          dist[nx][ny] = dist[x][y] + 1;
          q.push({nx, ny});
        }
      }
    }

    cout << dist[x2][y2] << nl;
  }
  // cerr << (clock() - tic*1.0) / CLOCKS_PER_SEC << nl;
  return 0;
}