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

#include <bits/stdc++.h>
using namespace std;
#define TRACE(x) x
#define WATCH(x) TRACE(cout << #x " = " << x << endl)
#define PRINT(x) TRACE(printf(x))
#define WATCHR(a, b)                                                           \\
  TRACE(for (auto c = a; c != b;) cout << *(c++) << " "; cout << endl)
#define WATCHC(V)                                                              \\
  TRACE({                                                                      \\
    cout << #V " = ";                                                          \\
    WATCHR(V.begin(), V.end());                                                \\
  })
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x).size()
#define mp make_pair
#define st first
#define nd second

using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using ld = long double;
using pll = pair<ll, ll>;
using vll = vector<ll>;
using pll = pair<ll, ll>;

void buff() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
}

constexpr ll MOD = 1e9 + 7;

inline ll pow_mod(ll a, ll b, ll mod = MOD) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
  }
  return res;
}
// End of template code!
vector<pii> dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
constexpr int inf = -1;
int main() {
  buff();
  int h, w, k;
  cin >> h >> w >> k;
  pii st, nd;
  cin >> st.first >> st.second;
  cin >> nd.first >> nd.second;
  st.first--;
  st.second--;
  nd.first--;
  nd.second--;
  vector<string> mat(h);
  vector<vector<int>> dist(h, vector<int>(w, inf));
  for (int i = 0; i < h; ++i) {
    cin >> mat[i];
  }

  queue<pair<int, int>> q;
  dist[st.first][st.second] = 0;
  q.push(st);

  auto inside = [&](int x, int y) {
    return (x >= 0 && x < h && y >= 0 && y < w);
  };
  while (!q.empty()) {
    auto vamo = q.front();
    q.pop();
    for (const auto &d : dirs) {
      int nx = vamo.first;
      int ny = vamo.second;
      for (int i = 0; i < k; ++i) {
        nx += d.first;
        ny += d.second;
        if (!inside(nx, ny))
          break;
        if (mat[nx][ny] == '@')
          break;
        if (dist[nx][ny] == dist[vamo.first][vamo.second])
          break;
        if (dist[nx][ny] == -1) {
          dist[nx][ny] = dist[vamo.first][vamo.second] + 1;
          q.push(make_pair(nx, ny));
        }
      }
    }
  }
  cout << dist[nd.first][nd.second] << '\\n';

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define TRACE(x) x
#define WATCH(x) TRACE(cout << #x " = " << x << endl)
#define PRINT(x) TRACE(printf(x))
#define WATCHR(a, b)                                                           \\
  TRACE(for (auto c = a; c != b;) cout << *(c++) << " "; cout << endl)
#define WATCHC(V)                                                              \\
  TRACE({                                                                      \\
    cout << #V " = ";                                                          \\
    WATCHR(V.begin(), V.end());                                                \\
  })
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x).size()
#define mp make_pair
#define st first
#define nd second

using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using ld = long double;
using pll = pair<ll, ll>;
using vll = vector<ll>;
using pll = pair<ll, ll>;

void buff() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
}

constexpr ll MOD = 1e9 + 7;

inline ll pow_mod(ll a, ll b, ll mod = MOD) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
  }
  return res;
}
// End of template code!
vector<pii> dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
constexpr int inf = -1;
int main() {
  buff();
  int h, w, k;
  cin >> h >> w >> k;
  pii st, nd;
  cin >> st.first >> st.second;
  cin >> nd.first >> nd.second;
  st.first--;
  st.second--;
  nd.first--;
  nd.second--;
  vector<string> mat(h);
  vector<vector<int>> dist(h, vector<int>(w, inf));
  for (int i = 0; i < h; ++i) {
    cin >> mat[i];
  }

  queue<pair<int, int>> q;
  dist[st.first][st.second] = 0;
  q.push(st);

  auto inside = [&](int x, int y) {
    return (x >= 0 && x < h && y >= 0 && y < w);
  };
  while (!q.empty()) {
    auto vamo = q.front();
    q.pop();
    for (const auto &d : dirs) {
      int nx = vamo.first;
      int ny = vamo.second;
      for (int i = 0; i < k; ++i) {
        nx += d.first;
        ny += d.second;
        if (!inside(nx, ny))
          break;
        if (mat[nx][ny] == '@')
          break;
        if (dist[nx][ny] != -1 && dist[nx][ny] <= dist[vamo.first][vamo.second])
          break;
        dist[nx][ny] = dist[vamo.first][vamo.second] + 1;
        q.push(make_pair(nx, ny));
      }
    }
  }
  cout << dist[nd.first][nd.second] << '\\n';

  return 0;
}