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

#include <bits/stdc++.h>
using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
// typedef vector<vector<ll>> Graph;

const ll mod = 1e9 + 7;
// const ll mod = 998244353;

#define REP(i, n) for (ll i = 0; i < (ll)n; i++)
#define dump(x) cerr << #x << " = " << (x) << endl;
#define spa << " " <<
#define fi first
#define se second

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

template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> v) {
  os << "(" << v.first << ", " << v.second << ")";
  return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> v) {
  for (int i = 0; i < (int)v.size(); i++) {
    if (i > 0) {
      os << " ";
    }
    os << v[i];
  }
  return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<vector<T>> v) {
  for (int i = 0; i < (int)v.size(); i++) {
    if (i > 0) {
      os << endl;
    }
    os << v[i];
  }
  return os;
}

template <typename T> void debug(vector<vector<T>> &v, ll h, ll w) {
  for (ll i = 0; i < h; i++) {
    cerr << v[i][0];
    for (ll j = 1; j < w; j++)
      cerr spa v[i][j];
    cerr << endl;
  }
};
template <typename T> void debug(vector<T> &v, ll n) {
  if (n != 0)
    cerr << v[0];
  for (ll i = 1; i < n; i++)
    cerr spa v[i];
  cerr << endl;
};

string num2bit(ll num, ll len) {
  string bit = "";
  REP(i, len) { bit += char('0' + (num >> i & 1)); }
  return bit;
}

struct P {
  ll x, y;
  P(ll x, ll y) : x(x), y(y){};
};

vector<ll> dx = {-1, 0, 1, 0};
vector<ll> dy = {0, -1, 0, 1};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll H, W, K;
  cin >> H >> W >> K;
  ll x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--, y1--, x2--, y2--;

  vector<string> c(H);
  REP(i, H) cin >> c[i];

  const ll INF = (1ll << 60);
  vector<vector<ll>> dist(H, vector<ll>(W, INF));

  queue<P> qu;

  qu.emplace(x1, y1);
  dist[x1][y1] = 0;

  while (!qu.empty()) {
    P p = qu.front();
    qu.pop();
    REP(dir, 4) {
      for (int k = 1; k <= K; k++) {
        ll nx = p.x + k * dx[dir];
        ll ny = p.y + k * dy[dir];

        if (nx < 0 or nx > H - 1 or ny < 0 or ny > W - 1)
          break;
        if (c[nx][ny] == '@')
          break;
        if (dist[nx][ny] <= dist[p.x][p.y])
          break;
        if (dist[nx][ny] != INF)
          continue;

        dist[nx][ny] = dist[p.x][p.y] + 1;
        qu.emplace(nx, ny);
      }
    }
  }

  // debug(dist, H, W);

  cout << (dist[x2][y2] == INF ? -1 : dist[x2][y2]) << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
// typedef vector<vector<ll>> Graph;

const ll mod = 1e9 + 7;
// const ll mod = 998244353;

#define REP(i, n) for (ll i = 0; i < (ll)n; i++)
#define dump(x) cerr << #x << " = " << (x) << endl;
#define spa << " " <<
#define fi first
#define se second

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

template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> v) {
  os << "(" << v.first << ", " << v.second << ")";
  return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> v) {
  for (int i = 0; i < (int)v.size(); i++) {
    if (i > 0) {
      os << " ";
    }
    os << v[i];
  }
  return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<vector<T>> v) {
  for (int i = 0; i < (int)v.size(); i++) {
    if (i > 0) {
      os << endl;
    }
    os << v[i];
  }
  return os;
}

template <typename T> void debug(vector<vector<T>> &v, ll h, ll w) {
  for (ll i = 0; i < h; i++) {
    cerr << v[i][0];
    for (ll j = 1; j < w; j++)
      cerr spa v[i][j];
    cerr << endl;
  }
};
template <typename T> void debug(vector<T> &v, ll n) {
  if (n != 0)
    cerr << v[0];
  for (ll i = 1; i < n; i++)
    cerr spa v[i];
  cerr << endl;
};

string num2bit(ll num, ll len) {
  string bit = "";
  REP(i, len) { bit += char('0' + (num >> i & 1)); }
  return bit;
}

struct P {
  ll x, y;
  P(ll x, ll y) : x(x), y(y){};
};

vector<ll> dx = {-1, 0, 1, 0};
vector<ll> dy = {0, -1, 0, 1};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll H, W, K;
  cin >> H >> W >> K;
  ll x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--, y1--, x2--, y2--;

  vector<string> c(H);
  REP(i, H) cin >> c[i];

  const ll INF = (1ll << 60);
  vector<vector<ll>> dist(H, vector<ll>(W, INF));

  queue<P> qu;

  qu.emplace(x1, y1);
  dist[x1][y1] = 0;

  while (!qu.empty()) {
    P p = qu.front();
    qu.pop();
    REP(dir, 4) {
      for (int k = 1; k <= K; k++) {
        ll nx = p.x + k * dx[dir];
        ll ny = p.y + k * dy[dir];

        if (nx < 0 or nx > H - 1 or ny < 0 or ny > W - 1)
          break;
        if (c[nx][ny] == '@')
          break;
        if (dist[nx][ny] != INF)
          continue;

        dist[nx][ny] = dist[p.x][p.y] + 1;
        qu.emplace(nx, ny);
      }
    }
  }

  // debug(dist, H, W);

  cout << (dist[x2][y2] == INF ? -1 : dist[x2][y2]) << endl;

  return 0;
}