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

#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)
#define ALL(c) (c).begin(), (c).end()

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<long double> VD;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<VD> VVD;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;

template <typename T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}
template <typename T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}

int in() {
  int x;
  scanf("%d", &x);
  return x;
}

#define INF 1LL << 60

struct state {
  int x;
  int y;
  int dir;
};

state next_state(state s, int i) {
  P cur = make_pair(s.x, s.y);
  P next = cur;
  if (i == 0)
    next.first += 1;
  else if (i == 1)
    next.first -= 1;
  else if (i == 2)
    next.second += 1;
  else
    next.second -= 1;

  state ns = {next.first, next.second, i};
  return ns;
}

int main() {
  int H, W, K;
  cin >> H >> W >> K;
  P s, t;
  cin >> s.first >> s.second >> t.first >> t.second;

  vector<string> c_raw(H);
  REP(i, H) cin >> c_raw[i];

  vector<vector<int>> c(H + 2, vector<int>(W + 2, 0));
  FOR(i, 1, H)
  FOR(j, 1, W)
  if (c_raw[i - 1][j - 1] == '.')
    c[i][j] = 1;

  // dp[i][j][k]: (i, j) にいるときに方向kを向いてる
  vector<vector<vector<ll>>> dist(
      H + 5, vector<vector<ll>>(W + 5, vector<ll>(4, 1e18)));
  REP(i, 4)
  dist[s.first][s.second][i] = 0;

  queue<pair<ll, state>> q;
  REP(i, 4) {
    state a = {s.first, s.second, i};
    q.push(make_pair(0, a));
  }

  while (!q.empty()) {
    state cur = q.front().second;
    q.pop();

    // 4方向について探索
    REP(i, 4) {
      state ns;
      ll nd;

      if (i == cur.dir) {
        ns = next_state(cur, i);
        if (c[ns.x][ns.y] == 0)
          continue;
        nd = dist[cur.x][cur.y][cur.dir] + 1;
      } else {
        state stmp = {cur.x, cur.y, i};
        ns = stmp;
        nd = ceil(dist[cur.x][cur.y][cur.dir] / double(K)) * K;
      }

      if (dist[ns.x][ns.y][ns.dir] > nd) {
        dist[ns.x][ns.y][ns.dir] = nd;
        q.push(make_pair(nd, ns));
      }
    }
  }

  // cerr << "DEBUG" << endl;
  // REP(i, H+2) {
  //     REP(j, W+2) {
  //         int a = 1e9;
  //         REP(k, 4) a = min(a, int(ceil(dist[i][j][k])));
  //         cerr << a << " ";
  //     }
  //     cerr << endl;
  // }

  double ans = 1e9;
  REP(i, 4)
  ans = min(ans, ceil(dist[t.first][t.second][i] / double(K)));

  if (ans == 1e9) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)
#define ALL(c) (c).begin(), (c).end()

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<long double> VD;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<VD> VVD;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;

template <typename T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}
template <typename T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}

int in() {
  int x;
  scanf("%d", &x);
  return x;
}

#define INF 1LL << 60

int main() {
  int H, W, K;
  cin >> H >> W >> K;
  P s, t;
  cin >> s.first >> s.second >> t.first >> t.second;
  s.first--;
  s.second--;
  t.first--;
  t.second--;

  vector<string> c(H);
  REP(i, H) cin >> c[i];

  vector<vector<int>> dist(H + 5, vector<int>(W + 5, -1));
  dist[s.first][s.second] = 0;

  queue<pair<int, P>> q;
  q.push(make_pair(0, s));
  while (!q.empty()) {
    P cur = q.front().second;
    if (dist[cur.first][cur.second] < q.front().first) {
      continue;
    }
    q.pop();

    // 4方向について探索
    REP(i, 4) {
      int k = 0;
      P next = cur;
      if (i == 0)
        next.first += 1;
      else if (i == 1)
        next.first -= 1;
      else if (i == 2)
        next.second += 1;
      else
        next.second -= 1;

      if (0 > next.first || next.first >= H || next.second < 0 ||
          next.second >= W || c[next.first][next.second] == '@')
        continue;

      while (0 <= next.first && next.first < H && next.second >= 0 &&
             next.second < W && c[next.first][next.second] != '@' && k < K) {
        if (dist[next.first][next.second] > dist[cur.first][cur.second] + 1 ||
            dist[next.first][next.second] == -1) {
          int d = dist[cur.first][cur.second] + 1;
          dist[next.first][next.second] = d;
          q.push(make_pair(d, next));
        }

        if (i == 0)
          next.first += 1;
        else if (i == 1)
          next.first -= 1;
        else if (i == 2)
          next.second += 1;
        else
          next.second -= 1;

        k++;
      }
    }
  }

  cerr << "DEBUG" << endl;
  REP(i, H) {
    REP(j, W)
    cerr << dist[i][j] << " ";
    cerr << endl;
  }
  if (dist[t.first][t.second] == 1e6) {
    cout << -1 << endl;
  } else {
    cout << dist[t.first][t.second] << endl;
  }
  return 0;
}