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

#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define repi(i, a, b) for (ll i = (a); i < (b); ++i)
#define rep(i, a) repi(i, 0, a)
#define repdi(i, a, b) for (ll i = (a)-1; i >= (b); --i)
#define repd(i, a) repdi(i, a, 0)
#define itr(it, a) for (auto it = (a).begin(); it != (a).end(); ++it)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()

using ll = long long;
using P = std::pair<ll, ll>;

constexpr ll INF = 1ll << 60;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

template <class S, class T>
std::ostream &operator<<(std::ostream &out, const std::pair<S, T> &a) {
  std::cout << '(' << a.first << ", " << a.second << ')';
  return out;
}

template <class T>
std::ostream &operator<<(std::ostream &out, const std::vector<T> &a) {
  std::cout << '[';
  rep(i, a.size()) {
    std::cout << a[i];
    if (i != a.size() - 1)
      std::cout << ", ";
  }
  std::cout << ']';
  return out;
}

const ll dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

ll H, W, K;
ll x1, x2, y1, y2;
std::string fld[1000010];

int main() {
  std::cin >> H >> W >> K;
  std::cin >> x1 >> y1 >> x2 >> y2;
  --x1;
  --y1;
  --x2;
  --y2;

  std::vector<std::vector<ll>> dist(H, std::vector<ll>(W, INF));

  rep(i, H) { std::cin >> fld[i]; }

  dist[x1][y1] = 0;

  std::queue<P> que;
  que.emplace(x1, y1);

  while (!que.empty()) {
    P p = que.front();
    que.pop();
    ll i = p.first, j = p.second;

    if (i == x2 && j == y2) {
      std::cout << dist[i][j] << std::endl;

      return 0;
    }

    rep(d, 4) repi(len, 1, K + 1) {
      ll ni = i + dy[d] * len, nj = j + dx[d] * len;

      if (!(ni >= 0 && ni < H && nj >= 0 && nj < W))
        break;

      if (fld[ni][nj] == '@')
        break;

      if (dist[ni][nj] != INF && dist[i][j] >= dist[ni][nj])
        break;

      if (dist[ni][nj] != INF)
        continue;

      dist[ni][nj] = dist[i][j] + 1;
      que.emplace(ni, nj);
    }
  }

  std::cout << -1 << std::endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define repi(i, a, b) for (ll i = (a); i < (b); ++i)
#define rep(i, a) repi(i, 0, a)
#define repdi(i, a, b) for (ll i = (a)-1; i >= (b); --i)
#define repd(i, a) repdi(i, a, 0)
#define itr(it, a) for (auto it = (a).begin(); it != (a).end(); ++it)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()

using ll = long long;
using P = std::pair<ll, ll>;

constexpr ll INF = 1ll << 60;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

template <class S, class T>
std::ostream &operator<<(std::ostream &out, const std::pair<S, T> &a) {
  std::cout << '(' << a.first << ", " << a.second << ')';
  return out;
}

template <class T>
std::ostream &operator<<(std::ostream &out, const std::vector<T> &a) {
  std::cout << '[';
  rep(i, a.size()) {
    std::cout << a[i];
    if (i != a.size() - 1)
      std::cout << ", ";
  }
  std::cout << ']';
  return out;
}

const ll dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

ll H, W, K;
ll x1, x2, y1, y2;
std::string fld[1000010];

int main() {
  std::cin >> H >> W >> K;
  std::cin >> x1 >> y1 >> x2 >> y2;
  --x1;
  --y1;
  --x2;
  --y2;

  std::vector<std::vector<ll>> dist(H, std::vector<ll>(W, INF));

  rep(i, H) { std::cin >> fld[i]; }

  dist[x1][y1] = 0;

  std::queue<P> que;
  que.emplace(x1, y1);

  while (!que.empty()) {
    P p = que.front();
    que.pop();
    ll i = p.first, j = p.second;

    if (i == x2 && j == y2) {
      std::cout << dist[i][j] << std::endl;

      return 0;
    }

    rep(d, 4) repi(len, 1, K + 1) {
      ll ni = i + dy[d] * len, nj = j + dx[d] * len;

      if (!(ni >= 0 && ni < H && nj >= 0 && nj < W))
        break;

      if (fld[ni][nj] == '@')
        break;

      if (dist[ni][nj] != INF && dist[i][j] >= dist[ni][nj])
        continue;

      dist[ni][nj] = dist[i][j] + 1;
      que.emplace(ni, nj);
    }
  }

  std::cout << -1 << std::endl;

  return 0;
}