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

#include <algorithm>
#include <cinttypes>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define FSCNd64 "%" SCNd64
#define FPRId64 "%" PRId64

using namespace std;

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vpii = vector<pii>;

#define PI 3.1415926535897932384626433832795

template <typename X> bool max_u(X &m, X v) {
  if (m < v) {
    m = v;
    return true;
  }
  return false;
}

template <typename X> bool min_u(X &m, X v) {
  if (m > v) {
    m = v;
    return true;
  }
  return false;
}

#define FLD_BAR 9999999
#define FLD_INF 8888888

struct dir_t {
  int k[4];
};

struct solve {
  int vx[4] = {1, 0, -1, 0};
  int vy[4] = {0, 1, 0, -1};

  solve() {}
  ll operator()(int k, int w, int h, vector<string> &c, int x1, int y1, int x2,
                int y2) {
    vvi f(h + 2);
    for (auto &m : f)
      m.assign(w + 2, FLD_BAR);

    vector<vector<dir_t>> d(h + 2);
    for (auto &m : d)
      m.resize(w + 2);

    for (int y = 1; y <= h; y++) {
      for (int x = 1; x <= w; x++) {
        if (c[y - 1][x - 1] == '.')
          f[y][x] = FLD_INF;
      }
    }

    queue<pair<int, int>> q;
    q.push(make_pair(x1, y1));
    f[y1][x1] = 0;
    while (!q.empty()) {
      int x, y;
      tie(x, y) = q.front();
      q.pop();
      if (x == x2 && y == y2)
        return f[y2][x2];
      // cout << "(x,y,c)=" << x << "," << y << "," << f[y][x] << "\\n";
      int nc = f[y][x] + 1;
      for (int i = 0; i < 4; i++) {
        int i_ = (i + 2) % 4;
        for (int j = 0; j <= k;) {
          int nx = x + vx[i] * j;
          int ny = y + vy[i] * j;
          if (d[ny][nx].k[i] < 0)
            break;
          if (d[ny][nx].k[i] > 0) {
            j += d[ny][nx].k[i];
            continue;
          }
          if (f[ny][nx] == FLD_BAR) {
            for (; j > 0; j--) {
              nx -= vx[i];
              ny -= vy[i];
              d[ny][nx].k[i] = -1;
              if (j > 1)
                d[ny][nx].k[i_] = -1;
            }
            break;
          }
          d[ny][nx].k[i] = k - j + 1;
          if (j > 0)
            d[ny][nx].k[i_] = -1;
          if (min_u(f[ny][nx], nc)) {
            q.push(make_pair(nx, ny));
          }
          j++;
        }
      }
    }

    return -1;
  }
};

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  for (;;) {
    int h, w, k;
    cin >> h >> w >> k;
    if (cin.fail())
      break;

    int x1, y1, x2, y2;
    cin >> y1 >> x1 >> y2 >> x2;

    vector<string> c(h);
    for (auto &m : c)
      cin >> m;

    cout << solve()(k, w, h, c, x1, y1, x2, y2) << "\\n";
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cinttypes>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define FSCNd64 "%" SCNd64
#define FPRId64 "%" PRId64

using namespace std;

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vpii = vector<pii>;

#define PI 3.1415926535897932384626433832795

template <typename X> bool max_u(X &m, X v) {
  if (m < v) {
    m = v;
    return true;
  }
  return false;
}

template <typename X> bool min_u(X &m, X v) {
  if (m > v) {
    m = v;
    return true;
  }
  return false;
}

#define FLD_BAR 9999999
#define FLD_INF 8888888

struct solve {
  int vx[4] = {1, 0, -1, 0};
  int vy[4] = {0, 1, 0, -1};

  solve() {}
  ll operator()(int k, int w, int h, vector<string> &c, int x1, int y1, int x2,
                int y2) {
    vvi f(h + 2);
    for (auto &m : f)
      m.assign(w + 2, FLD_BAR);

    for (int y = 1; y <= h; y++) {
      for (int x = 1; x <= w; x++) {
        if (c[y - 1][x - 1] == '.')
          f[y][x] = FLD_INF;
      }
    }

    queue<pair<int, int>> q;
    q.push(make_pair(x1, y1));
    f[y1][x1] = 0;
    while (!q.empty()) {
      int x, y;
      tie(x, y) = q.front();
      q.pop();
      if (x == x2 && y == y2)
        return f[y2][x2];
      // cout << "(x,y,c)=" << x << "," << y << "," << f[y][x] << "\\n";
      int nc = f[y][x] + 1;
      for (int i = 0; i < 4; i++) {
        int nx = x;
        int ny = y;
        for (int j = 0; j < k; j++) {
          nx += vx[i];
          ny += vy[i];
          if (f[ny][nx] == FLD_BAR)
            break;
          if (min_u(f[ny][nx], nc)) {
            q.push(make_pair(nx, ny));
          }
        }
      }
    }

    return -1;
  }
};

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  for (;;) {
    int h, w, k;
    cin >> h >> w >> k;
    if (cin.fail())
      break;

    int x1, y1, x2, y2;
    cin >> y1 >> x1 >> y2 >> x2;

    vector<string> c(h);
    for (auto &m : c)
      cin >> m;

    cout << solve()(k, w, h, c, x1, y1, x2, y2) << "\\n";
  }
  return 0;
}