Problem p02644 (C++)

Buggy submission (status: Time Limit Exceeded):
// #include <bits/stdc++.h>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef vector<int> vint;
typedef vector<vector<int>> vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long>> vvll, vvLL;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = 0; i < n; ++i)
#define mod (ll)(1e9 + 7)
#define FIX(a) ((a) % mod + mod) % mod
#define ALL(obj) (obj).begin(), (obj).end()
#define rALL(obj) (obj).rbegin(), (obj).rend()
#define INF 1000000000              // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define fi first
#define se second
#define pb push_back
int dy[] = {0, 0, 1, -1};
int dx[] = {1, -1, 0, 0};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll h, w, k;
  cin >> h >> w >> k;
  vll s(2), g(2);
  REP(i, 2) {
    cin >> s[i];
    s[i]--;
  }
  REP(i, 2) {
    cin >> g[i];
    g[i]--;
  }
  vector<vector<vector<pair<ll, ll>>>> c(
      h, vector<vector<pair<ll, ll>>>(
             w, vector<pair<ll, ll>>(4, make_pair(INF, INF))));
  REP(i, h) {
    string ci;
    cin >> ci;
    REP(j, w) {
      if (ci[j] == '@') {
        REP(l, 4) { c[i][j][l] = make_pair(-1LL, -1LL); }
      }
    }
  }
  REP(i, 4) { c[s[0]][s[1]][i] = make_pair(0, k); }
  queue<pair<ll, ll>> q;
  q.push(make_pair(s[0], s[1]));
  while (!q.empty()) {
    auto now = q.front();
    q.pop();
    REP(i, 4) {
      // 行く方向
      ll ni = now.fi + dy[i];
      ll nj = now.se + dx[i];
      if (ni < 0 || ni >= h || nj < 0 || nj >= w)
        continue;
      if (c[ni][nj][0] == make_pair(-1LL, -1LL))
        continue;
      REP(j, 4) {
        // 来た方向
        if (i == j && c[now.fi][now.se][i].se > 0 &&
            c[now.fi][now.se][j].fi < INF) {
          ll tf = c[now.fi][now.se][i].fi;
          ll ts = c[now.fi][now.se][i].se - 1;
          if ((tf < c[ni][nj][j].fi) ||
              (tf == c[ni][nj][j].fi && ts > c[ni][nj][j].se)) {
            c[ni][nj][j].fi = tf;
            c[ni][nj][j].se = ts;
            q.push(make_pair(ni, nj));
          }

        } else {
          ll tf = c[now.fi][now.se][j].fi + 1;
          ll ts = k - 1;
          if ((tf <= c[ni][nj][i].fi)) {
            c[ni][nj][i].fi = tf;
            c[ni][nj][i].se = ts;
            q.push(make_pair(ni, nj));
          }
        }
      }
    }
  }
  ll ans = LLINF;

  REP(i, 4) { ans = min(ans, c[g[0]][g[1]][i].fi); }
  if (ans == INF) {
    cout << -1 << endl;
  } else {
    cout << ans + 1 << endl;
  }

  return 0;
}

Fixed submission:
// #include <bits/stdc++.h>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef vector<int> vint;
typedef vector<vector<int>> vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long>> vvll, vvLL;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = 0; i < n; ++i)
#define mod (ll)(1e9 + 7)
#define FIX(a) ((a) % mod + mod) % mod
#define ALL(obj) (obj).begin(), (obj).end()
#define rALL(obj) (obj).rbegin(), (obj).rend()
#define INF 1000000000              // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define fi first
#define se second
#define pb push_back
int dy[] = {0, 0, 1, -1};
int dx[] = {1, -1, 0, 0};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll h, w, k;
  cin >> h >> w >> k;
  vll s(2), g(2);
  REP(i, 2) {
    cin >> s[i];
    s[i]--;
  }
  REP(i, 2) {
    cin >> g[i];
    g[i]--;
  }
  vector<vector<vector<pair<ll, ll>>>> c(
      h, vector<vector<pair<ll, ll>>>(
             w, vector<pair<ll, ll>>(4, make_pair(INF, INF))));
  REP(i, h) {
    string ci;
    cin >> ci;
    REP(j, w) {
      if (ci[j] == '@') {
        REP(l, 4) { c[i][j][l] = make_pair(-1LL, -1LL); }
      }
    }
  }
  REP(i, 4) { c[s[0]][s[1]][i] = make_pair(0, k); }
  queue<pair<ll, ll>> q;
  q.push(make_pair(s[0], s[1]));
  while (!q.empty()) {
    auto now = q.front();
    q.pop();
    REP(i, 4) {
      // 行く方向
      ll ni = now.fi + dy[i];
      ll nj = now.se + dx[i];
      if (ni < 0 || ni >= h || nj < 0 || nj >= w)
        continue;
      if (c[ni][nj][0] == make_pair(-1LL, -1LL))
        continue;
      REP(j, 4) {
        // 来た方向
        if (i == j && c[now.fi][now.se][i].se > 0 &&
            c[now.fi][now.se][j].fi < INF) {
          ll tf = c[now.fi][now.se][i].fi;
          ll ts = c[now.fi][now.se][i].se - 1;
          if ((tf < c[ni][nj][j].fi) ||
              (tf == c[ni][nj][j].fi && ts > c[ni][nj][j].se)) {
            c[ni][nj][j].fi = tf;
            c[ni][nj][j].se = ts;
            q.push(make_pair(ni, nj));
          }

        } else {
          ll tf = c[now.fi][now.se][j].fi + 1;
          ll ts = k - 1;
          if ((tf < c[ni][nj][i].fi) ||
              (tf == c[ni][nj][i].fi && ts > c[ni][nj][i].se)) {
            c[ni][nj][i].fi = tf;
            c[ni][nj][i].se = ts;
            q.push(make_pair(ni, nj));
          }
        }
      }
    }
  }
  ll ans = LLINF;

  REP(i, 4) { ans = min(ans, c[g[0]][g[1]][i].fi); }
  if (ans == INF) {
    cout << -1 << endl;
  } else {
    cout << ans + 1 << endl;
  }

  return 0;
}