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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define all(a) a.begin(), a.end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define pb push_back
#define debug(x) cerr << __LINE__ << ' ' << #x << ':' << x << '\\n'
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;
typedef complex<ld> com;
template <class T> using pque = priority_queue<T, vector<T>, greater<T>>;
constexpr int inf = 1000000010;
constexpr ll INF = 1000000000000000010;
constexpr int mod1e9 = 1000000007;
constexpr int mod998 = 998244353;
constexpr ld eps = 1e-12;
constexpr ld pi = 3.141592653589793238;
constexpr ll ten(int n) { return n ? 10 * ten(n - 1) : 1; };
int dx[] = {1, 0, -1, 0, 1, 1, -1, -1};
int dy[] = {0, 1, 0, -1, 1, -1, 1, -1};
void fail() {
  cout << "-1\\n";
  exit(0);
}
template <class T, class U> inline bool chmax(T &a, const U &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T, class U> inline bool chmin(T &a, const U &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> istream &operator>>(istream &s, vector<T> &v) {
  for (auto &e : v)
    s >> e;
  return s;
}
template <class T> ostream &operator<<(ostream &s, const vector<T> &v) {
  for (auto &e : v)
    s << e << ' ';
  return s;
}

struct fastio {
  fastio() {
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
    cerr << fixed << setprecision(20);
  }
} fastio_;

struct str {
  int cost, mode, x, y;
  bool operator>(const str &x) const { return cost > x.cost; }
};

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  int sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;
  sx--;
  sy--;
  gx--;
  gy--;
  vector<string> a(h);
  cin >> a;
  vector dis(h, vector(w, vector(2, inf)));
  pque<str> que;
  que.push({0, 0, sx, sy});
  que.push({0, 1, sx, sy});
  dis[sx][sy][0] = dis[sx][sy][1] = 0;
  while (!que.empty()) {
    str p = que.top();
    que.pop();
    if (p.cost > dis[p.x][p.y][p.mode])
      continue;
    rep(d, 4) {
      int nx = p.x + dx[d];
      int ny = p.y + dy[d];
      if (0 <= nx && nx < h && 0 <= ny && ny < w && a[nx][ny] == '.') {
        int nxt = p.cost + 1;
        if (p.mode == 0 && dx[d] == 0 || p.mode == 1 && dy[d] == 0) {
          nxt = p.cost;
          if (nxt % k == 0)
            nxt -= k;
          nxt -= p.cost % k;
          nxt += k + 1;
        }
        int m = 0;
        if (dy[d])
          m = 1;
        if (dis[nx][ny][m] > nxt) {
          dis[nx][ny][m] = nxt;
          que.push({nxt, m, nx, ny});
        }
      }
    }
  }
  int ans = min(dis[gx][gy][0], dis[gx][gy][1]);
  if (ans == inf)
    fail();
  ans = (ans + k - 1) / k;
  cout << ans << '\\n';
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define all(a) a.begin(), a.end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define pb push_back
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;
typedef complex<ld> com;
constexpr int inf = 1000000010;
constexpr ll INF = 1000000000000000010;
constexpr ld eps = 1e-12;
constexpr ld pi = 3.141592653589793238;
template <class T, class U> inline bool chmax(T &a, const U &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T, class U> inline bool chmin(T &a, const U &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

typedef pair<int, P> PP;

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);

  int h, w, k;
  cin >> h >> w >> k;
  int sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;
  sx--;
  sy--;
  gx--;
  gy--;
  vector<vector<char>> a(h, vector<char>(w));
  rep(i, h) rep(j, w) cin >> a[i][j];
  vector<vector<int>> dis(h, vector<int>(w, inf));
  dis[sx][sy] = 0;
  priority_queue<PP> que;
  que.push(PP(0, P(sx, sy)));
  while (!que.empty()) {
    PP p = que.top();
    que.pop();
    int cost = p.first;
    int nx = p.second.first;
    int ny = p.second.second;
    if (cost > dis[nx][ny])
      continue;
    int px = nx;
    int qx = nx;
    while (px < h - 1 && a[px + 1][ny] == '.' &&
           dis[px + 1][ny] > (abs(px + 1 - nx) + k - 1) / k + cost)
      px++;
    while (0 < qx && a[qx - 1][ny] == '.' &&
           dis[qx - 1][ny] > (abs(qx - 1 - nx) + k - 1) / k + cost)
      qx--;
    for (int i = qx; i <= px; i++) {
      int ab = abs(i - nx);
      ab = (ab + k - 1) / k;
      int ncost = cost + ab;
      if (ncost < dis[i][ny]) {
        dis[i][ny] = ncost;
        que.push(PP(ncost, P(i, ny)));
      }
    }
    int py = ny;
    int qy = ny;
    while (py < w - 1 && a[nx][py + 1] == '.' &&
           dis[nx][py + 1] > (abs(py + 1 - ny) + k - 1) / k + cost)
      py++;
    while (0 < qy && a[nx][qy - 1] == '.' &&
           dis[nx][qy - 1] > (abs(qy - 1 - ny) + k - 1) / k + cost)
      qy--;
    for (int i = qy; i <= py; i++) {
      int ab = abs(i - ny);
      ab = (ab + k - 1) / k;
      int ncost = cost + ab;
      if (ncost < dis[nx][i]) {
        dis[nx][i] = ncost;
        que.push(PP(ncost, P(nx, i)));
      }
    }
  }
  if (dis[gx][gy] == inf)
    cout << "-1\\n";
  else
    cout << dis[gx][gy] << '\\n';
}