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

#include <bits/stdc++.h>

using namespace std;

#define debug(n) cerr << #n << ':' << n << endl;
#define dline cerr << __LINE__ << endl;

using ll = long long;
template <class T, class U> using P = pair<T, U>;
template <class T> using Heap = priority_queue<T>;
template <class T> using heaP = priority_queue<T, vector<T>, greater<T>>;
template <class T, class U> using umap = unordered_map<T, U>;
template <class T> using uset = unordered_set<T>;

template <class T> bool ChangeMax(T &a, const T &b) {
  if (a >= b)
    return false;
  a = b;
  return true;
}

template <class T> bool ChangeMin(T &a, const T &b) {
  if (a <= b)
    return false;
  a = b;
  return true;
}

template <class T, size_t N, class U> void Fill(T (&a)[N], const U &v) {
  fill((U *)a, (U *)(a + N), v);
}

template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &e : v)
    is >> e;
  return is;
}

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  int sr, sc, gr, gc;
  cin >> sr >> sc >> gr >> gc;
  sr--;
  sc--;
  gr--;
  gc--;
  static string s[1000000];
  for (int i = 0; i < h; ++i)
    cin >> s[i];
  vector<vector<int>> dis(h, vector<int>(w, -1));
  int mr[] = {-1, 0, 1, 0};
  int mc[] = {0, 1, 0, -1};
  queue<P<int, int>> que;
  que.emplace(sr, sc);
  dis[sr][sc] = 0;
  while (!que.empty()) {
    auto now = que.front();
    que.pop();
    for (int i = 0; i < 4; ++i) {
      for (int j = 1; j <= k; ++j) {
        int nr = now.first + mr[i] * j;
        int nc = now.second + mc[i] * j;
        if (nr >= h || nr < 0 || nc >= w || nc < 0 || s[nr][nc] == '@')
          break;
        if (dis[nr][nc] != -1 && dis[nr][nc] <= dis[now.first][now.second])
          break;
        if (dis[nr][nc] == dis[now.first][now.second] + 1)
          continue;
        dis[nr][nc] = dis[now.first][now.second] + 1;
        que.emplace(nr, nc);
      }
    }
  }
  cout << dis[gr][gc] << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define debug(n) cerr << #n << ':' << n << endl;
#define dline cerr << __LINE__ << endl;

using ll = long long;
template <class T, class U> using P = pair<T, U>;
template <class T> using Heap = priority_queue<T>;
template <class T> using heaP = priority_queue<T, vector<T>, greater<T>>;
template <class T, class U> using umap = unordered_map<T, U>;
template <class T> using uset = unordered_set<T>;

template <class T> bool ChangeMax(T &a, const T &b) {
  if (a >= b)
    return false;
  a = b;
  return true;
}

template <class T> bool ChangeMin(T &a, const T &b) {
  if (a <= b)
    return false;
  a = b;
  return true;
}

template <class T, size_t N, class U> void Fill(T (&a)[N], const U &v) {
  fill((U *)a, (U *)(a + N), v);
}

template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &e : v)
    is >> e;
  return is;
}

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  int sr, sc, gr, gc;
  cin >> sr >> sc >> gr >> gc;
  sr--;
  sc--;
  gr--;
  gc--;
  static string s[1000000];
  for (int i = 0; i < h; ++i)
    cin >> s[i];
  vector<vector<int>> dis(h, vector<int>(w, -1));
  int mr[] = {-1, 0, 1, 0};
  int mc[] = {0, 1, 0, -1};
  queue<P<int, int>> que;
  que.emplace(sr, sc);
  dis[sr][sc] = 0;
  while (!que.empty()) {
    auto now = que.front();
    que.pop();
    for (int i = 0; i < 4; ++i) {
      for (int j = 1; j <= k; ++j) {
        int nr = now.first + mr[i] * j;
        int nc = now.second + mc[i] * j;
        if (nr >= h || nr < 0 || nc >= w || nc < 0 || s[nr][nc] == '@')
          break;
        if (dis[nr][nc] != -1 && dis[nr][nc] <= dis[now.first][now.second])
          break;
        dis[nr][nc] = dis[now.first][now.second] + 1;
        que.emplace(nr, nc);
      }
    }
  }
  cout << dis[gr][gc] << endl;
  return 0;
}