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

/*input
3 5 2
3 2 3 4
.....
.@..@
..@..
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>

using namespace std;

int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
struct NOD {
  int x, y;
} u, v;

queue<NOD> que;

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  int bx, by, ex, ey;
  cin >> bx >> by >> ex >> ey;
  char mp[n + 5][m + 5];
  int d[n + 5][m + 5];
  for (int i = 1; i <= n; i++)
    scanf("%s", mp[i] + 1);
  memset(d, 33, sizeof d);

  d[bx][by] = 0;
  for (que.push((NOD){bx, by}); !que.empty(); que.pop()) {
    u = que.front();
    if (u.x == ex && u.y == ey)
      return 0 * printf("%d", d[u.x][u.y]);
    for (int di = 0; di < 4; di++) {
      for (int i = 1; i <= k; i++) {
        v = (NOD){u.x + dx[di] * i, u.y + dy[di] * i};
        if (mp[v.x][v.y] != '.' || d[v.x][v.y] <= d[u.x][u.y])
          break;
        if (d[v.x][v.y] > d[u.x][u.y] + 1) {
          d[v.x][v.y] = d[u.x][u.y] + 1;
          que.push(v);
        }
      }
    }
  }
  printf("-1");

  return 0;
}

Error: TLE

Fixed submission:

/*input
3 5 2
3 2 3 4
.....
.@..@
..@..

*/

/*
                                                    ______________
                                                          |       )
                                                          |   )   /
                                                          |______/
                                                    |     |      \\
                                                    |     |   )   \\
                                                    |_____|_______)

*/
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef long double ld;

#define pb push_back
#define all(c) c.begin(), c.end()
#define allr(c) c.rbegin(), c.rend()
int mod = 1000000007;
const int inf = 1034567891;
const ll LL_INF = 1234567890123456789ll;
#define PI 3.14159265
#define endl '\\n'
#define F first
#define S second
#define debug(x) cout << #x << " = " << x << endl;
#define TRACE

#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif

#define out(container)                                                         \\
  for (auto it : container)                                                    \\
    cout << it << " ";                                                         \\
  cout << endl;

template <typename T> T GCD(T a, T b) {
  ll t;
  while (a) {
    t = a;
    a = b % a;
    b = t;
  }
  return b;
}
template <typename T> string toString(T a) { return to_string(a); }
template <typename T> void toInt(string s, T &x) {
  stringstream str(s);
  str >> x;
}
inline int add(int x, int y) {
  x += y;
  if (x >= mod)
    x -= mod;
  return x;
}
inline int sub(int x, int y) {
  x -= y;
  if (x < 0)
    x += mod;
  return x;
}
inline int mul(int x, int y) { return (x * 1ll * y) % mod; }
inline int powr(int a, ll b) {
  int x = 1 % mod;
  while (b) {
    if (b & 1)
      x = mul(x, a);
    a = mul(a, a);
    b >>= 1;
  }
  return x;
}
inline int inv(int a) { return powr(a, mod - 2); }

const int N = 1e3 + 5;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int n, m, k;
  cin >> n >> m >> k;
  k = min(k, max(n, m));
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  char arr[n + 1][m + 1];
  set<int> row[n + 1], col[m + 1];
  int psum1[n + 1][m + 1], psum2[n + 1][m + 1];
  memset(psum1, 0, sizeof(psum1));
  memset(psum2, 0, sizeof(psum2));
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> arr[i][j];
      psum1[i][j] = psum1[i][j - 1];
      if (arr[i][j] != '@')
        row[i].insert(j), psum1[i][j]++;
    }
  }
  for (int j = 1; j <= m; j++) {
    for (int i = 1; i <= n; i++) {
      psum2[i][j] = psum2[i - 1][j];
      if (arr[i][j] != '@')
        col[j].insert(i), psum2[i][j]++;
    }
  }

  // priority_queue<pair<int, PII>, vector<pair<int, PII>>, greater<pair<int,
  // PII>>> qu;
  set<pair<int, PII>> qu;
  qu.insert({0, {x1, y1}});
  int ans = inf;
  row[x1].erase(y1);
  col[y1].erase(x1);
  while (!qu.empty()) {
    pair<int, PII> p = *qu.begin();
    qu.erase(qu.begin());
    int r = p.S.F, c = p.S.S, d = p.F;
    if (r == x2 && c == y2) {
      ans = min(ans, d);
      break;
    }
    vector<int> temp;
    auto x = row[r].lower_bound(c - k);
    for (auto y = x; y != row[r].end(); y++) {
      auto it = *y;
      int a = abs(it - c);
      int cnt = psum1[r][max(it, c)] - psum1[r][min(it, c) - 1];
      if (a <= k && cnt == a + 1) {
        qu.insert({d + 1, {r, it}});
        temp.pb(it);
      } else if (a > k || it > c) {
        break;
      }
    }
    for (auto it : temp) {
      row[r].erase(it);
      col[it].erase(r);
    }
    temp.clear();
    x = col[c].lower_bound(r - k);
    for (auto y = x; y != col[c].end(); y++) {
      auto it = *y;
      int a = abs(it - r);
      int cnt = psum2[max(it, r)][c] - psum2[min(it, r) - 1][c];
      if (a <= k && cnt == a + 1) {
        qu.insert({d + 1, {it, c}});
        temp.pb(it);
      } else if (a > k || it > r) {
        break;
      }
    }
    for (auto it : temp) {
      col[c].erase(it);
      row[it].erase(c);
    }
  }
  if (ans == inf)
    ans = -1;

  cout << ans << endl;

  return 0;
}