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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define ll long long
#define pal pair<ll, ll>
#define pad pair<double, double>
#define PB push_back
#define MP make_pair
#define F first
#define S second

const ll MOD = 1e9 + 7;
const ll INF = 1e18;
const double EPS = 1e-8;

long long pow(long long x, long long n) {
  long long ret = 1;
  while (n > 0) {
    if (n & 1)
      ret = ret * x % MOD;
    x = x * x % MOD;
    n >>= 1;
  }
  return ret;
}

template <typename T, typename U> bool chmax(T &max, const U &x) {
  if (max < x) {
    max = x;
    return true;
  }
  return false;
}

template <typename T, typename U> bool chmin(T &min, const U &x) {
  if (min > x) {
    min = x;
    return true;
  }
  return false;
}

ll dx[4] = {1, 0, -1, 0}, dy[6] = {0, 1, 0, -1};

int main() {
  ll h, w, k;
  cin >> h >> w >> k;

  pal s, g;
  cin >> s.F >> s.S >> g.F >> g.S;

  ll d[h + 2][w + 2];
  bool obs[h + 2][w + 2];
  fill(d[0], d[0] + (h + 2) * (w + 2), -INF);
  fill(obs[0], obs[0] + (h + 2) * (w + 2), false);

  for (ll i = 1; i <= h; i++) {
    for (ll j = 1; j <= w; j++) {
      char tmp;
      cin >> tmp;
      if (tmp == '@') {
        obs[i][j] = true;
      }
    }
  }
  for (ll i = 0; i < h + 2; i++) {
    obs[i][0] = true;
    obs[i][w + 1] = true;
  }
  for (ll i = 0; i < w + 2; i++) {
    obs[0][i] = true;
    obs[h + 1][i] = true;
  }

  priority_queue<pair<pal, pal>> que; //(掻いた回数,向いてる方向),座標

  d[s.F][s.S] = 0;

  que.push(MP(MP(0, -1), s));

  while (que.size()) {
    pair<pal, pal> t = que.top();
    que.pop();
    if (t.S.F == g.F && t.S.S == g.S)
      break; // 終了条件

    for (ll i = 0; i < 4; i++) { // 4方向
      if (i == t.F.S || (i + 2) % 4 == t.F.S)
        continue;
      for (ll j = 0; j < k; j++) { // kマス先まで
        if (obs[t.S.F + (j + 1) * dx[i]][t.S.S + (j + 1) * dy[i]])
          break;
        if (d[t.S.F + (j + 1) * dx[i]][t.S.S + (j + 1) * dy[i]] ==
            -INF) { // 未訪問&蓮ナシ
          d[t.S.F + (j + 1) * dx[i]][t.S.S + (j + 1) * dy[i]] = t.F.F - 1;
          if (j == k - 1) {
            que.push(MP(MP(t.F.F - 1, -1),
                        MP(t.S.F + (j + 1) * dx[i], t.S.S + (j + 1) * dy[i])));
          } else {
            que.push(MP(MP(t.F.F - 1, i),
                        MP(t.S.F + (j + 1) * dx[i], t.S.S + (j + 1) * dy[i])));
          }
        }
      }
    }
  }
  if (d[g.F][g.S] != -INF) {
    cout << -d[g.F][g.S] << endl;
  } else {
    cout << -1 << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define ll long long
#define pal pair<ll, ll>
#define pad pair<double, double>
#define PB push_back
#define MP make_pair
#define F first
#define S second

const ll MOD = 1e9 + 7;
const ll INF = 1e18;
const double EPS = 1e-8;

long long pow(long long x, long long n) {
  long long ret = 1;
  while (n > 0) {
    if (n & 1)
      ret = ret * x % MOD;
    x = x * x % MOD;
    n >>= 1;
  }
  return ret;
}

template <typename T, typename U> bool chmax(T &max, const U &x) {
  if (max < x) {
    max = x;
    return true;
  }
  return false;
}

template <typename T, typename U> bool chmin(T &min, const U &x) {
  if (min > x) {
    min = x;
    return true;
  }
  return false;
}

ll dx[4] = {1, 0, -1, 0}, dy[6] = {0, 1, 0, -1};

int main() {
  ll h, w, k;
  cin >> h >> w >> k;

  pal s, g;
  cin >> s.F >> s.S >> g.F >> g.S;

  ll d[h + 2][w + 2];
  bool obs[h + 2][w + 2];
  fill(d[0], d[0] + (h + 2) * (w + 2), -INF);
  fill(obs[0], obs[0] + (h + 2) * (w + 2), false);

  for (ll i = 1; i <= h; i++) {
    for (ll j = 1; j <= w; j++) {
      char tmp;
      cin >> tmp;
      if (tmp == '@') {
        obs[i][j] = true;
      }
    }
  }
  for (ll i = 0; i < h + 2; i++) {
    obs[i][0] = true;
    obs[i][w + 1] = true;
  }
  for (ll i = 0; i < w + 2; i++) {
    obs[0][i] = true;
    obs[h + 1][i] = true;
  }

  priority_queue<pair<ll, pal>> que; // 掻いた回数,座標

  d[s.F][s.S] = 0;

  que.push(MP(0, s));

  while (que.size()) {
    pair<ll, pal> t = que.top();
    que.pop();
    if (t.S.F == g.F && t.S.S == g.S)
      break; // 終了条件

    for (ll i = 0; i < 4; i++) {   // tについて4方向探索
      for (ll j = 0; j < k; j++) { // kマス先まで
        if (obs[t.S.F + (j + 1) * dx[i]][t.S.S + (j + 1) * dy[i]])
          break;
        if (d[t.S.F + (j + 1) * dx[i]][t.S.S + (j + 1) * dy[i]] ==
            -INF) { // 未訪問&蓮ナシ
          d[t.S.F + (j + 1) * dx[i]][t.S.S + (j + 1) * dy[i]] = t.F - 1;
          que.push(MP(t.F - 1,
                      MP(t.S.F + (j + 1) * dx[i], t.S.S + (j + 1) * dy[i])));
        }
      }
    }
  }
  if (d[g.F][g.S] != -INF) {
    cout << -d[g.F][g.S] << endl;
  } else {
    cout << -1 << endl;
  }
}