Problem p02644 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

const int N = 200005;

int n, m, k;
vector<vector<char>> mat;
vector<vector<int>> dist;

set<int> r[N], c[N];

void bfs(int x1, int y1, int x2, int y2) {
  dist.assign(n, vector<int>(m, -1));
  dist[x1][y1] = 0;
  queue<pair<int, int>> q;
  r[x1].erase(y1);
  c[y1].erase(x1);
  q.emplace(x1, y1);
  while (!q.empty()) {
    int x, y;
    tie(x, y) = q.front();
    q.pop();
    {
      auto it = r[x].lower_bound(y);
      while (it != r[x].end() and *it - y <= k) {
        if (mat[x][*it] == '@')
          break;
        if (dist[x][*it] == -1) {
          dist[x][*it] = dist[x][y] + 1;
          q.emplace(x, *it);
        }
        it = r[x].erase(it);
      }
      while (it != r[x].begin()) {
        it--;
        if (y - *it > k or mat[x][*it] == '@')
          break;
        if (dist[x][*it] == -1) {
          dist[x][*it] = dist[x][y] + 1;
          q.emplace(x, *it);
        }
        it = r[x].erase(it);
      }
    }
    {
      auto it = c[y].lower_bound(x);
      while (it != c[y].end() and *it - x <= k) {
        if (mat[*it][y] == '@')
          break;
        if (dist[*it][y] == -1) {
          dist[*it][y] = dist[x][y] + 1;
          q.emplace(*it, y);
        }
        it = c[y].erase(it);
      }
      while (it != c[y].begin()) {
        it--;
        if (x - *it > k)
          break;
        if (mat[*it][y] == '@')
          break;
        if (dist[*it][y] == -1) {
          dist[*it][y] = dist[x][y] + 1;
          q.emplace(*it, y);
        }
        it = c[y].erase(it);
      }
    }
  }
}

int main() {
  scanf("%d %d %d", &n, &m, &k);
  int x1, y1, x2, y2;
  scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
  mat.assign(n, vector<char>(m));

  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      scanf(" %c", &mat[i][j]);

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

  x1--, y1--, x2--, y2--;
  bfs(x1, y1, x2, y2);

  printf("%d\n", dist[x2][y2]);
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

const int N = 1000006;

int n, m, k;
vector<vector<char>> mat;
vector<vector<int>> dist;

set<int> r[N], c[N];

void bfs(int x1, int y1, int x2, int y2) {
  dist.assign(n, vector<int>(m, -1));
  dist[x1][y1] = 0;
  queue<pair<int, int>> q;
  r[x1].erase(y1);
  c[y1].erase(x1);
  q.emplace(x1, y1);
  while (!q.empty()) {
    int x, y;
    tie(x, y) = q.front();
    q.pop();
    {
      auto it = r[x].lower_bound(y);
      while (it != r[x].end() and *it - y <= k) {
        if (mat[x][*it] == '@')
          break;
        if (dist[x][*it] == -1) {
          dist[x][*it] = dist[x][y] + 1;
          q.emplace(x, *it);
        }
        it = r[x].erase(it);
      }
      while (it != r[x].begin()) {
        it--;
        if (y - *it > k or mat[x][*it] == '@')
          break;
        if (dist[x][*it] == -1) {
          dist[x][*it] = dist[x][y] + 1;
          q.emplace(x, *it);
        }
        it = r[x].erase(it);
      }
    }
    {
      auto it = c[y].lower_bound(x);
      while (it != c[y].end() and *it - x <= k) {
        if (mat[*it][y] == '@')
          break;
        if (dist[*it][y] == -1) {
          dist[*it][y] = dist[x][y] + 1;
          q.emplace(*it, y);
        }
        it = c[y].erase(it);
      }
      while (it != c[y].begin()) {
        it--;
        if (x - *it > k)
          break;
        if (mat[*it][y] == '@')
          break;
        if (dist[*it][y] == -1) {
          dist[*it][y] = dist[x][y] + 1;
          q.emplace(*it, y);
        }
        it = c[y].erase(it);
      }
    }
  }
}

int main() {
  scanf("%d %d %d", &n, &m, &k);
  int x1, y1, x2, y2;
  scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
  mat.assign(n, vector<char>(m));

  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      scanf(" %c", &mat[i][j]);

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

  x1--, y1--, x2--, y2--;
  bfs(x1, y1, x2, y2);

  printf("%d\n", dist[x2][y2]);
}