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

/**
 *    author:  boutarou
 *    created: 14.06.2020 21:50:21
 **/

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define fcout cout << fixed << setprecision(15)
using lint = long long;
using P = pair<int, int>;

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

const double PI = 3.1415926535897932;
const lint INF = 3e18;
const int dy[] = {0, 0, -1, 1};
const int dx[] = {1, -1, 0, 0};

int h, w, k;
int sy, sx, gy, gx;
vector<char> c[1000005];
vector<lint> shortest[1000005];
queue<P> que;

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

  cin >> h >> w >> k;
  cin >> sy >> sx >> gy >> gx;
  sy--, sx--, gy--, gx--;
  rep(i, h) {
    rep(j, w) { shortest[i].push_back(INF); }
  }
  rep(i, h) {
    rep(j, w) {
      char now;
      cin >> now;
      c[i].push_back(now);
      if (now == '@') {
        shortest[i][j] = -1;
      }
    }
  }

  shortest[sy][sx] = 0;
  que.push({sy, sx});
  while (!que.empty()) {
    P now = que.front();
    que.pop();
    int now_y = now.first, now_x = now.second;
    rep(i, 4) {
      int ny = now_y + dy[i], nx = now_x + dx[i];
      rep(j, k) {
        if (ny < 0 || ny >= h || nx < 0 || nx >= w)
          break;
        if (shortest[ny][nx] <= shortest[now_y][now_x])
          break;
        if (shortest[ny][nx] == shortest[now_y][now_x] + 1) {
          ny += dy[i], nx += dx[i];
          continue;
        }
        que.push({ny, nx});
        shortest[ny][nx] = shortest[now_y][now_x] + 1;
        ny += dy[i], nx += dx[i];
      }
    }
  }
  if (shortest[gy][gx] == INF) {
    cout << -1 << endl;
  } else {
    cout << shortest[gy][gx] << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

/**
 *    author:  boutarou
 *    created: 14.06.2020 21:50:21
 **/

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define fcout cout << fixed << setprecision(15)
using lint = long long;
using P = pair<int, int>;

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

const double PI = 3.1415926535897932;
const lint INF = 3e18;
const int dy[] = {0, 0, -1, 1};
const int dx[] = {1, -1, 0, 0};

int h, w, k;
int sy, sx, gy, gx;
vector<char> c[1000005];
vector<lint> shortest[1000005];
queue<P> que;

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

  cin >> h >> w >> k;
  cin >> sy >> sx >> gy >> gx;
  sy--, sx--, gy--, gx--;
  rep(i, h) {
    rep(j, w) { shortest[i].push_back(INF); }
  }
  rep(i, h) {
    rep(j, w) {
      char now;
      cin >> now;
      c[i].push_back(now);
      if (now == '@') {
        shortest[i][j] = -1;
      }
    }
  }

  shortest[sy][sx] = 0;
  que.push({sy, sx});
  while (!que.empty()) {
    P now = que.front();
    que.pop();
    int now_y = now.first, now_x = now.second;
    rep(i, 4) {
      int ny = now_y + dy[i], nx = now_x + dx[i];
      rep(j, k) {
        if (ny < 0 || ny >= h || nx < 0 || nx >= w)
          break;
        if (shortest[ny][nx] <= shortest[now_y][now_x])
          break;
        que.push({ny, nx});
        shortest[ny][nx] = shortest[now_y][now_x] + 1;
        ny += dy[i], nx += dx[i];
      }
    }
  }
  if (shortest[gy][gx] == INF) {
    cout << -1 << endl;
  } else {
    cout << shortest[gy][gx] << endl;
  }

  return 0;
}