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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

using ll = long long;
using P = pair<int, int>;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

template <class T> inline bool chmax(T &x, T y) {
  if (x < y) {
    x = y;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &x, T y) {
  if (x > y) {
    x = y;
    return true;
  }
  return false;
}

constexpr int dx[4] = {1, 0, -1, 0};
constexpr int dy[4] = {0, 1, 0, -1};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int H, W, K, sx, sy, tx, ty;
  cin >> H >> W >> K >> sx >> sy >> tx >> ty;
  --sx, --sy, --tx, --ty;
  vector<string> s(H);
  for (int i = 0; i < H; ++i)
    cin >> s[i];

  vector<vector<int>> dist(H, vector<int>(W, INF));
  using Data = tuple<int, int, int>;
  priority_queue<Data, vector<Data>, greater<Data>> que;
  dist[sx][sy] = 0;
  que.emplace(0, sx, sy);
  while (!que.empty()) {
    int d, x, y;
    tie(d, x, y) = que.top();
    que.pop();
    if (d > dist[x][y])
      continue;
    for (int i = 0; i < 4; ++i) {
      int nx = x, ny = y;
      for (int j = 0; j < K; ++j) {
        nx += dx[i], ny += dy[i];
        if (nx < 0 || nx >= H || ny < 0 || ny >= W)
          break;
        if (s[nx][ny] == '@')
          break;
        if (d + 1 > dist[nx][ny])
          break;
        if (chmin(dist[nx][ny], d + 1))
          que.emplace(dist[nx][ny], nx, ny);
      }
    }
  }
  if (dist[tx][ty] == INF)
    dist[tx][ty] = -1;
  cout << dist[tx][ty] << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

using ll = long long;
using P = pair<int, int>;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

template <class T> inline bool chmax(T &x, T y) {
  if (x < y) {
    x = y;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &x, T y) {
  if (x > y) {
    x = y;
    return true;
  }
  return false;
}

constexpr int dx[4] = {1, 0, -1, 0};
constexpr int dy[4] = {0, 1, 0, -1};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int H, W, K, sx, sy, tx, ty;
  cin >> H >> W >> K >> sx >> sy >> tx >> ty;
  --sx, --sy, --tx, --ty;
  vector<string> s(H);
  for (int i = 0; i < H; ++i)
    cin >> s[i];

  vector<vector<int>> dist(H, vector<int>(W, INF));
  using Data = tuple<int, int, int>;
  priority_queue<Data, vector<Data>, greater<Data>> que;
  dist[sx][sy] = 0;
  que.emplace(0, sx, sy);
  while (!que.empty()) {
    int d, x, y;
    tie(d, x, y) = que.top();
    que.pop();
    if (d > dist[x][y])
      continue;
    for (int i = 0; i < 4; ++i) {
      int nx = x, ny = y;
      for (int j = 0; j < K; ++j) {
        nx += dx[i], ny += dy[i];
        if (nx < 0 || nx >= H || ny < 0 || ny >= W)
          break;
        if (s[nx][ny] == '@')
          break;
        if (chmin(dist[nx][ny], dist[x][y] + 1))
          que.emplace(dist[nx][ny], nx, ny);
      }
    }
  }
  if (dist[tx][ty] == INF)
    dist[tx][ty] = -1;
  cout << dist[tx][ty] << endl;
}