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

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
// FILE *fi = freopen("1.txt", "r", stdin);
// FILE *fo = freopen("1", "w", stdout);
// FILE *fi = freopen("P.inp", "r", stdin);
// FILE *fo = freopen("P.out", "w", stdout);
typedef pair<int, int> ii;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int n, m, k;
struct Node {
  int x, y;
  int w;
};
struct cmp {
  bool operator()(Node a, Node b) { return a.w > b.w; }
};
string s[1000001];
ii S, T;
vector<vector<int>> Dist;
vector<vector<int>> vis;
void Dijktra() {
  Dist.resize(n + 10);
  for (int i = 1; i <= n; i++) {
    Dist[i].resize(m + 10);
  }
  vis = Dist;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      Dist[i][j] = 1e9;
    }
  }
  Dist[S.first][S.second] = 0;
  queue<Node> h;
  h.push({S.first, S.second, 0});
  while (h.size()) {
    Node x = h.front();
    h.pop();
    ii u = {x.x, x.y};
    int du = x.w;
    if (du != Dist[u.first][u.second])
      continue;
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k; j++) {
        ii v;
        v.first = u.first + dx[i] * j;
        v.second = u.second + dy[i] * j;
        int val = j / k + (j % k != 0);
        if (v.first < 1 || v.first > n || v.second < 1 || v.second > m ||
            s[v.first][v.second] == '@' ||
            Dist[v.first][v.second] <= Dist[u.first][u.second])
          break;
        if (vis[v.first][v.second])
          continue;
        vis[v.first][v.second] = true;
        Dist[v.first][v.second] = Dist[u.first][u.second] + 1;
        h.push({v.first, v.second, Dist[v.first][v.second]});
      }
    }
  }
  if (Dist[T.first][T.second] == 1e9) {
    cout << -1;
    return;
  }
  cout << Dist[T.first][T.second];
}
int main() {
  //------------------------------
  // Faster
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  //-------------------------------
  cin >> n >> m >> k;
  cin >> S.first >> S.second >> T.first >> T.second;
  for (int i = 1; i <= n; i++) {
    cin >> s[i];
    s[i] = '.' + s[i];
  }
  Dijktra();
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
// FILE *fi = freopen("1.txt", "r", stdin);
// FILE *fo = freopen("1", "w", stdout);
// FILE *fi = freopen("P.inp", "r", stdin);
// FILE *fo = freopen("P.out", "w", stdout);
typedef pair<int, int> ii;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int n, m, k;
struct Node {
  int x, y;
  int w;
};
struct cmp {
  bool operator()(Node a, Node b) { return a.w > b.w; }
};
string s[1000001];
ii S, T;
vector<vector<int>> Dist;
void Dijktra() {
  Dist.resize(n + 10);
  for (int i = 1; i <= n; i++) {
    Dist[i].resize(m + 10);
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      Dist[i][j] = 1e9;
    }
  }
  Dist[S.first][S.second] = 0;
  queue<Node> h;
  h.push({S.first, S.second, 0});
  while (h.size()) {
    Node x = h.front();
    h.pop();
    ii u = {x.x, x.y};
    int du = x.w;
    if (du != Dist[u.first][u.second])
      continue;
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k; j++) {
        ii v;
        v.first = u.first + dx[i] * j;
        v.second = u.second + dy[i] * j;
        int val = j / k + (j % k != 0);
        if (v.first < 1 || v.first > n || v.second < 1 || v.second > m ||
            s[v.first][v.second] == '@' ||
            Dist[v.first][v.second] <= Dist[u.first][u.second])
          break;
        Dist[v.first][v.second] = Dist[u.first][u.second] + 1;
        h.push({v.first, v.second, Dist[v.first][v.second]});
      }
    }
  }
  if (Dist[T.first][T.second] == 1e9) {
    cout << -1;
    return;
  }
  cout << Dist[T.first][T.second];
}
int main() {
  //------------------------------
  // Faster
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  //-------------------------------
  cin >> n >> m >> k;
  cin >> S.first >> S.second >> T.first >> T.second;
  for (int i = 1; i <= n; i++) {
    cin >> s[i];
    s[i] = '.' + s[i];
  }
  Dijktra();
  return 0;
}