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

#include "bits/stdc++.h"

using namespace std;
using P = pair<int, int>;
using LP = pair<long long, int>;

const long long INF = 1e18;

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

int main() {
  int H;
  int W;
  int K; // 泳げる距離
  cin >> H >> W >> K;
  int Sx;
  int Sy;
  int Gx;
  int Gy;
  cin >> Sx >> Sy >> Gx >> Gy;
  --Sx;
  --Sy;
  --Gx;
  --Gy;

  vector<string> g(H);
  for (int n = 0; n < H; ++n) {
    string str;
    cin >> g[n];
  }

  auto told = [&](int i, int j, int v) { return (i * W + j) * 4 + v; };

  auto modceil = [&](long long x) { // 切り上げ
    return (x + K - 1) / K * K;
  };

  vector<long long> dist(H * W * 4, INF);
  priority_queue<LP, vector<LP>, greater<LP>> q; // 小さいもの順
  auto push = [&](int i, int j, int v, long long x) {
    int id = told(i, j, v);
    if (dist[id] <= x) {
      return;
    }
    dist[id] = x;
    q.emplace(x, id);
  };

  for (int n = 0; n < 4; ++n) {
    push(Sx, Sy, n, 0);
  }

  while (!q.empty()) {
    long long d = q.top().first;
    int id = q.top().second;
    q.pop();
    if (dist[id] != d) { // 古いデータは捨てる
      continue;
    }

    int i = id / W / 4;
    int j = id / 4 % W;
    int v = id % 4;
    // 方向転換
    for (int n = 0; n < 4; ++n) {
      push(i, j, n, modceil(d));
    }
    // 直進
    int nx = i + dx[v];
    int ny = j + dy[v];
    if ((0 <= nx) && (H > nx) && (0 <= ny) && (W > ny)) {
      if (g[nx][ny] == '.') {
        push(nx, ny, v, d + 1);
      }
    }
  }

  long long ans = INF;
  for (int n = 0; n < 4; ++n) {
    ans = min(ans, dist[told(Gx, Gy, n)]);
  }
  if (INF == ans) {
    cout << -1 << endl;
  } else {
    ans = modceil(ans) / K;
    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"

using namespace std;

int main() {
  int H;
  int W;
  int K; // 泳げる距離
  cin >> H >> W >> K;
  int Sx;
  int Sy;
  int Gx;
  int Gy;
  cin >> Sx >> Sy >> Gx >> Gy;
  --Sx;
  --Sy;
  --Gx;
  --Gy;

  vector<vector<string>> map(H, vector<string>(W));
  for (int n = 0; n < H; ++n) {
    string str;
    cin >> str;
    for (int m = 0; m < W; ++m) {
      map[n][m] = str[m];
    }
  }
  const int dx[] = {-1, 0, 1, 0};
  const int dy[] = {0, -1, 0, 1};

  vector<vector<int>> D(H, vector<int>(W)); // 回数
  for (int n = 0; n < H; ++n) {
    for (int m = 0; m < W; ++m) {
      D[n][m] = -1; // 初期値
    }
  }

  queue<pair<int, int>> q;
  q.emplace(Sx, Sy);
  D[Sx][Sy] = 0;

  while (!q.empty()) {
    int x = q.front().first;
    int y = q.front().second;
    q.pop();
    // 範囲内かつ0を4セット
    for (int l = 0; l < 4; ++l) {
      for (int m = 1; m <= K; ++m) {
        int x1 = x + m * dx[l];
        int y1 = y + m * dy[l];
        if ((0 <= x1) && (H > x1) && (0 <= y1) && (W > y1)) {
          if ((0 != map[x1][y1].compare("@"))) {
            if (-1 == D[x1][y1]) {
              D[x1][y1] = D[x][y] + 1;
              q.emplace(x1, y1);
            } else if (D[x1][y1] > D[x][y] + 1) {
              D[x1][y1] = D[x][y] + 1;
              q.emplace(x1, y1);
            }
          } else {
            break;
          }
        }
        if ((x1 == Gx) && (y1 == Gy)) {
          cout << D[Gx][Gy] << endl;
          return 0;
        }
      }
    }
    if (false) {
      for (int n = 0; n < H; ++n) {
        for (int m = 0; m < W; ++m) {
          cout << D[n][m]; // 初期値
        }
        cout << endl;
      }
      cout << endl;
    }
  }

  cout << D[Gx][Gy] << endl;
  return 0;
}