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

#include <bits/stdc++.h>
#define ll long long
#define pii_ pair<int, int>
#define mp_ make_pair
#define pb push_back
#define fi first
#define se second
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define show1(a) cout << #a << " = " << a << endl
#define show2(a, b) cout << #a << " = " << a << "; " << #b << " = " << b << endl
using namespace std;
const ll INF = 1LL << 60;
const int inf = 1 << 30;
const int maxn = 2e5 + 5;
inline void fastio() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}

int n, m, k, x1, x2, y11, y2;
int dx[5] = {-1, 1, 0, 0};
int dy[5] = {0, 0, -1, 1};

int main() {
  fastio();
  cin >> n >> m >> k;
  vector<vector<char>> mp(n, vector<char>(m));
  vector<vector<int>> dis(n, vector<int>(m, inf));
  cin >> x1 >> y11 >> x2 >> y2;
  rep(i, 0, n - 1) rep(j, 0, m - 1) cin >> mp[i][j];
  x1--, y11--, x2--, y2--;
  queue<pii_> q;
  q.push(mp_(x1, y11));
  dis[x1][y11] = 0;
  while (!q.empty()) {
    int x = q.front().fi, y = q.front().se;
    q.pop();
    rep(i, 0, 3) {
      rep(j, 1, k) {
        int nx = x + j * dx[i], ny = y + j * dy[i];
        if (nx >= 0 && nx < n && ny >= 0 && ny < m && mp[nx][ny] == '.' &&
            (dis[nx][ny] == inf || dis[nx][ny] == dis[x][y] + 1)) {
          if (dis[nx][ny] == inf) {
            dis[nx][ny] = dis[x][y] + 1;
            q.push(mp_(nx, ny));
          }
        } else
          break;
      }
    }
  }
  if (dis[x2][y2] == inf)
    cout << -1 << endl;
  else
    cout << dis[x2][y2] << endl;
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define pii_ pair<int, int>
#define mp_ make_pair
#define pb push_back
#define fi first
#define se second
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define show1(a) cout << #a << " = " << a << endl
#define show2(a, b) cout << #a << " = " << a << "; " << #b << " = " << b << endl
using namespace std;
const ll INF = 1LL << 60;
const int inf = 1 << 30;
const int maxn = 1e6 + 5;
inline void fastio() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}

int n, m, S, x1, x2, y11, y2;
string mp[maxn];
void bfs() {
  vector<vector<int>> vis(n + 2);
  rep(i, 1, n) vis[i].resize(m + 2);
  rep(i, 1, n) rep(j, 1, m) vis[i][j] = 0;

  vector<vector<int>> preX(n + 2);
  rep(i, 1, n) preX[i].resize(m + 2);
  rep(i, 1, n) rep(j, 1, m) preX[i][j] = i - 1;

  vector<vector<int>> bakX(n + 2);
  rep(i, 1, n) bakX[i].resize(m + 2);
  rep(i, 1, n) rep(j, 1, m) bakX[i][j] = i + 1;

  vector<vector<int>> preY(n + 2);
  rep(i, 1, n) preY[i].resize(m + 2);
  rep(i, 1, n) rep(j, 1, m) preY[i][j] = j - 1;

  vector<vector<int>> bakY(n + 2);
  rep(i, 1, n) bakY[i].resize(m + 2);
  rep(i, 1, n) rep(j, 1, m) bakY[i][j] = j + 1;

  vector<vector<int>> dis(n + 2);
  rep(i, 1, n) dis[i].resize(m + 2);
  queue<pii_> q;
  stack<int> st;
  q.push(mp_(x1, y11));
  vis[x1][y11] = 1;
  dis[x1][y11] = 0;
  while (!q.empty()) {
    pii_ now = q.front();
    q.pop();
    int x = now.fi, y = now.se;
    show2(x, y);
    int prex = preX[x][y], bakx = bakX[x][y], prey = preY[x][y],
        baky = bakY[x][y];
    while (x - prex <= S) {
      show1("111");
      show2(prex, y);
      if (prex < 1 || prex > n)
        break;
      if (mp[prex][y - 1] == '@')
        break;
      if (!vis[prex][y]) {
        vis[prex][y] = 1;
        dis[prex][y] = dis[x][y] + 1;
        q.push(mp_(prex, y));
        st.push(prex);
        bakX[preX[prex][y]][y] = bakX[prex][y];
        prex = preX[prex][y];
      }
    }
    while (!st.empty()) {
      int xx = st.top();
      st.pop();
      preX[xx][y] = preX[preX[xx][y]][y];
      bakY[bakY[xx][y]][y] = bakY[xx][y];
      preY[preY[xx][y]][y] = preY[xx][y];
    }
    while (bakx - x <= S) {
      show1("222");
      if (bakx < 1 || bakx > n)
        break;
      if (mp[bakx][y - 1] == '@')
        break;
      if (!vis[bakx][y]) {
        vis[bakx][y] = 1;
        dis[bakx][y] = dis[x][y] + 1;
        st.push(bakx);
        q.push(mp_(bakx, y));
        preX[bakX[bakx][y]][y] = preX[bakx][y];
        bakx = bakX[bakx][y];
      }
    }
    while (!st.empty()) {
      int xx = st.top();
      st.pop();
      bakX[xx][y] = bakX[bakX[xx][y]][y];
      bakY[bakY[xx][y]][y] = bakY[xx][y];
      preY[preY[xx][y]][y] = preY[xx][y];
    }
    while (y - prey <= S) {
      show1("333");
      if (prey < 1 || prey > m)
        break;
      if (mp[x][prey - 1] == '@')
        break;
      show2(x, prey);
      if (!vis[x][prey]) {
        vis[x][prey] = 1;
        dis[x][prey] = dis[x][y] + 1;
        st.push(prey);
        q.push(mp_(x, prey));
        bakY[x][preY[x][prey]] = bakY[x][prey];
        prey = preY[x][prey];
      }
    }
    while (!st.empty()) {
      int yy = st.top();
      st.pop();
      preY[x][yy] = preY[x][preY[x][yy]];
      bakX[x][bakX[x][yy]] = bakY[x][yy];
      preX[x][preY[x][yy]] = preY[x][yy];
    }
    while (baky - y <= S) {
      show1("444");
      if (baky < 1 || baky > m)
        break;
      if (mp[x][baky - 1] == '@')
        break;
      if (!vis[x][baky]) {
        vis[x][baky] = 1;
        dis[x][baky] = dis[x][y] + 1;
        st.push(baky);
        q.push(mp_(x, baky));
        preY[x][bakY[x][baky]] = preY[x][baky];
        baky = bakY[x][baky];
      }
    }
    while (!st.empty()) {
      int yy = st.top();
      st.pop();
      bakY[x][yy] = bakY[x][bakY[x][yy]];
      bakX[x][bakX[x][yy]] = bakY[x][yy];
      preX[x][preY[x][yy]] = preY[x][yy];
    }
  }
  if (!vis[x2][y2])
    cout << -1 << endl;
  else
    cout << dis[x2][y2] << endl;
}
int main() {
  fastio();
  cin >> n >> m >> S >> x1 >> y11 >> x2 >> y2;
  rep(i, 1, n) cin >> mp[i];
  bfs();
  return 0;
}