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

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define int long long
#define endl "\\n"

constexpr long long INF = (long long)1e18;
constexpr long long MOD = 1'000'000'007;

struct fast_io {
  fast_io() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);
  };
} fio;

int H, W, K;
vector<string> c;

int dijkstra(int sy, int sx, int gy, int gx) {
  priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>,
                 greater<pair<int, pair<int, int>>>>
      q;

  vector<vector<int>> d(H, vector<int>(W, INF));

  d[sy][sx] = 0;
  q.push({0, {sy, sx}});

  while (!q.empty()) {
    pair<int, pair<int, int>> p = q.top();
    q.pop();
    int x = p.second.second;
    int y = p.second.first;
    int cost = p.first;

    if (d[y][x] < cost)
      continue;

    if (y == gy && gx == x)
      return cost;

    {
      for (int i = x + 1; i < W; i++) {
        if (c[y][i] == '@')
          break;
        if (d[y][i] <= d[y][x])
          break;
        int ncost = (i - x - 1) / K + 1;

        if (ncost >= 2)
          break;

        if (ncost + cost < d[y][i]) {
          d[y][i] = ncost + cost;
          q.push({d[y][i], {y, i}});
        }
      }

      for (int i = x - 1; i >= 0; i--) {
        if (c[y][i] == '@')
          break;
        if (d[y][i] <= d[y][x])
          break;

        int ncost = (x - i - 1) / K + 1;

        if (ncost >= 2)
          break;
        if (ncost + cost < d[y][i]) {
          d[y][i] = ncost + cost;
          q.push({d[y][i], {y, i}});
        }
      }
    }

    {
      for (int i = y + 1; i < H; i++) {
        if (c[i][x] == '@')
          break;
        if (d[i][x] <= d[y][x])
          break;

        int ncost = (i - y - 1) / K + 1;
        if (ncost >= 2)
          break;
        if (ncost + cost < d[i][x]) {
          d[i][x] = ncost + cost;
          q.push({d[i][x], {i, x}});
        }
      }

      for (int i = y - 1; i >= 0; i--) {
        if (c[i][x] == '@')
          break;
        if (d[i][x] <= d[y][x])
          break;

        int ncost = (y - i - 1) / K + 1;
        if (ncost >= 2)
          break;
        if (ncost + cost < d[i][x]) {
          d[i][x] = ncost + cost;
          q.push({d[i][x], {i, x}});
        }
      }
    }
  }

  return -1;
}

signed main() {
  cout << fixed << setprecision(10);

  int x1, y1, x2, y2;

  cin >> H >> W >> K;

  cin >> x1 >> y1 >> x2 >> y2;

  x1--;
  y1--;
  x2--;
  y2--;

  c.resize(H);

  for (int i = 0; i < H; i++) {
    cin >> c[i];
  }
  cout << dijkstra(x1, y1, x2, y2) << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define int long long
#define endl "\\n"

constexpr long long INF = (long long)1e18;
constexpr long long MOD = 1'000'000'007;

struct fast_io {
  fast_io() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);
  };
} fio;

int H, W, K;
vector<string> c;

int dijkstra(int sy, int sx, int gy, int gx) {
  priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>,
                 greater<pair<int, pair<int, int>>>>
      q;

  vector<vector<int>> d(H, vector<int>(W, INF));

  d[sy][sx] = 0;
  q.push({0, {sy, sx}});

  while (!q.empty()) {
    pair<int, pair<int, int>> p = q.top();
    q.pop();
    int x = p.second.second;
    int y = p.second.first;
    int cost = p.first;

    if (d[y][x] < cost)
      continue;

    if (y == gy && gx == x)
      return cost;

    {
      for (int i = x + 1; i < W; i++) {
        if (c[y][i] == '@')
          break;
        if (d[y][i] <= d[y][x])
          break;
        int ncost = (i - x - 1) / K + 1;

        if (ncost + cost < d[y][i]) {
          d[y][i] = ncost + cost;
          q.push({d[y][i], {y, i}});
        }
      }

      for (int i = x - 1; i >= 0; i--) {
        if (c[y][i] == '@')
          break;
        if (d[y][i] <= d[y][x])
          break;

        int ncost = (x - i - 1) / K + 1;

        if (ncost + cost < d[y][i]) {
          d[y][i] = ncost + cost;
          q.push({d[y][i], {y, i}});
        }
      }
    }

    {
      for (int i = y + 1; i < H; i++) {
        if (c[i][x] == '@')
          break;
        if (d[i][x] <= d[y][x])
          break;

        int ncost = (i - y - 1) / K + 1;

        if (ncost + cost < d[i][x]) {
          d[i][x] = ncost + cost;
          q.push({d[i][x], {i, x}});
        }
      }

      for (int i = y - 1; i >= 0; i--) {
        if (c[i][x] == '@')
          break;
        if (d[i][x] <= d[y][x])
          break;

        int ncost = (y - i - 1) / K + 1;

        if (ncost + cost < d[i][x]) {
          d[i][x] = ncost + cost;
          q.push({d[i][x], {i, x}});
        }
      }
    }
  }

  return -1;
}

signed main() {
  cout << fixed << setprecision(10);

  int x1, y1, x2, y2;

  cin >> H >> W >> K;

  cin >> x1 >> y1 >> x2 >> y2;

  x1--;
  y1--;
  x2--;
  y2--;

  c.resize(H);

  for (int i = 0; i < H; i++) {
    cin >> c[i];
  }
  cout << dijkstra(x1, y1, x2, y2) << endl;

  return 0;
}