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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define chmax(a, b) a = max(a, b);
#define chmin(a, b) a = min(a, b);
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  vector<vector<int>> c(h, vector<int>(w));
  rep(i, h) rep(j, w) {
    char ci;
    cin >> ci;
    c[i][j] = ci == '.' ? 0 : 1;
  }
  vector<set<int>> rrest(h);
  vector<set<int>> crest(w);
  rep(i, h) rep(j, w) {
    rrest[i].insert(j);
    crest[j].insert(i);
  }
  vector<vector<int>> dp(h, vector<int>(w, 1001001001));
  vector<P> q{{x1, y1}};
  dp[x1][y1] = 0;
  rrest[x1].erase(y1);
  crest[y1].erase(x1);
  int now = 1;
  while (!q.empty()) {
    vector<P> nq;
    for (auto &p : q) {
      int x = p.first, y = p.second;
      if (!rrest[x].empty()) {
        // move (1, 0)
        auto it = rrest[x].lower_bound(y);
        vector<int> rmlater;
        while (it != rrest[x].end()) {
          int ny = *it;
          if (ny <= y + k && c[x][ny] != 1) {
            rmlater.push_back(ny);
            dp[x][ny] = now;
            nq.emplace_back(x, ny);
          } else
            break;
          it++;
        }
        for (int ny : rmlater) {
          rrest[x].erase(ny);
          crest[ny].erase(x);
        }
      }
      if (!rrest[x].empty()) {
        // move (-1, 0)
        auto it = rrest[x].lower_bound(y);
        vector<int> rmlater;
        while (it != rrest[x].begin()) {
          it--;
          int ny = *it;
          if (ny >= y - k && c[x][ny] != 1) {
            rmlater.push_back(ny);
            dp[x][ny] = now;
            nq.emplace_back(x, ny);
          } else
            break;
        }
        for (int ny : rmlater) {
          rrest[x].erase(ny);
          crest[ny].erase(x);
        }
      }
      if (!crest[y].empty()) {
        // move (0, 1)
        auto it = crest[y].lower_bound(x);
        vector<int> rmlater;
        while (it != crest[y].end()) {
          int nx = *it;
          if (nx <= x + k && c[nx][y] != 1) {
            rmlater.push_back(nx);
            dp[nx][y] = now;
            nq.emplace_back(nx, y);
          } else
            break;
          it++;
        }
        for (int nx : rmlater) {
          rrest[nx].erase(y);
          crest[y].erase(nx);
        }
      }
      if (!crest[y].empty()) {
        // move (0, -1)
        auto it = crest[y].lower_bound(x);
        vector<int> rmlater;
        while (it != crest[y].begin()) {
          it--;
          int nx = *it;
          if (nx >= x - k && c[nx][y] != 1) {
            rmlater.push_back(nx);
            dp[nx][y] = now;
            nq.emplace_back(nx, y);
          } else
            break;
        }
        for (int nx : rmlater) {
          rrest[nx].erase(y);
          crest[y].erase(nx);
        }
      }
    }
    now++;
    q = move(nq);
  }
  int ans = dp[x2][y2];
  if (ans == 1001001001)
    ans = -1;
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define chmax(a, b) a = max(a, b);
#define chmin(a, b) a = min(a, b);
using namespace std;
using ll = long long;
using P = pair<int, int>;

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

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  vector<vector<int>> c(h, vector<int>(w));
  rep(i, h) rep(j, w) {
    char ci;
    cin >> ci;
    c[i][j] = ci == '.' ? 0 : 1;
  }
  vector<vector<int>> dp(h, vector<int>(w, 1001001001));
  vector<P> q{{x1, y1}};
  dp[x1][y1] = 0;
  int now = 1;
  vector<vector<vector<P>>> nextstart(h, vector<vector<P>>(w, vector<P>(4)));
  rep(i, h) rep(j, w) rep(t, 4) nextstart[i][j][t] = {i + dx[t], j + dy[t]};
  while (!q.empty()) {
    vector<P> nq;
    for (auto p : q) {
      int x, y;
      tie(x, y) = p;
      rep(t, 4) {
        vector<P> memo;
        P np;
        for (np = nextstart[x][y][t];
             0 <= np.first && np.first < h && 0 <= np.second && np.second < w &&
             max(abs(np.first - x), abs(np.second - y)) <= k &&
             c[np.first][np.second] != 1;
             np.first += dx[t], np.second += dy[t]) {
          int nx = np.first, ny = np.second;
          if (!(0 <= nx && nx < h && 0 <= ny && ny < w) ||
              dp[nx][ny] != 1001001001)
            continue;
          dp[nx][ny] = now;
          nq.emplace_back(nx, ny);
          memo.emplace_back(nx, ny);
        }
        rep(i, memo.size()) {
          int z, r;
          tie(z, r) = p;
          nextstart[z][r][t] =
              (i == memo.size() - 1) ? np : P(1001001001, 1001001001);
        }
      }
    }
    now++;
    swap(q, nq);
  }
  int ans = dp[x2][y2];
  if (ans == 1001001001)
    ans = -1;
  cout << ans << endl;
}