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 rrep(i, n) for (int i = n - 1; i >= 0; --i)
#define fi first
#define se second
using namespace std;
using lint = long long;
using uint = unsigned int;
using ulint = unsigned long long;
using ldouble = long double;
using pii = pair<int, int>;
using pli = pair<lint, lint>;
using pdd = pair<double, double>;
using pld = pair<ldouble, ldouble>;
using v1i = vector<int>;
using v1li = vector<lint>;
using v2i = vector<vector<int>>;
using v2li = vector<vector<lint>>;
using v3i = vector<vector<vector<int>>>;
using v3li = vector<vector<vector<lint>>>;
using v1b = vector<bool>;
using v2b = vector<vector<bool>>;
using v3b = vector<vector<vector<bool>>>;
using v1c = vector<char>;
using v2c = vector<vector<char>>;
using v3c = vector<vector<vector<char>>>;
constexpr lint mod1 = 1e9 + 7;
constexpr lint mod2 = 998244353;

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  int sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;
  --sx;
  --sy;
  --gx;
  --gy;
  vector<string> v(h);
  rep(i, h) cin >> v[i];
  v2i dis(h, v1i(w, -1));
  v2b see(h, v1b(w, 1));
  pii tmp = {gx, gy};
  deque<pii> d;
  d.push_back(tmp);
  dis[tmp.fi][tmp.se] = 0;
  see[tmp.fi][tmp.se] = 0;
  while (!d.empty()) {
    d.pop_front();
    int td = dis[tmp.fi][tmp.se] + 1;
    rep(i, k) {
      if (tmp.se + i + 1 >= w)
        break;
      if (v[tmp.fi][tmp.se + i + 1] == '@')
        break;
      if (see[tmp.fi][tmp.se + i + 1]) {
        see[tmp.fi][tmp.se + i + 1] = 0;
        d.push_back({tmp.fi, tmp.se + i + 1});
        dis[tmp.fi][tmp.se + i + 1] = td;
      }
      if (!see[tmp.fi][tmp.se + i + 1] && dis[tmp.fi][tmp.se + i + 1] < td)
        break;
    }
    rep(i, k) {
      if (tmp.se - i - 1 < 0)
        break;
      if (v[tmp.fi][tmp.se - i - 1] == '@')
        break;
      if (see[tmp.fi][tmp.se - i - 1]) {
        see[tmp.fi][tmp.se - i - 1] = 0;
        d.push_back({tmp.fi, tmp.se - i - 1});
        dis[tmp.fi][tmp.se - i - 1] = td;
      }
      if (!see[tmp.fi][tmp.se - i - 1] && dis[tmp.fi][tmp.se - i - 1] < td)
        break;
    }
    rep(i, k) {
      if (tmp.fi + i + 1 >= h)
        break;
      if (v[tmp.fi + i + 1][tmp.se] == '@')
        break;
      if (see[tmp.fi + i + 1][tmp.se]) {
        see[tmp.fi + i + 1][tmp.se] = 0;
        d.push_back({tmp.fi + i + 1, tmp.se});
        dis[tmp.fi + i + 1][tmp.se] = td;
      }
      if (!see[tmp.fi + i + 1][tmp.se] && dis[tmp.fi + i + 1][tmp.se] < td)
        break;
    }
    rep(i, k) {
      if (tmp.fi - i - 1 < 0)
        break;
      if (v[tmp.fi - i - 1][tmp.se] == '@')
        break;
      if (see[tmp.fi - i - 1][tmp.se]) {
        see[tmp.fi - i - 1][tmp.se] = 0;
        d.push_back({tmp.fi - i - 1, tmp.se});
        dis[tmp.fi - i - 1][tmp.se] = td;
      }
      if (!see[tmp.fi - i - 1][tmp.se] && dis[tmp.fi - i - 1][tmp.se] < td)
        break;
    }
    tmp = d.front();
  }
  cout << dis[sx][sy] << "\\n";
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n - 1; i >= 0; --i)
#define fi first
#define se second
using namespace std;
using lint = long long;
using uint = unsigned int;
using ulint = unsigned long long;
using ldouble = long double;
using pii = pair<int, int>;
using pli = pair<lint, lint>;
using pdd = pair<double, double>;
using pld = pair<ldouble, ldouble>;
using v1i = vector<int>;
using v1li = vector<lint>;
using v2i = vector<vector<int>>;
using v2li = vector<vector<lint>>;
using v3i = vector<vector<vector<int>>>;
using v3li = vector<vector<vector<lint>>>;
using v1b = vector<bool>;
using v2b = vector<vector<bool>>;
using v3b = vector<vector<vector<bool>>>;
using v1c = vector<char>;
using v2c = vector<vector<char>>;
using v3c = vector<vector<vector<char>>>;
constexpr lint mod1 = 1e9 + 7;
constexpr lint mod2 = 998244353;

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  int sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;
  --sx;
  --sy;
  --gx;
  --gy;
  vector<string> v(h);
  rep(i, h) cin >> v[i];
  v2i dis(h, v1i(w, -1));
  v2b see(h, v1b(w, 1));
  pii tmp = {gx, gy};
  deque<pii> d;
  d.push_back(tmp);
  dis[tmp.fi][tmp.se] = 0;
  see[tmp.fi][tmp.se] = 0;
  while (!d.empty()) {
    d.pop_front();
    int td = dis[tmp.fi][tmp.se] + 1;
    rep(i, k) {
      if (tmp.se + i + 1 >= w)
        break;
      if (v[tmp.fi][tmp.se + i + 1] == '@')
        break;
      if (see[tmp.fi][tmp.se + i + 1]) {
        see[tmp.fi][tmp.se + i + 1] = 0;
        d.push_back({tmp.fi, tmp.se + i + 1});
        dis[tmp.fi][tmp.se + i + 1] = td;
      }
    }
    rep(i, k) {
      if (tmp.se - i - 1 < 0)
        break;
      if (v[tmp.fi][tmp.se - i - 1] == '@')
        break;
      if (see[tmp.fi][tmp.se - i - 1]) {
        see[tmp.fi][tmp.se - i - 1] = 0;
        d.push_back({tmp.fi, tmp.se - i - 1});
        dis[tmp.fi][tmp.se - i - 1] = td;
      }
    }
    rep(i, k) {
      if (tmp.fi + i + 1 >= h)
        break;
      if (v[tmp.fi + i + 1][tmp.se] == '@')
        break;
      if (see[tmp.fi + i + 1][tmp.se]) {
        see[tmp.fi + i + 1][tmp.se] = 0;
        d.push_back({tmp.fi + i + 1, tmp.se});
        dis[tmp.fi + i + 1][tmp.se] = td;
      }
    }
    rep(i, k) {
      if (tmp.fi - i - 1 < 0)
        break;
      if (v[tmp.fi - i - 1][tmp.se] == '@')
        break;
      if (see[tmp.fi - i - 1][tmp.se]) {
        see[tmp.fi - i - 1][tmp.se] = 0;
        d.push_back({tmp.fi - i - 1, tmp.se});
        dis[tmp.fi - i - 1][tmp.se] = td;
      }
    }
    tmp = d.front();
  }
  cout << dis[sx][sy] << "\\n";
  return 0;
}