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

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n) for (int i = 1; i < n; i++)
#define rev(i, n) for (int i = n - 1; i >= 0; i--)
#define all(v) v.begin(), v.end()
#define P pair<int, int>
#define len(s) (int)s.size()
#define pb push_back

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
constexpr int mod = 1e9 + 7;
constexpr int inf = 3e18;

int H, W, K;
int sx, sy, gx, gy;
vector<char> c[1000005];
vector<int> dis[1000005][4];
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
vector<int> l[1000005], r[1000005], u[1000005], d[1000005];
signed main() {
  cin >> H >> W >> K;
  cin >> sx >> sy >> gx >> gy;
  sx--;
  sy--;
  gx--;
  gy--;
  rep(i, H) {
    c[i].resize(W);
    l[i].resize(W);
    r[i].resize(W);
    u[i].resize(W);
    d[i].resize(W);
    rep(j, 4) dis[i][j].resize(W, inf);
  }
  rep(i, H) rep(j, W) cin >> c[i][j];
  rep(i, H) {
    int now = -1;
    rep(j, W) {
      if (c[i][j] == '@')
        now = -1;
      if (now == -1 && c[i][j] == '.')
        now = j;
      l[i][j] = now;
    }
    now = -1;
    for (int j = W - 1; j >= 0; j--) {
      if (c[i][j] == '@')
        now = -1;
      if (now == -1 && c[i][j] == '.')
        now = j;
      r[i][j] = now;
    }
  }
  rep(j, W) {
    int now = -1;
    rep(i, H) {
      if (c[i][j] == '@')
        now = -1;
      if (now == -1 && c[i][j] == '.')
        now = i;
      u[i][j] = now;
    }
    now = -1;
    for (int i = H - 1; i >= 0; i--) {
      if (c[i][j] == '@')
        now = -1;
      if (now == -1 && c[i][j] == '.')
        now = i;
      d[i][j] = now;
    }
  }
  queue<pair<int, P>> que;
  rep(i, 4) que.push({i, {sx, sy}});
  rep(i, 4) dis[sx][i][sy] = 0;
  while (!que.empty()) {
    pair<int, P> q = que.front();
    que.pop();
    rep(i, 4) {
      int gen;
      if (i == 0)
        gen = d[q.second.first][q.second.second] - q.second.first;
      else if (i == 1)
        gen = r[q.second.first][q.second.second] - q.second.second;
      else if (i == 2)
        gen = q.second.first - u[q.second.first][q.second.second];
      else
        gen = q.second.second - l[q.second.first][q.second.second];
      chmin(gen, K);
      for (int j = gen; j > 0; j--) {
        int nx = q.second.first + dx[i] * j;
        int ny = q.second.second + dy[i] * j;
        if (nx < 0 || ny < 0 || nx >= H || ny >= W)
          break;
        if (c[nx][ny] == '@')
          break;
        int ne =
            dis[q.second.first][q.first][q.second.second] + (j + K - 1) / K;
        if (dis[nx][i][ny] <= ne)
          break;
        dis[nx][i][ny] = ne;
        que.push({i, {nx, ny}});
      }
    }
  }
  /*rep(i,H){
          rep(j,W)cout<<dis[i][j]<<" ";
          cout<<endl;
  }*/
  int mn = inf;
  rep(i, 4) chmin(mn, dis[gx][i][gy]);
  if (mn == inf)
    cout << -1 << endl;
  else
    cout << mn << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n) for (int i = 1; i < n; i++)
#define rev(i, n) for (int i = n - 1; i >= 0; i--)
#define all(v) v.begin(), v.end()
#define P pair<int, int>
#define len(s) (int)s.size()
#define pb push_back

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
constexpr int mod = 1e9 + 7;
constexpr int inf = 3e18;

int H, W, K;
int sx, sy, gx, gy;
vector<char> c[1000005];
vector<int> dis[1000005][4];
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
signed main() {
  cin >> H >> W >> K;
  cin >> sx >> sy >> gx >> gy;
  sx--;
  sy--;
  gx--;
  gy--;
  rep(i, H) {
    c[i].resize(W);
    rep(j, 4) dis[i][j].resize(W, inf);
  }
  rep(i, H) rep(j, W) cin >> c[i][j];
  queue<pair<int, P>> que;
  rep(i, 4) que.push({i, {sx, sy}});
  rep(i, 4) dis[sx][i][sy] = 0;
  while (!que.empty()) {
    pair<int, P> q = que.front();
    que.pop();
    rep(i, 4) {
      REP(j, K + 1) {
        int nx = q.second.first + dx[i] * j;
        int ny = q.second.second + dy[i] * j;
        if (nx < 0 || ny < 0 || nx >= H || ny >= W)
          break;
        if (c[nx][ny] == '@')
          break;
        int ne =
            dis[q.second.first][q.first][q.second.second] + (j + K - 1) / K;
        if (dis[nx][i][ny] < ne)
          break;
        dis[nx][i][ny] = ne;
        que.push({i, {nx, ny}});
      }
    }
  }
  /*rep(i,H){
          rep(j,W)cout<<dis[i][j]<<" ";
          cout<<endl;
  }*/
  int mn = inf;
  rep(i, 4) chmin(mn, dis[gx][i][gy]);
  if (mn == inf)
    cout << -1 << endl;
  else
    cout << mn << endl;
}