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

#include <bits/stdc++.h>
using namespace std;
using LL = long long int;
#define incID(i, l, r) for (int i = (l); i < (r); ++i)
#define decID(i, l, r) for (int i = (r)-1; i >= (l); --i)
#define incII(i, l, r) for (int i = (l); i <= (r); ++i)
#define decII(i, l, r) for (int i = (r); i >= (l); --i)
#define inc(i, n) incID(i, 0, n)
#define dec(i, n) decID(i, 0, n)
#define inc1(i, n) incII(i, 1, n)
#define dec1(i, n) decII(i, 1, n)
#define inID(v, l, r) ((l) <= (v) && (v) < (r))
#define inII(v, l, r) ((l) <= (v) && (v) <= (r))
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define MT make_tuple
#define FI first
#define SE second
#define FR front()
#define BA back()
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
auto setmin = [](auto &a, auto b) { return (b < a ? a = b, true : false); };
auto setmax = [](auto &a, auto b) { return (b > a ? a = b, true : false); };
auto setmineq = [](auto &a, auto b) { return (b <= a ? a = b, true : false); };
auto setmaxeq = [](auto &a, auto b) { return (b >= a ? a = b, true : false); };
#define SI(v) static_cast<int>(v.size())
#define RF(e, v) for (auto &e : v)
#define until(e) while (!(e))
#define if_not(e) if (!(e))
#define ef else if
#define UR assert(false)
#define IN(T, ...)                                                             \\
  T __VA_ARGS__;                                                               \\
  IN_(__VA_ARGS__);
void IN_(){};
template <typename T, typename... U> void IN_(T &a, U &...b) {
  cin >> a;
  IN_(b...);
};
template <typename T> void OUT(T &&a) { cout << a << endl; }
template <typename T, typename... U> void OUT(T &&a, U &&...b) {
  cout << a << " ";
  OUT(b...);
}

// ---- ----

template <typename T> T MV(T v) { return v; }
template <typename T, typename... U> auto MV(T v, int a, U... b) {
  return vector<decltype(MV(v, b...))>(a, MV(v, b...));
}

auto fl = [](auto a, auto b) {
  assert(b != 0);
  return a / b - (a % b != 0 && ((a >= 0) != (b >= 0)) ? 1 : 0);
};
auto ce = [](auto a, auto b) {
  assert(b != 0);
  return a / b + (a % b != 0 && ((a >= 0) == (b >= 0)) ? 1 : 0);
};
auto mo = [](auto a, auto b) {
  assert(b != 0);
  a %= b;
  if (a < 0) {
    a += abs(b);
  }
  return a;
};

int main() {
  IN(int, h, w, k, si, sj, gi, gj);
  vector<string> s(h + 2);
  s.FR = s.BA = string(w + 2, '@');
  inc1(i, h) {
    IN(string, t);
    s[i] = "@" + t + "@";
  }
  h += 2;
  w += 2;

  const int INF = 1e7;
  auto dist = MV(INF, 2, h, w);
  queue<tuple<int, int, int, int>> q;
  auto update = [&](int v, int i, int j, int d) {
    if (setmin(dist[v][i][j], d)) {
      q.emplace(v, i, j, d);
    }
  };
  update(0, si, sj, 0);
  update(1, si, sj, 0);
  until(q.empty()) {
    auto [v, i, j, d] = q.front();
    q.pop();
    if (dist[v][i][j] != d) {
      continue;
    }

    vector<int> di = {+1, -1, 0, 0};
    vector<int> dj = {0, 0, +1, -1};
    inc(l, 4) {
      int ii = i + di[l];
      int jj = j + dj[l];
      if_not(inID(ii, 0, h) && inID(jj, 0, w)) { continue; }
      if (s[ii][jj] == '@') {
        continue;
      }
      update(l / 2, ii, jj, (l / 2 == v ? d + 1 : ce(d, k) * k + 1));
    }
  }
  int ans = min(dist[0][gi][gj], dist[1][gi][gj]);
  OUT(ans == INF ? -1 : ce(ans, k));
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using LL = long long int;
#define incID(i, l, r) for (int i = (l); i < (r); ++i)
#define decID(i, l, r) for (int i = (r)-1; i >= (l); --i)
#define incII(i, l, r) for (int i = (l); i <= (r); ++i)
#define decII(i, l, r) for (int i = (r); i >= (l); --i)
#define inc(i, n) incID(i, 0, n)
#define dec(i, n) decID(i, 0, n)
#define inc1(i, n) incII(i, 1, n)
#define dec1(i, n) decII(i, 1, n)
#define inID(v, l, r) ((l) <= (v) && (v) < (r))
#define inII(v, l, r) ((l) <= (v) && (v) <= (r))
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define MT make_tuple
#define FI first
#define SE second
#define FR front()
#define BA back()
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
auto setmin = [](auto &a, auto b) { return (b < a ? a = b, true : false); };
auto setmax = [](auto &a, auto b) { return (b > a ? a = b, true : false); };
auto setmineq = [](auto &a, auto b) { return (b <= a ? a = b, true : false); };
auto setmaxeq = [](auto &a, auto b) { return (b >= a ? a = b, true : false); };
#define SI(v) static_cast<int>(v.size())
#define RF(e, v) for (auto &e : v)
#define until(e) while (!(e))
#define if_not(e) if (!(e))
#define ef else if
#define UR assert(false)
#define IN(T, ...)                                                             \\
  T __VA_ARGS__;                                                               \\
  IN_(__VA_ARGS__);
void IN_(){};
template <typename T, typename... U> void IN_(T &a, U &...b) {
  cin >> a;
  IN_(b...);
};
template <typename T> void OUT(T &&a) { cout << a << endl; }
template <typename T, typename... U> void OUT(T &&a, U &&...b) {
  cout << a << " ";
  OUT(b...);
}

// ---- ----

template <typename T> istream &operator>>(istream &s, vector<T> &v) {
  RF(e, v) { s >> e; }
  return s;
}
template <typename T> ostream &operator<<(ostream &s, vector<T> const &v) {
  inc(i, SI(v)) { s << (i == 0 ? "" : " ") << v[i]; }
  return s;
}

template <typename T> T MV(T v) { return v; }
template <typename T, typename... U> auto MV(T v, int a, U... b) {
  return vector<decltype(MV(v, b...))>(a, MV(v, b...));
}

int main() {
  IN(int, h, w, k, si, sj, gi, gj);
  si--;
  sj--;
  gi--;
  gj--;
  vector<string> s(h);
  cin >> s;

  const int INF = 1e7;
  auto tate = MV(INF, h, w);
  auto yoko = MV(INF, h, w);
  tate[si][sj] = 0;
  yoko[si][sj] = 0;

  queue<tuple<int, int, int, int>> q;
  q.emplace(si, sj, 0, 0);
  until(q.empty()) {
    auto [i, j, t, y] = q.front();
    q.pop();
    if_not(tate[i][j] == t && yoko[i][j] == y) { continue; }

    int d = min(t, y);
    inc1(l, k) {
      int ii = i + l;
      if_not(inID(ii, 0, h)) { break; }
      if (s[ii][j] == '@') {
        break;
      }
      auto &T = tate[ii][j];
      auto &Y = yoko[ii][j];
      bool f = setmin(T, d + 1);
      bool g = setmin(Y, d + 2);
      if (f || g) {
        q.emplace(ii, j, T, Y);
      }
    }
    inc1(l, k) {
      int ii = i - l;
      if_not(inID(ii, 0, h)) { break; }
      if (s[ii][j] == '@') {
        break;
      }
      auto &T = tate[ii][j];
      auto &Y = yoko[ii][j];
      bool f = setmin(T, d + 1);
      bool g = setmin(Y, d + 2);
      if (f || g) {
        q.emplace(ii, j, T, Y);
      }
    }

    inc1(l, k) {
      int jj = j + l;
      if_not(inID(jj, 0, w)) { break; }
      if (s[i][jj] == '@') {
        break;
      }
      auto &T = tate[i][jj];
      auto &Y = yoko[i][jj];
      bool f = setmin(T, d + 2);
      bool g = setmin(Y, d + 1);
      if (f || g) {
        q.emplace(i, jj, T, Y);
      }
    }
    inc1(l, k) {
      int jj = j - l;
      if_not(inID(jj, 0, w)) { break; }
      if (s[i][jj] == '@') {
        break;
      }
      auto &T = tate[i][jj];
      auto &Y = yoko[i][jj];
      bool f = setmin(T, d + 2);
      bool g = setmin(Y, d + 1);
      if (f || g) {
        q.emplace(i, jj, T, Y);
      }
    }
  }
  int ans = min(yoko[gi][gj], tate[gi][gj]);
  if (ans == INF) {
    ans = -1;
  }
  OUT(ans);
}