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

// #include "bits/stdc++.h"
#include <algorithm>
#include <bitset>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

#ifdef LOCAL
#include "debug.h"
#define input                                                                  \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout);                                          \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#else
#define debug(...) 4
#define input                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#endif

using ll = long long;
const int N = 1e6 + 5;
const int mod = 1e9 + 7;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
int n, m, k;
string s[N];
vector<vector<int>> bfs(int xx, int yy) {
  auto check = [&](int x, int y) {
    if (x >= 0 and x < n and y >= 0 and y < m)
      return true;
    return false;
  };
  queue<pair<int, int>> q;
  q.push({xx, yy});
  vector<vector<int>> dist(n, vector<int>(m, 1e9));
  vector<vector<int>> vis(n, vector<int>(m, 0));

  dist[xx][yy] = 0;
  while (!q.empty()) {
    int x = q.front().first;
    int y = q.front().second;
    q.pop();
    vis[x][y] = 1;
    debug(x, y);
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k; j++) {
        int nx = x + dx[i] * j;
        int ny = y + dy[i] * j;
        if (!check(nx, ny))
          break;
        if (s[nx][ny] == '@')
          break;
        if (dist[nx][ny] >= dist[x][y] + 1) {
          if (vis[nx][ny] == 0) {
            dist[nx][ny] = dist[x][y] + 1;
            vis[nx][ny] = 1;
            q.push({nx, ny});
          }
        } else
          break;
      }
    }
  }
  return dist;
}
signed main() {
  input;
  cin >> n >> m >> k;
  int x1, x2, y1, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  x2--;
  y1--;
  y2--;

  for (int i = 0; i < n; i++) {
    cin >> s[i];
  }
  vector<vector<int>> d = bfs(x1, y1);
  cout << (d[x2][y2] == 1e9 ? -1 : d[x2][y2]);
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
int n, m, k;
string s[2000];
int dis[1005][1005], vis[1005][1005];
void bfs(int x, int y) {
  queue<pair<int, int>> q;
  q.push({x, y});
  dis[x][y] = 0;
  while (q.size()) {
    int x1 = q.front().first;
    int y1 = q.front().second;
    // cout<<x1<<y1<<' '<<dis[x1][y1]<<endl;
    q.pop();
    vis[x1][y1] = 1;
    for (int i = x1 + 1;
         i <= (x1 + k) && (i < n) && dis[i][y1] >= dis[x1][y1] + 1; i++) {

      if (s[i][y1] == '.') {
        if (!vis[i][y1]) {
          dis[i][y1] = dis[x1][y1] + 1;
          q.push({i, y1});
          vis[i][y1] = 1;
        }
      } else
        break;
    }
    for (int i = x1 - 1;
         i >= (x1 - k) && i >= 0 && dis[i][y1] >= dis[x1][y1] + 1; i--) {

      if (s[i][y1] == '.') {
        if (!vis[i][y1]) {
          dis[i][y1] = dis[x1][y1] + 1;
          q.push({i, y1});
          vis[i][y1] = 1;
        }
      } else
        break;
    }
    for (int i = y1 + 1; i <= y1 + k && i < m && s[x1][i] == '.' &&
                         dis[x1][i] >= dis[x1][y1] + 1;
         i++) {

      if (s[x1][i] == '.') {
        if (!vis[x1][i]) {
          dis[x1][i] = dis[x1][y1] + 1;
          q.push({x1, i});
          vis[x1][i] = 1;
        }
      } else
        break;
    }
    for (int i = y1 - 1;
         i >= (y1 - k) && i >= 0 && dis[x1][i] >= dis[x1][y1] + 1; i--) {

      if (s[x1][i] == '.') {
        if (!vis[x1][i]) {
          dis[x1][i] = dis[x1][y1] + 1;
          q.push({x1, i});
          vis[x1][i] = 1;
        }
      } else
        break;
    }
  }
}
int main() {
  int i, j;
  cin >> n >> m >> k;
  for (i = 0; i < n; i++)
    cin >> s[i];
  int x1, x2, y1, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  x2--;
  y1--;
  y2--;

  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++)
      dis[i][j] = 1e7;
  }
  bfs(x1, y1);
  if (dis[x2][y2] == 1e7)
    cout << "-1";
  else
    cout << dis[x2][y2];
}