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;
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
#define int long long
#define double long double
#define endl '\\n'
#define vi vector<int>
#define vvi vector<vi>
#define vpii vector<pii>
#define vpipii vector<pipii>
#define vpiipi vector<piipi>
#define pb push_back
#define pob pop_back
#define pii pair<int, int>
#define pipii pair<int, pii>
#define piipi pair<pii, int>
#define pld pair<long double, long double>
#define mod 1000000007
// #define mod 1000000009
// #define mod 163577857
// #define mod 998244353
#define rep(i, n) for (int i = 0; i < n; i++)
#define repp(i, a, b) for (int i = a; i < b; i++)
#define reppr(i, a, b) for (int i = a - 1; i >= b; i--)
#define repr(i, n) for (int i = n - 1; i >= 0; i--)
#define ff first
#define ss second
#define pc putchar_unlocked
#define gc getchar_unlocked
#define inf 9223372036854775807
#define infn -9223372036854775807
#define pi 3.14159265358979323846
#define eps 0.0000000001
#define sp << " " <<
#define setprec(x) cout << fixed << setprecision(x);
#define REVERSE(a) reverse(all(a));
#define SORT(a) sort(all(a));
#define all(n) n.begin(), n.end()

// GCD and LCM
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a / gcd(a, b) * b; }

// Modular Exponentiation
int powmod(int x, int y) {
  if (y == 0)
    return 1;
  int p = powmod(x, y / 2) % mod;
  p = (p * p) % mod;
  return (y % 2 == 0) ? p : (x * p) % mod;
}

// Modular Inverse
int inverse(int a) { return powmod(a, mod - 2); }

// Binomial Coefficients
int fact[500010], factinv[500010], coefficientflag = 0;
void Util_nCr() {
  fact[0] = 1, factinv[0] = 1;
  repp(i, 1, 500010) {
    fact[i] = (fact[i - 1] * i) % mod;
    factinv[i] = (factinv[i - 1] * inverse(i)) % mod;
  }
}
int nCr(int n, int r) {
  if (!coefficientflag)
    coefficientflag = 1, Util_nCr();
  return ((fact[n] * inverse(fact[n - r]) % mod * inverse(fact[r])) % mod);
}

void solve();
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  // cin >> t;
  rep(i, t) {
    // cout << "Case #" << i+1 << ": ";
    solve();
  }
}

int h, w, k;
int x[2], y[2];
vector<vector<char>> grid;
vector<vector<int>> vis;
vector<vector<int>> step;

void bfs(set<pii> &hev, int stc) {
  set<pii> nhev;
  for (auto x : hev) {
    int u = x.ff, v = x.ss;
    // cout << u << ' ' << v << ' ' << vis[u][v] << endl;
    if (vis[u][v] != -1)
      continue;
    // cout << "Hi" << endl;
    step[u][v] = stc;
    vis[u][v] = 1;
    int hi = k - 1;
    for (int i = u - 1; i >= 0 && hi >= 0; i--, hi--) {
      // cout << vis[i][v] << endl;
      if (vis[i][v] != -1)
        break;
      // if(!vis[i][v])
      nhev.insert({i, v});
    }
    hi = k - 1;
    for (int i = u + 1; i < h && hi >= 0; i++, hi--) {
      if (vis[i][v] != -1)
        break;
      // if(!vis[i][v])
      nhev.insert({i, v});
    }
    hi = k - 1;
    for (int i = v - 1; i >= 0 && hi >= 0; i--, hi--) {
      if (vis[u][i] != -1)
        break;
      // if(!vis[u][i])
      nhev.insert({u, i});
    }
    hi = k - 1;
    for (int i = v + 1; i < w && hi >= 0; i++, hi--) {
      if (vis[u][i] != -1)
        break;
      // if(!vis[u][i])
      nhev.insert({u, i});
    }
  }
  if (vis[x[1]][y[1]] != -1)
    return;
  if (nhev.size() != 0)
    bfs(nhev, stc + 1);
  return;
}

void solve() {
  cin >> h >> w >> k;
  grid.resize(h);
  vis.resize(h);
  step.resize(h);
  set<pii> hev;
  rep(i, h) {
    grid[i].resize(w);
    vis[i].resize(w);
    step[i].resize(w);
  }
  rep(i, 2) {
    cin >> x[i] >> y[i];
    x[i]--, y[i]--;
  }
  hev.insert({x[0], y[0]});
  rep(i, h) rep(j, w) step[i][j] = -1, vis[i][j] = -1;
  rep(i, h) rep(j, w) {
    cin >> grid[i][j];
    if (grid[i][j] == '@')
      vis[i][j] = 1;
  }
  bfs(hev, 0);
  cout << step[x[1]][y[1]];
}