Problem p02644 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
const auto start_time = std::chrono::high_resolution_clock::now();
void timecalculater() {
#ifndef ONLINE_JUDGE
  auto end_time = std::chrono::high_resolution_clock::now();
  std::chrono::duration<double> diff = end_time - start_time;
  cerr << "Time Taken : " << diff.count() << "\\n";
#endif
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
void __print(int x) { cout << x; }
void __print(long x) { cout << x; }
void __print(long long x) { cout << x; }
void __print(unsigned x) { cout << x; }
void __print(unsigned long x) { cout << x; }
void __print(unsigned long long x) { cout << x; }
void __print(float x) { cout << x; }
void __print(double x) { cout << x; }
void __print(long double x) { cout << x; }
void __print(char x) { cout << '\\'' << x << '\\''; }
void __print(const char *x) { cout << '\\"' << x << '\\"'; }
void __print(const string &x) { cout << '\\"' << x << '\\"'; }
void __print(bool x) { cout << (x ? "true" : "false"); }
template <typename T, typename V> void __print(const pair<T, V> &x) {
  cout << '{';
  __print(x.first);
  cout << ',';
  __print(x.second);
  cout << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cout << '{';
  for (auto &i : x)
    cout << (f++ ? "," : ""), __print(i);
  cout << "}";
}
void _print() { cout << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cout << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define dbg(x...)                                                              \\
  cout << "[" << #x << "] = [";                                                \\
  _print(x)
#else
#define dbg(x...)
#endif
#define fastio                                                                 \\
  cin.sync_with_stdio(0);                                                      \\
  cin.tie(0);                                                                  \\
  cin.exceptions(cin.failbit);
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repA(i, a, n) for (int i = a; i <= (n); ++i)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define fill(a, v) memset(a, v, sizeof(a))
#define fst first
#define snd second
#define mp make_pair
#define pb push_back
#define endl '\\n'
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<long long int> vll;
typedef vector<pii> vii;
ll p = 1e9 + 7;
const int N = 2e5 + 5;
int main() {

  fastio;
  int T = 1;
  // cin>>T;
  while (T--) {
    int h, k, w;
    cin >> h >> w >> k;
    int x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    x1--;
    y1--;
    x2--;
    y2--;
    int ans[h][w];
    fill(ans, -1);
    char a[h][w];
    rep(i, h) rep(j, w) cin >> a[i][j];
    queue<pair<pair<int, int>, int>> q;
    q.push({{x1, y1}, 0});
    set<int> col[w];
    set<int> row[h];
    rep(i, h) {
      rep(j, w) {
        if (i == x1 && j == y1)
          continue;
        row[i].insert(j);
      }
    }
    rep(j, w) {
      rep(i, h) {
        if (i == x1 && j == y1)
          continue;
        col[j].insert(i);
      }
    }
    while (!q.empty()) {
      pair<pair<int, int>, int> p = q.front();
      q.pop();
      int i = p.fst.fst;
      int j = p.fst.snd;
      int d = p.snd;
      ans[i][j] = d;
      auto it = row[i].lower_bound(j);
      while (1) {
        if (sz(row[i]) == 0) {
          break;
        }
        if (it == row[i].end()) {
          it--;
        }
        int f = 0;
        int y = *it;
        if (it == row[i].begin()) {
          f = 1;
        }
        if (y >= j) {
          if (f)
            break;
          it--;
          continue;
        }

        if (y <= (j) && y >= (j - k)) {
          if (a[i][y] != '.') {
            break;
          }

          q.push({{i, y}, d + 1});
          if (f) {
            row[i].erase(it);
            col[y].erase(col[y].find(i));
            break;
          }
          row[i].erase(it--);
          col[y].erase(col[y].find(i));

        } else {
          break;
        }
      }
      it = row[i].lower_bound(j);
      while (it != row[i].end()) {
        int y = *it;
        if (y <= (j + k) && y >= (j)) {
          if (a[i][y] != '.') {
            break;
          }
          q.push({{i, y}, d + 1});
          it = row[i].erase(it);
          col[y].erase(col[y].find(i));
        } else {
          break;
        }
      }
      it = col[j].lower_bound(i);
      while (1) {
        if (sz(col[j]) == 0) {
          break;
        }
        if (it == col[j].end()) {
          it--;
        }
        int y = *it;
        int f = 0;
        if (it == col[j].begin()) {
          f = 1;
        }
        if (y >= i) {
          if (f)
            break;
          it--;
          continue;
        }
        if (y <= (i) && y >= (i - k)) {
          if (a[y][j] != '.') {
            break;
          }
          q.push({{y, j}, d + 1});
          if (f) {
            col[j].erase(it);
            row[y].erase(row[y].find(j));
            break;
          }
          col[j].erase(it--);
          row[y].erase(row[y].find(j));
        } else {
          break;
        }
      }
      it = col[j].lower_bound(i);
      while (it != col[j].end()) {
        int y = *it;
        if (y <= (i + k) && y >= (i)) {
          if (a[y][j] != '.') {
            break;
          }
          q.push({{y, j}, d + 1});
          it = col[j].erase(it);
          row[y].erase(row[y].find(j));
        } else {
          break;
        }
      }
    }
    cout << ans[x2][y2];
  }
  timecalculater();
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const auto start_time = std::chrono::high_resolution_clock::now();
void timecalculater() {
#ifndef ONLINE_JUDGE
  auto end_time = std::chrono::high_resolution_clock::now();
  std::chrono::duration<double> diff = end_time - start_time;
  cerr << "Time Taken : " << diff.count() << "\\n";
#endif
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
void __print(int x) { cout << x; }
void __print(long x) { cout << x; }
void __print(long long x) { cout << x; }
void __print(unsigned x) { cout << x; }
void __print(unsigned long x) { cout << x; }
void __print(unsigned long long x) { cout << x; }
void __print(float x) { cout << x; }
void __print(double x) { cout << x; }
void __print(long double x) { cout << x; }
void __print(char x) { cout << '\\'' << x << '\\''; }
void __print(const char *x) { cout << '\\"' << x << '\\"'; }
void __print(const string &x) { cout << '\\"' << x << '\\"'; }
void __print(bool x) { cout << (x ? "true" : "false"); }
template <typename T, typename V> void __print(const pair<T, V> &x) {
  cout << '{';
  __print(x.first);
  cout << ',';
  __print(x.second);
  cout << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cout << '{';
  for (auto &i : x)
    cout << (f++ ? "," : ""), __print(i);
  cout << "}";
}
void _print() { cout << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cout << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define dbg(x...)                                                              \\
  cout << "[" << #x << "] = [";                                                \\
  _print(x)
#else
#define dbg(x...)
#endif
#define fastio                                                                 \\
  cin.sync_with_stdio(0);                                                      \\
  cin.tie(0);                                                                  \\
  cin.exceptions(cin.failbit);
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repA(i, a, n) for (int i = a; i <= (n); ++i)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define fill(a, v) memset(a, v, sizeof(a))
#define fst first
#define snd second
#define mp make_pair
#define pb push_back
#define endl '\\n'
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<long long int> vll;
typedef vector<pii> vii;
ll p = 1e9 + 7;
const int N = 2e5 + 5;
int main() {

  fastio;
  int T = 1;
  // cin>>T;
  while (T--) {
    int h, k, w;
    cin >> h >> w >> k;
    int x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    x1--;
    y1--;
    x2--;
    y2--;
    int ans[h][w];
    fill(ans, -1);
    char a[h][w];
    rep(i, h) rep(j, w) cin >> a[i][j];
    queue<pair<pair<int, int>, int>> q;
    q.push({{x1, y1}, 0});
    set<int> col[w];
    set<int> row[h];
    rep(i, h) {
      rep(j, w) {
        if (i == x1 && j == y1)
          continue;
        row[i].insert(j);
      }
    }
    rep(j, w) {
      rep(i, h) {
        if (i == x1 && j == y1)
          continue;
        col[j].insert(i);
      }
    }
    while (!q.empty()) {
      pair<pair<int, int>, int> p = q.front();
      q.pop();
      int i = p.fst.fst;
      int j = p.fst.snd;
      int d = p.snd;
      auto it = row[i].lower_bound(j);
      ans[i][j] = d;
      // dbg(i,j);
      while (1) {
        if (sz(row[i]) == 0) {
          break;
        }
        int f = 0;
        int y = *it;
        if (it == row[i].begin()) {
          f = 1;
        }
        if (y >= j) {
          if (f)
            break;
          it--;
          continue;
        }
        if (y <= (j) && y >= (j - k)) {
          if (a[i][y] != '.') {
            break;
          }
          q.push({{i, y}, d + 1});
          if (f) {
            row[i].erase(it);
            col[y].erase(col[y].find(i));
            break;
          }
          row[i].erase(it--);
          col[y].erase(col[y].find(i));
        } else {
          break;
        }
      }
      it = row[i].lower_bound(j);
      while (it != row[i].end()) {
        int y = *it;
        if (y <= (j + k) && y >= (j)) {
          if (a[i][y] != '.') {
            break;
          }
          q.push({{i, y}, d + 1});
          it = row[i].erase(it);
          col[y].erase(col[y].find(i));
        } else {
          break;
        }
      }
      it = col[j].lower_bound(i);
      while (1) {
        if (sz(col[j]) == 0) {
          break;
        }
        int y = *it;
        int f = 0;
        if (it == col[j].begin()) {
          f = 1;
        }
        if (y >= i) {
          if (f)
            break;
          it--;
          continue;
        }
        if (y <= (i) && y >= (i - k)) {
          if (a[y][j] != '.') {
            break;
          }
          q.push({{y, j}, d + 1});
          if (f) {
            col[j].erase(it);
            row[y].erase(row[y].find(j));
            break;
          }
          col[j].erase(it--);
          row[y].erase(row[y].find(j));
        } else {
          break;
        }
      }
      it = col[j].lower_bound(i);
      while (it != col[j].end()) {
        int y = *it;
        if (y <= (i + k) && y >= (i)) {
          if (a[y][j] != '.') {
            break;
          }
          q.push({{y, j}, d + 1});
          it = col[j].erase(it);
          row[y].erase(row[y].find(j));
        } else {
          break;
        }
      }
    }
    cout << ans[x2][y2];
  }
  timecalculater();
  return 0;
}