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

#include <bits/stdc++.h>
#define endl "\\n"
using namespace std;
typedef long long int lli;
const lli N = 1e6 + 5;
lli n, m, k;
set<lli> row[N], col[N];
vector<pair<lli, lli>> v;

bool inbounds(lli x, lli y) { return (x > 0 && x <= n && y > 0 && y <= m); }
void remove() {
  for (auto j : v) {
    row[j.first].erase(j.second);
    col[j.second].erase(j.first);
  }
  v.clear();
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  lli x1, y1, x2, y2, val, x, y;
  cin >> n >> m >> k >> x1 >> y1 >> x2 >> y2;
  lli i, j, num;
  char a[n + 1][m + 1];

  for (i = 1; i <= n; i++)
    for (j = 1; j <= m; j++)
      cin >> a[i][j];
  for (i = 1; i <= n; i++)
    for (j = 1; j <= m; j++)
      row[i].insert(j), col[j].insert(i);

  set<pair<lli, pair<lli, lli>>> s;
  s.insert({0, {x1, y1}});
  row[x1].erase(y1);
  col[y1].erase(x1);

  while (!s.empty()) {
    pair<lli, pair<lli, lli>> p = *s.begin();
    s.erase(p);

    val = p.first;
    x = p.second.first;
    y = p.second.second;
    if (x == x2 && y == y2)
      return cout << val, 0;

    auto it = row[x].lower_bound(y);
    while (it != row[x].end()) {
      num = *it;
      if (!inbounds(x, num) || num - y > k || a[x][num] == '@')
        break;

      s.insert({val + 1, {x, num}});
      v.push_back({x, num});
      it++;
    }
    remove();

    it = row[x].lower_bound(y);
    while (it != row[x].begin()) {
      it--;
      num = *it;
      if (!inbounds(x, num) || y - num > k || a[x][num] == '@')
        break;

      s.insert({val + 1, {x, num}});
      v.push_back({x, num});
    }
    remove();

    it = col[y].lower_bound(x);
    while (it != col[y].end()) {
      num = *it;
      if (!inbounds(num, y) || num - x > k || a[num][y] == '@')
        break;

      s.insert({val + 1, {num, y}});
      v.push_back({num, y});
      it++;
    }
    remove();

    it = col[y].lower_bound(x);
    while (it != col[y].begin()) {
      it--;
      num = *it;
      if (!inbounds(num, y) || x - num > k || a[num][y] == '@')
        break;

      s.insert({val + 1, {num, y}});
      v.push_back({num, y});
    }
    remove();
  }
  cout << -1;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define endl "\\n"
using namespace std;
typedef long long int lli;
lli n, m, k;

bool inbounds(lli x, lli y) { return (x > 0 && x <= n && y > 0 && y <= m); }

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  lli x1, y1, x2, y2, val, x, y;
  cin >> n >> m >> k >> x1 >> y1 >> x2 >> y2;
  lli i, j;
  char a[n + 1][m + 1];
  bool vis[n + 1][m + 1], taken[n + 1][m + 1];
  memset(vis, false, sizeof(vis));
  memset(taken, false, sizeof(taken));

  for (i = 1; i <= n; i++)
    for (j = 1; j <= m; j++)
      cin >> a[i][j];

  set<pair<lli, pair<lli, lli>>> s;
  s.insert({0, {x1, y1}});
  taken[x1][y1] = true;

  while (!s.empty()) {
    pair<lli, pair<lli, lli>> p = *s.begin();
    s.erase(p);

    val = p.first;
    x = p.second.first;
    y = p.second.second;
    if (vis[x][y])
      continue;
    vis[x][y] = true;
    if (x == x2 && y == y2)
      return cout << val, 0;

    for (i = 1; i <= k; i++) {
      if (inbounds(x + i, y) && a[x + i][y] == '.') {
        if (!vis[x + i][y] && !taken[x + i][y])
          s.insert({val + 1, {x + i, y}}), taken[x + i][y] = true;
      } else
        break;
    }
    for (i = 1; i <= k; i++) {
      if (inbounds(x - i, y) && a[x - i][y] == '.') {
        if (!vis[x - i][y] && !taken[x - i][y])
          s.insert({val + 1, {x - i, y}}), taken[x - i][y] = true;
      } else
        break;
    }
    for (i = 1; i <= k; i++) {
      if (inbounds(x, y + i) && a[x][y + i] == '.') {
        if (!vis[x][y + i] && !taken[x][y + i])
          s.insert({val + 1, {x, y + i}}), taken[x][y + i] = true;
      } else
        break;
    }
    for (i = 1; i <= k; i++) {
      if (inbounds(x, y - i) && a[x][y - i] == '.') {
        if (!vis[x][y - i] && !taken[x][y - i])
          s.insert({val + 1, {x, y - i}}), taken[x][y - i] = true;
      } else
        break;
    }
  }
  cout << -1;
  return 0;
}