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

// To debug :  g++ -g file.cpp -o code
// to flush output : fflush(stdout) or cout.flush()
// cout<<setprecision(p)<<fixed<<var
// use 1LL<<i to for 64 bit shifting , (ll)2 because by default 2 is ll

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, a, n) for (i = a; i < n; ++i)
#define irep(i, n, a) for (i = n; i > a; --i)
#define mod 1000000007
#define pb push_back
#define big 9223372036854775807
#define big1 LONG_MAX
#define big2 ll_MAX
#define big3 1000000000
#define sma1 LONG_MIN
#define sma2 ll_MIN
#define sma3 -1000000000
#define mp make_pair
#define dub double
#define ivec vector<ll>
#define lvec vector<long long>
#define cvec vector<char>
#define svec vector<string>
#define mt make_tuple
#define MOD 998244353
#define pi acos(-1.0)
/*
#define ss second.second
#define ff first.first
#define f first
#define s second
#define sf second.first
#define fs first.second*/
#define IOS                                                                    \\
  std::ios::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)

const int N = 1e6 + 2;

set<int> r[N + 1];
set<int> c[N + 1];

vector<pair<int, int>> v;

set<int>::iterator it;

void remove() {

  for (auto u : v) {
    r[u.first].erase(u.second);
    c[u.second].erase(u.first);
  }

  v.clear();
}

int main() {

  int n, m, k, i, j, x1, y1, x2, y2, x, y;

  cin >> n >> m >> k;

  char a[n + 1][m + 1];
  int ans[n + 1][m + 1];

  cin >> x1 >> y1 >> x2 >> y2;

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

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

  for (i = 1; i <= n; ++i)
    for (j = 1; j <= m; ++j) {

      cin >> a[i][j];
      ans[i][j] = -1;
    }

  queue<pair<int, int>> q;

  q.push({x1, y1});
  ans[x1][y1] = 0;

  v.pb({x1, y1});

  remove();

  while (!q.empty()) {

    x = q.front().first;
    y = q.front().second;

    q.pop();

    if (!r[x].empty())
      for (it = r[x].lower_bound(y); it != r[x].end() && (*it - y) <= k; ++it) {
        if (a[x][*it] == '@')
          break;
        else {
          q.push({x, *it});
          v.pb({x, *it});
          ans[x][*it] = 1 + ans[x][y];
        }
      }

    if (!r[x].empty()) {

      it = r[x].lower_bound(y);

      if (it != r[x].begin())
        for (--it; (y - *it) <= k; --it) {

          if (a[x][*it] == '@')
            break;
          else {
            q.push({x, *it});
            v.pb({x, *it});
            ans[x][*it] = 1 + ans[x][y];
          }

          if (it == r[x].begin())
            break;
        }
    }

    if (!c[y].empty())
      for (it = c[y].lower_bound(x); it != c[y].end() && (*it - x) <= k; ++it) {
        if (a[*it][y] == '@')
          break;
        else {
          q.push({*it, y});
          v.pb({*it, y});
          ans[*it][y] = 1 + ans[x][y];
        }
      }

    if (!c[y].empty()) {

      it = c[y].lower_bound(x);

      if (it != c[y].begin())
        for (--it; (x - *it) <= k; --it) {
          if (a[*it][y] == '@')
            break;
          else {
            q.push({*it, y});
            v.pb({*it, y});
            ans[*it][y] = 1 + ans[x][y];
          }

          if (it == c[y].begin())
            break;
        }
    }

    remove();
  }

  cout << ans[x2][y2] << "\\n";

  return 0;
}

Error: 0

Fixed submission:

// To debug :  g++ -g file.cpp -o code
// to flush output : fflush(stdout) or cout.flush()
// cout<<setprecision(p)<<fixed<<var
// use 1LL<<i to for 64 bit shifting , (ll)2 because by default 2 is ll

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, a, n) for (i = a; i < n; ++i)
#define irep(i, n, a) for (i = n; i > a; --i)
#define mod 1000000007
#define pb push_back
#define big 9223372036854775807
#define big1 LONG_MAX
#define big2 ll_MAX
#define big3 1000000000
#define sma1 LONG_MIN
#define sma2 ll_MIN
#define sma3 -1000000000
#define mp make_pair
#define dub double
#define ivec vector<ll>
#define lvec vector<long long>
#define cvec vector<char>
#define svec vector<string>
#define mt make_tuple
#define MOD 998244353
#define pi acos(-1.0)
/*
#define ss second.second
#define ff first.first
#define f first
#define s second
#define sf second.first
#define fs first.second*/
#define IOS                                                                    \\
  std::ios::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)

const int N = 1001;

char a[N + 1][N + 1];
int ans[N + 1][N + 1];
set<int> r[N + 1];
set<int> c[N + 1];

vector<pair<int, int>> v;

set<int>::iterator it;

void remove() {

  for (auto u : v) {
    r[u.first].erase(u.second);
    c[u.second].erase(u.first);
  }

  v.clear();
}

int main() {

  int n, m, k, i, j, x1, y1, x2, y2, x, y;

  cin >> n >> m >> k;

  cin >> x1 >> y1 >> x2 >> y2;

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

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

  for (i = 1; i <= n; ++i)
    for (j = 1; j <= m; ++j) {

      cin >> a[i][j];
      ans[i][j] = -1;
    }

  queue<pair<int, int>> q;

  q.push({x1, y1});
  ans[x1][y1] = 0;

  v.pb({x1, y1});

  remove();

  while (!q.empty()) {

    x = q.front().first;
    y = q.front().second;

    q.pop();

    if (!r[x].empty())
      for (it = r[x].lower_bound(y); it != r[x].end() && (*it - y) <= k; ++it) {
        if (a[x][*it] == '@')
          break;
        else {
          q.push({x, *it});
          v.pb({x, *it});
          ans[x][*it] = 1 + ans[x][y];
        }
      }

    if (!r[x].empty()) {

      it = r[x].lower_bound(y);

      if (it != r[x].begin())
        for (--it; (y - *it) <= k; --it) {

          if (a[x][*it] == '@')
            break;
          else {
            q.push({x, *it});
            v.pb({x, *it});
            ans[x][*it] = 1 + ans[x][y];
          }

          if (it == r[x].begin())
            break;
        }
    }

    if (!c[y].empty())
      for (it = c[y].lower_bound(x); it != c[y].end() && (*it - x) <= k; ++it) {
        if (a[*it][y] == '@')
          break;
        else {
          q.push({*it, y});
          v.pb({*it, y});
          ans[*it][y] = 1 + ans[x][y];
        }
      }

    if (!c[y].empty()) {

      it = c[y].lower_bound(x);

      if (it != c[y].begin())
        for (--it; (x - *it) <= k; --it) {
          if (a[*it][y] == '@')
            break;
          else {
            q.push({*it, y});
            v.pb({*it, y});
            ans[*it][y] = 1 + ans[x][y];
          }

          if (it == c[y].begin())
            break;
        }
    }

    remove();
  }

  cout << ans[x2][y2] << "\\n";

  return 0;
}