Problem p02644 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
template <class c, class cmp = less<c>>
using ordered_set =
    tree<c, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif
template <class T> ostream &operator<<(ostream &os, vector<T> V) {
  os << "[ ";
  for (auto v : V)
    os << v << " ";
  return os << "]";
}
template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) {
  return os << "(" << P.first << "," << P.second << ")";
}
template <typename T, typename U>
pair<T, U> operator+(const pair<T, U> &l, const std::pair<T, U> &r) {
  return {l.first + r.first, l.second + r.second};
}
typedef long long int ll;
const ll mod = 1e9 + 7;
const int maxn = 200002;
#define endl '\n'
#define int ll
#define ld long double
// #define all(x) (x).begin(),(x).end()

queue<int> q;
bool vis[2001001];
int dist[2001010];
string s[2001001];
int n, m, K;
int x1, Y1, x2, y2;
int get(int i, int j) { return (i * m + j); }
set<int> col[1000001], row[1000001];
int di[4] = {1, 0, -1, 0};
int dj[4] = {0, 1, 0, -1};
bool isvalid(int i, int j) {
  return (i >= 0 and j >= 0 and i < n and j < m and s[i][j] != '@');
}
void bfs() {

  q.push(get(x1, Y1));
  dist[get(x1, Y1)] = 0;
  row[x1].erase(Y1);
  col[Y1].erase(x1);
  int cnt = 0;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    int i = u / m;
    int j = u % m;
    cnt++;
    assert(cnt < 100000000);
    int cur = dist[get(i, j)];
    if (!isvalid(i, j))
      continue;
    for (int k = 0; k < 4; k++) {

      int ni = i + di[k];
      int nj = j + dj[k];
      if (!isvalid(ni, nj))
        continue;
      if (di[k] == 1) {
        auto it = col[nj].lower_bound(i);
        while (it != col[nj].end() and abs(*it - i) <= K) {
          int val = *it;
          if (!isvalid(val, nj))
            break;
          dist[get(val, nj)] = cur + 1;
          col[nj].erase(val);
          row[val].erase(nj);
          q.push(get(val, nj));
          it = col[nj].lower_bound(i);
        }
      }
      if (di[k] == -1) {
        auto it = col[nj].lower_bound(i);
        while (it != col[nj].begin() and abs(*(--it) - i) <= K) {
          int val = *it;
          if (!isvalid(val, nj))
            break;
          dist[get(val, nj)] = cur + 1;
          col[nj].erase(val);
          row[val].erase(nj);
          q.push(get(val, nj));
          it = col[nj].lower_bound(i);
        }
      }
      if (dj[k] == 1) {
        auto it = row[ni].lower_bound(j);
        while (it != row[ni].end() and abs(*it - j) <= K) {
          int val = *it;
          if (!isvalid(ni, val))
            break;
          dist[get(ni, val)] = cur + 1;
          row[ni].erase(val);
          col[val].erase(ni);
          q.push(get(ni, val));
          it = row[ni].lower_bound(j);
        }
      }
      if (dj[k] == -1) {
        auto it = row[ni].lower_bound(j);
        while (it != row[ni].begin() and abs(*(--it) - j) <= K) {
          int val = *it;
          if (!isvalid(ni, val))
            break;
          dist[get(ni, val)] = cur + 1;
          row[ni].erase(val);
          col[val].erase(ni);
          q.push(get(ni, val));
          it = row[ni].lower_bound(j);
        }
      }
    }
  }
}
int32_t main() {
  IOS

          cin >>
      n >> m >> K;
  cin >> x1 >> Y1 >> x2 >> y2;
  x1--;
  Y1--;
  x2--;
  y2--;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      row[i].insert(j);
      col[j].insert(i);
    }
  }

  for (int i = 0; i < (n + 2) * (m + 2); i++)
    dist[i] = 1e9;
  bfs();
  if (dist[get(x2, y2)] > 1e7) {
    cout << -1 << endl;
  } else
    cout << dist[get(x2, y2)];
}

Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
template <class c, class cmp = less<c>>
using ordered_set =
    tree<c, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif
template <class T> ostream &operator<<(ostream &os, vector<T> V) {
  os << "[ ";
  for (auto v : V)
    os << v << " ";
  return os << "]";
}
template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) {
  return os << "(" << P.first << "," << P.second << ")";
}
template <typename T, typename U>
pair<T, U> operator+(const pair<T, U> &l, const std::pair<T, U> &r) {
  return {l.first + r.first, l.second + r.second};
}
typedef long long int ll;
const ll mod = 1e9 + 7;
const int maxn = 200002;
#define endl '\n'
#define int ll
#define ld long double
// #define all(x) (x).begin(),(x).end()

queue<int> q;
bool vis[2001001];
int dist[2001010];
string s[2001001];
int n, m, K;
int x1, Y1, x2, y2;
int get(int i, int j) { return (i * m + j); }
set<int> col[1000001], row[1000001];
int di[4] = {1, 0, -1, 0};
int dj[4] = {0, 1, 0, -1};
bool isvalid(int i, int j) {
  return (i >= 0 and j >= 0 and i < n and j < m and s[i][j] != '@');
}
void bfs() {

  q.push(get(x1, Y1));
  dist[get(x1, Y1)] = 0;
  row[x1].erase(Y1);
  col[Y1].erase(x1);
  int cnt = 0;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    int i = u / m;
    int j = u % m;
    cnt++;
    assert(cnt < 100000000);
    int cur = dist[get(i, j)];
    if (!isvalid(i, j))
      continue;
    for (int k = 0; k < 4; k++) {

      int ni = i + di[k];
      int nj = j + dj[k];
      if (!isvalid(ni, nj))
        continue;
      if (di[k] == 1) {
        auto it = col[nj].lower_bound(i);
        while (it != col[nj].end() and abs(*it - i) <= K) {
          int val = *it;
          if (!isvalid(val, nj))
            break;
          dist[get(val, nj)] = cur + 1;
          col[nj].erase(val);
          row[val].erase(nj);
          q.push(get(val, nj));
          it = col[nj].lower_bound(i);
        }
      }
      if (di[k] == -1) {
        auto it = col[nj].lower_bound(i);
        while (it != col[nj].begin() and abs(*(--it) - i) <= K) {
          int val = *it;
          if (!isvalid(val, nj))
            break;
          dist[get(val, nj)] = cur + 1;
          col[nj].erase(val);
          row[val].erase(nj);
          q.push(get(val, nj));
          it = col[nj].lower_bound(i);
        }
      }
      if (dj[k] == 1) {
        auto it = row[ni].lower_bound(j);
        while (it != row[ni].end() and abs(*it - j) <= K) {
          int val = *it;
          if (!isvalid(ni, val))
            break;
          dist[get(ni, val)] = cur + 1;
          row[ni].erase(val);
          col[val].erase(ni);
          q.push(get(ni, val));
          it = row[ni].lower_bound(j);
        }
      }
      if (dj[k] == -1) {
        auto it = row[ni].lower_bound(j);
        while (it != row[ni].begin() and abs(*(--it) - j) <= K) {
          int val = *it;
          if (!isvalid(ni, val))
            break;
          dist[get(ni, val)] = cur + 1;
          row[ni].erase(val);
          col[val].erase(ni);
          q.push(get(ni, val));
          it = row[ni].lower_bound(j);
        }
      }
    }
  }
}
int32_t main() {
  IOS

          cin >>
      n >> m >> K;
  cin >> x1 >> Y1 >> x2 >> y2;
  x1--;
  Y1--;
  x2--;
  y2--;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      row[i].insert(j);
      col[j].insert(i);
    }
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++)
      dist[get(i, j)] = 1e9;
  }
  bfs();
  if (dist[get(x2, y2)] > 1e7) {
    cout << -1 << endl;
  } else
    cout << dist[get(x2, y2)];
}