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

#include <bits/stdc++.h>
using namespace std;
#define Sz(a) ((int)(a).size())
const int maxn = 1000009;
const int MOD = 1e9 + 7;
string MP[maxn];
vector<vector<int>> U, D, L, R, V;
int main() {
#ifdef LOCAL
  freopen("a.in", "r", stdin);
#endif
  ios_base::sync_with_stdio(false), cout.tie(0), cin.tie(0);
  int n, m, k, x1, x2, y1, y2;
  cin >> n >> m >> k;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  for (int i = 0; i < n; ++i)
    cin >> MP[i];
  U.assign(n, vector<int>(m));
  D.assign(n, vector<int>(m));
  R.assign(n, vector<int>(m));
  L.assign(n, vector<int>(m));
  V.assign(n, vector<int>(m, -1));
  auto in = [&](int x, int y) { return x >= 0 && x < n && y >= 0 && y < m; };
  auto work = [&](int x, int y, int dx, int dy, vector<vector<int>> &X,
                  vector<pair<int, int>> &quee) {
    int inx = x, iny = y;
    for (int j = 1; j <= k; ++j) {
      x += dx;
      y += dy;
      if (!in(x, y))
        break;
      if (MP[x][y] == '@')
        break;
      if (V[x][y] == -1) {
        X[x][y] = k - j;
        V[x][y] = V[inx][iny] + 1;
        quee.emplace_back(x, y);
      } else if (V[x][y] == V[inx][iny]) {
        if (X[x][y] >= k - j) {
          break;
        } else {
          if (!in(x + dx * X[x][y], y + dy * X[x][y]))
            break;
          if (X[x + dx * X[x][y]][y + dy * X[x][y]] == 0 &&
              V[inx][iny] == V[x + dx * X[x][y]][y + dy * X[x][y]]) {
            j += X[x][y];
            X[x + dx * X[x][y]][y + dy * X[x][y]] = k - j;
          }
        }
      } else if (V[x][y] < V[inx][iny]) {
        break;
      }
    }
  };
  vector<pair<int, int>> que;
  que.emplace_back(x1, y1);
  V[x1][y1] = 0;
  for (int i = 0; i < Sz(que); ++i) {
    int x = que[i].first, y = que[i].second;
    work(x, y, +1, 0, D, que);
    work(x, y, -1, 0, U, que);
    work(x, y, 0, +1, R, que);
    work(x, y, 0, -1, L, que);
  }
  cout << V[x2][y2] << '\\n';
  return 0;
}
// 2020.06.14 21:11:35


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define Sz(a) ((int)(a).size())
const int maxn = 1000009;
const int MOD = 1e9 + 7;
string MP[maxn];
vector<vector<int>> U, D, L, R, V;
int main() {
#ifdef LOCAL
  freopen("a.in", "r", stdin);
#endif
  ios_base::sync_with_stdio(false), cout.tie(0), cin.tie(0);
  int n, m, k, x1, x2, y1, y2;
  cin >> n >> m >> k;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  for (int i = 0; i < n; ++i)
    cin >> MP[i];
  U.assign(n, vector<int>(m));
  D.assign(n, vector<int>(m));
  R.assign(n, vector<int>(m));
  L.assign(n, vector<int>(m));
  V.assign(n, vector<int>(m, -1));
  auto in = [&](int x, int y) { return x >= 0 && x < n && y >= 0 && y < m; };
  auto work = [&](int x, int y, int dx, int dy, vector<vector<int>> &X,
                  vector<pair<int, int>> &quee) {
    int inx = x, iny = y;
    for (int j = 1; j <= k; ++j) {
      x += dx;
      y += dy;
      if (!in(x, y))
        break;
      if (MP[x][y] == '@')
        break;
      if (V[x][y] == -1) {
        X[x][y] = k - j;
        V[x][y] = V[inx][iny] + 1;
        quee.emplace_back(x, y);
      } else if (V[x][y] == V[inx][iny]) {
        if (X[x][y] >= k - j) {
          break;
        } else {
          if (!in(x + dx * X[x][y], y + dy * X[x][y]))
            break;
          if (X[x + dx * X[x][y]][y + dy * X[x][y]] == 0 &&
              V[inx][iny] == V[x + dx * X[x][y]][y + dy * X[x][y]])
            j += X[x][y];
        }
      } else if (V[x][y] < V[inx][iny]) {
        break;
      }
    }
  };
  vector<pair<int, int>> que;
  que.emplace_back(x1, y1);
  V[x1][y1] = 0;
  for (int i = 0; i < Sz(que); ++i) {
    int x = que[i].first, y = que[i].second;
    work(x, y, +1, 0, D, que);
    work(x, y, -1, 0, U, que);
    work(x, y, 0, +1, R, que);
    work(x, y, 0, -1, L, que);
  }
  cout << V[x2][y2] << '\\n';
  return 0;
}
// 2020.06.14 21:11:35