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

#include <algorithm>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define eprintf(...) 42
#endif
using ll = long long;
using vll = std::vector<ll>;
using vvll = std::vector<vll>;
using vvvll = std::vector<vvll>;
#define reps(i, S, E) for (ll i = (S); i <= (E); i++)
#define rep(i, N) reps(i, 0, N - 1)
#define deps(i, E, S) for (ll i = (E); i >= (S); i--)
#define dep(i, N) deps(i, N - 1, 0)
const ll INF = 1LL << 60;
const int INF_INT = 1 << 30;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
struct mll {
  static ll MOD;
  ll val;
  mll(ll v = 0) : val(v % MOD) {
    if (val < 0)
      val += MOD;
  }
  mll operator-() const { return -val; }
  mll operator+(const mll &b) const { return val + b.val; }
  mll operator-(const mll &b) const { return val - b.val; }
  mll operator*(const mll &b) const { return val * b.val; }
  mll operator/(const mll &b) const { return mll(*this) /= b; }
  mll operator+(ll b) const { return *this + mll(b); }
  mll operator-(ll b) const { return *this - mll(b); }
  mll operator*(ll b) const { return *this * mll(b); }
  friend mll operator+(ll a, const mll &b) { return b + a; }
  friend mll operator-(ll a, const mll &b) { return -b + a; }
  friend mll operator*(ll a, const mll &b) { return b * a; }
  mll &operator+=(const mll &b) {
    val = (val + b.val) % MOD;
    return *this;
  }
  mll &operator-=(const mll &b) {
    val = (val + MOD - b.val) % MOD;
    return *this;
  }
  mll &operator*=(const mll &b) {
    val = (val * b.val) % MOD;
    return *this;
  }
  mll &operator/=(const mll &b) {
    ll c = b.val, d = MOD, u = 1, v = 0;
    while (d) {
      ll t = c / d;
      c -= t * d;
      swap(c, d);
      u -= t * v;
      swap(u, v);
    }
    val = val * u % MOD;
    if (val < 0)
      val += MOD;
    return *this;
  }
  mll &operator+=(ll b) { return *this += mll(b); }
  mll &operator-=(ll b) { return *this -= mll(b); }
  mll &operator*=(ll b) { return *this *= mll(b); }
  mll &operator/=(ll b) { return *this /= mll(b); }
  bool operator==(const mll &b) { return val == b.val; }
  bool operator!=(const mll &b) { return val != b.val; }
  bool operator==(ll b) { return *this == mll(b); }
  bool operator!=(ll b) { return *this != mll(b); }
  friend bool operator==(ll a, const mll &b) { return mll(a) == b.val; }
  friend bool operator!=(ll a, const mll &b) { return mll(a) != b.val; }
  friend ostream &operator<<(ostream &os, const mll &a) { return os << a.val; }
  friend istream &operator>>(istream &is, mll &a) { return is >> a.val; }
  static mll Combination(ll a, ll b) {
    chmin(b, a - b);
    if (b < 0)
      return mll(0);
    mll c = 1;
    rep(i, b) c *= a - i;
    rep(i, b) c /= i + 1;
    return c;
  }
};
using vmll = std::vector<mll>;
using vvmll = std::vector<vmll>;
using vvvmll = std::vector<vvmll>;
using vvvvmll = std::vector<vvvmll>;

struct Fast {
  Fast() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(std::numeric_limits<double>::max_digits10);
  }
} fast; // cin,cout高速化のおまじない＋桁数指定

ll mll::MOD = (ll)(1e9 + 7); // 998244353ll;

//--for debug--
#ifdef LOCAL
void _debug_out_(string s = "") {
  if (s != "") {
    eprintf(s.c_str());
  } else {
    eprintf("\\n");
  }
}
void _debug_out_(ll x, string s = "") {
  if (s != "") {
    eprintf(s.c_str(), x);
  } else {
    eprintf("%lld\\n", x);
  }
}
void _debug_out_(mll x, string s = "") {
  if (s != "") {
    eprintf(s.c_str(), x);
  } else {
    eprintf("%lld\\n", x.val);
  }
}
void _debug_out_(vvll x, string s = "") {
  string t = s;
  if (s == "") {
    ll maxLen = 0;
    rep(i, (ll)x.size()) rep(j, (ll)x[i].size()) {
      ll n = x[i][j] / 10, len = 1;
      while (n > 0) {
        len++;
        n /= 10;
      }
      chmax(maxLen, len);
    }
    t = "%" + to_string(maxLen) + "lld ";
  }
  rep(i, (ll)x.size()) {
    rep(j, (ll)x[i].size()) { eprintf(t.c_str(), x[i][j]); }
    eprintf("\\n");
  }
  eprintf("\\n");
}
void _debug_out_(vll x, string s = "") {
  rep(i, (ll)x.size()) {
    if (s != "") {
      eprintf(s.c_str(), x[i]);
    } else {
      eprintf("%lld ", x[i]);
    }
  }
  eprintf("\\n");
}
void _debug_out_(vector<bool> x) {
  rep(i, (ll)x.size()) { eprintf("%s", x[i] ? "o" : "x"); }
  eprintf("\\n");
}
void _debug_out_(vector<vector<bool>> x) {
  rep(i, (ll)x.size()) {
    rep(j, (ll)x[i].size()) { eprintf("%s", x[i][j] ? "o" : "x"); }
    eprintf("\\n");
  }
  eprintf("\\n");
}
void _debug_out_(map<ll, ll> x) {
  auto begin = x.begin(), end = x.end();
  for (auto iter = begin; iter != end; iter++) {
    eprintf("%lld:%lld ", iter->first, iter->second);
  }
  eprintf("\\n");
}
void _debug_out_(map<char, ll> x) {
  auto begin = x.begin(), end = x.end();
  for (auto iter = begin; iter != end; iter++) {
    eprintf("%c:%lld ", iter->first, iter->second);
  }
  eprintf("\\n");
}
#define debug_out(...) _debug_out_(__VA_ARGS__)
#else
#define debug_out(...) 42
#endif
//!--for debug--

void solve() {
  ll H;
  cin >> H;
  ll W;
  cin >> W;
  ll K;
  cin >> K;
  ll Y1;
  cin >> Y1;
  ll X1;
  cin >> X1;
  ll Y2;
  cin >> Y2;
  ll X2;
  cin >> X2;
  vector<string> S(H);
  rep(i, H) { cin >> S[i]; }
  X1--;
  X2--;
  Y1--;
  Y2--;

  vvll d(H, vll(W, -1));
  queue<pair<ll, ll>> q;

  d[Y1][X1] = 0;
  q.emplace(Y1, X1);
  while (q.empty() == false) {
    auto curr = q.front();
    q.pop();

    // ↓
    reps(i, 1, K) {
      if (curr.first + i >= H) {
        break;
      }
      if (S[curr.first + i][curr.second] == '@') {
        break;
      }
      // 既に探索済み
      if (d[curr.first + i][curr.second] >= 0) {
        if (d[curr.first + i][curr.second] <= d[curr.first][curr.second]) {
          break;
        } else {
          continue;
        }
      }
      // キューに入れて探索継続
      d[curr.first + i][curr.second] = d[curr.first][curr.second] + 1;
      q.emplace(curr.first + i, curr.second);
    }

    // →
    reps(i, 1, K) {
      if (curr.second + i >= W) {
        break;
      }
      if (S[curr.first][curr.second + i] == '@') {
        break;
      }
      // 既に探索済み
      if (d[curr.first][curr.second + i] >= 0) {
        if (d[curr.first][curr.second + i] <= d[curr.first][curr.second]) {
          break;
        } else {
          continue;
        }
      }
      // キューに入れて探索継続
      d[curr.first][curr.second + i] = d[curr.first][curr.second] + 1;
      q.emplace(curr.first, curr.second + i);
    }

    // ↑
    reps(i, 1, K) {
      if (curr.first - i < 0) {
        break;
      }
      if (S[curr.first - i][curr.second] == '@') {
        break;
      }
      // 既に探索済み
      if (d[curr.first - i][curr.second] >= 0) {
        if (d[curr.first - i][curr.second] <= d[curr.first][curr.second]) {
          break;
        } else {
          continue;
        }
      }
      // キューに入れて探索継続
      d[curr.first - i][curr.second] = d[curr.first][curr.second] + 1;
      q.emplace(curr.first - i, curr.second);
    }

    // ←
    reps(i, 1, K) {
      if (curr.second - i < 0) {
        break;
      }
      if (S[curr.first][curr.second - i] == '@') {
        break;
      }
      // 既に探索済み
      if (d[curr.first][curr.second - i] >= 0) {
        if (d[curr.first][curr.second - 1] <= d[curr.first][curr.second]) {
          break;
        } else {
          continue;
        }
      }
      // キューに入れて探索継続
      d[curr.first][curr.second - i] = d[curr.first][curr.second] + 1;
      q.emplace(curr.first, curr.second - i);
    }

    // debug_out(d);
  }

  ll result = d[Y2][X2];

  cout << result << endl;
}

int main() {
#if TEST && LOCAL
  ll t;
  cin >> t;
  rep(i, t) { solve(); }
#else
  solve();
#endif
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define eprintf(...) 42
#endif
using ll = long long;
using vll = std::vector<ll>;
using vvll = std::vector<vll>;
using vvvll = std::vector<vvll>;
#define reps(i, S, E) for (ll i = (S); i <= (E); i++)
#define rep(i, N) reps(i, 0, N - 1)
#define deps(i, E, S) for (ll i = (E); i >= (S); i--)
#define dep(i, N) deps(i, N - 1, 0)
const ll INF = 1LL << 60;
const int INF_INT = 1 << 30;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
struct mll {
  static ll MOD;
  ll val;
  mll(ll v = 0) : val(v % MOD) {
    if (val < 0)
      val += MOD;
  }
  mll operator-() const { return -val; }
  mll operator+(const mll &b) const { return val + b.val; }
  mll operator-(const mll &b) const { return val - b.val; }
  mll operator*(const mll &b) const { return val * b.val; }
  mll operator/(const mll &b) const { return mll(*this) /= b; }
  mll operator+(ll b) const { return *this + mll(b); }
  mll operator-(ll b) const { return *this - mll(b); }
  mll operator*(ll b) const { return *this * mll(b); }
  friend mll operator+(ll a, const mll &b) { return b + a; }
  friend mll operator-(ll a, const mll &b) { return -b + a; }
  friend mll operator*(ll a, const mll &b) { return b * a; }
  mll &operator+=(const mll &b) {
    val = (val + b.val) % MOD;
    return *this;
  }
  mll &operator-=(const mll &b) {
    val = (val + MOD - b.val) % MOD;
    return *this;
  }
  mll &operator*=(const mll &b) {
    val = (val * b.val) % MOD;
    return *this;
  }
  mll &operator/=(const mll &b) {
    ll c = b.val, d = MOD, u = 1, v = 0;
    while (d) {
      ll t = c / d;
      c -= t * d;
      swap(c, d);
      u -= t * v;
      swap(u, v);
    }
    val = val * u % MOD;
    if (val < 0)
      val += MOD;
    return *this;
  }
  mll &operator+=(ll b) { return *this += mll(b); }
  mll &operator-=(ll b) { return *this -= mll(b); }
  mll &operator*=(ll b) { return *this *= mll(b); }
  mll &operator/=(ll b) { return *this /= mll(b); }
  bool operator==(const mll &b) { return val == b.val; }
  bool operator!=(const mll &b) { return val != b.val; }
  bool operator==(ll b) { return *this == mll(b); }
  bool operator!=(ll b) { return *this != mll(b); }
  friend bool operator==(ll a, const mll &b) { return mll(a) == b.val; }
  friend bool operator!=(ll a, const mll &b) { return mll(a) != b.val; }
  friend ostream &operator<<(ostream &os, const mll &a) { return os << a.val; }
  friend istream &operator>>(istream &is, mll &a) { return is >> a.val; }
  static mll Combination(ll a, ll b) {
    chmin(b, a - b);
    if (b < 0)
      return mll(0);
    mll c = 1;
    rep(i, b) c *= a - i;
    rep(i, b) c /= i + 1;
    return c;
  }
};
using vmll = std::vector<mll>;
using vvmll = std::vector<vmll>;
using vvvmll = std::vector<vvmll>;
using vvvvmll = std::vector<vvvmll>;

struct Fast {
  Fast() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(std::numeric_limits<double>::max_digits10);
  }
} fast; // cin,cout高速化のおまじない＋桁数指定

ll mll::MOD = (ll)(1e9 + 7); // 998244353ll;

//--for debug--
#ifdef LOCAL
void _debug_out_(string s = "") {
  if (s != "") {
    eprintf(s.c_str());
  } else {
    eprintf("\\n");
  }
}
void _debug_out_(ll x, string s = "") {
  if (s != "") {
    eprintf(s.c_str(), x);
  } else {
    eprintf("%lld\\n", x);
  }
}
void _debug_out_(mll x, string s = "") {
  if (s != "") {
    eprintf(s.c_str(), x);
  } else {
    eprintf("%lld\\n", x.val);
  }
}
void _debug_out_(vvll x, string s = "") {
  string t = s;
  if (s == "") {
    ll maxLen = 0;
    rep(i, (ll)x.size()) rep(j, (ll)x[i].size()) {
      ll n = x[i][j] / 10, len = 1;
      while (n > 0) {
        len++;
        n /= 10;
      }
      chmax(maxLen, len);
    }
    t = "%" + to_string(maxLen) + "lld ";
  }
  rep(i, (ll)x.size()) {
    rep(j, (ll)x[i].size()) { eprintf(t.c_str(), x[i][j]); }
    eprintf("\\n");
  }
  eprintf("\\n");
}
void _debug_out_(vll x, string s = "") {
  rep(i, (ll)x.size()) {
    if (s != "") {
      eprintf(s.c_str(), x[i]);
    } else {
      eprintf("%lld ", x[i]);
    }
  }
  eprintf("\\n");
}
void _debug_out_(vector<bool> x) {
  rep(i, (ll)x.size()) { eprintf("%s", x[i] ? "o" : "x"); }
  eprintf("\\n");
}
void _debug_out_(vector<vector<bool>> x) {
  rep(i, (ll)x.size()) {
    rep(j, (ll)x[i].size()) { eprintf("%s", x[i][j] ? "o" : "x"); }
    eprintf("\\n");
  }
  eprintf("\\n");
}
void _debug_out_(map<ll, ll> x) {
  auto begin = x.begin(), end = x.end();
  for (auto iter = begin; iter != end; iter++) {
    eprintf("%lld:%lld ", iter->first, iter->second);
  }
  eprintf("\\n");
}
void _debug_out_(map<char, ll> x) {
  auto begin = x.begin(), end = x.end();
  for (auto iter = begin; iter != end; iter++) {
    eprintf("%c:%lld ", iter->first, iter->second);
  }
  eprintf("\\n");
}
#define debug_out(...) _debug_out_(__VA_ARGS__)
#else
#define debug_out(...) 42
#endif
//!--for debug--

void solve() {
  ll H;
  cin >> H;
  ll W;
  cin >> W;
  ll K;
  cin >> K;
  ll Y1;
  cin >> Y1;
  ll X1;
  cin >> X1;
  ll Y2;
  cin >> Y2;
  ll X2;
  cin >> X2;
  vector<string> S(H);
  rep(i, H) { cin >> S[i]; }
  X1--;
  X2--;
  Y1--;
  Y2--;

  vvll d(H, vll(W, -1));
  queue<pair<ll, ll>> q;

  d[Y1][X1] = 0;
  q.emplace(Y1, X1);
  while (q.empty() == false) {
    auto curr = q.front();
    q.pop();

    // ↓
    reps(i, 1, K) {
      if (curr.first + i >= H) {
        break;
      }
      if (S[curr.first + i][curr.second] == '@') {
        break;
      }
      // 既に探索済み
      if (d[curr.first + i][curr.second] >= 0) {
        continue;
      }
      // キューに入れて探索継続
      d[curr.first + i][curr.second] = d[curr.first][curr.second] + 1;
      q.emplace(curr.first + i, curr.second);
    }

    // →
    reps(i, 1, K) {
      if (curr.second + i >= W) {
        break;
      }
      if (S[curr.first][curr.second + i] == '@') {
        break;
      }
      // 既に探索済み
      if (d[curr.first][curr.second + i] >= 0) {
        continue;
      }
      // キューに入れて探索継続
      d[curr.first][curr.second + i] = d[curr.first][curr.second] + 1;
      q.emplace(curr.first, curr.second + i);
    }

    // ↑
    reps(i, 1, K) {
      if (curr.first - i < 0) {
        break;
      }
      if (S[curr.first - i][curr.second] == '@') {
        break;
      }
      // 既に探索済み
      if (d[curr.first - i][curr.second] >= 0) {
        continue;
      }
      // キューに入れて探索継続
      d[curr.first - i][curr.second] = d[curr.first][curr.second] + 1;
      q.emplace(curr.first - i, curr.second);
    }

    // ←
    reps(i, 1, K) {
      if (curr.second - i < 0) {
        break;
      }
      if (S[curr.first][curr.second - i] == '@') {
        break;
      }
      // 既に探索済み
      if (d[curr.first][curr.second - i] >= 0) {
        continue;
      }
      // キューに入れて探索継続
      d[curr.first][curr.second - i] = d[curr.first][curr.second] + 1;
      q.emplace(curr.first, curr.second - i);
    }

    // debug_out(d);
  }

  ll result = d[Y2][X2];

  cout << result << endl;
}

int main() {
#if TEST && LOCAL
  ll t;
  cin >> t;
  rep(i, t) { solve(); }
#else
  solve();
#endif
  return 0;
}