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

#include <bits/stdc++.h>
using namespace std;

long long R, C, startr, startc, finishr, finishc, dist[1010][1010];
string arr[1010];
bool visited[1010][1010];

void Input() {
  cin >> R >> C >> startr >> startc >> finishr >> finishc;
  for (long long i = 0; i < R; i++) {
    cin >> arr[i];
  }
}

void Initialize() {
  for (long long i = 0; i < R; i++) {
    for (long long j = 0; j < C; j++) {
      dist[i][j] = -1;
    }
  }
}

bool check(long long i, long long j) {
  if (i < 0 || i >= R || j < 0 || j >= C) {
    return false;
  }
  if (arr[i][j] == '.')
    return true;
  if (arr[i][j] == '#')
    return false;
}

void bfs() {
  queue<pair<long long, long long>> q[1050000];
  q[0].push(make_pair(startr - 1, startc - 1));
  for (long long idx = 0; idx <= 1010000; idx++) {
    while (!q[idx].empty()) {
      long long i = q[idx].front().first, j = q[idx].front().second;
      q[idx].pop();
      if (visited[i][j])
        continue;
      visited[i][j] = true;
      dist[i][j] = idx;
      if (check(i + 1, j))
        q[idx].push(make_pair(i + 1, j));
      if (check(i - 1, j))
        q[idx].push(make_pair(i - 1, j));
      if (check(i, j + 1))
        q[idx].push(make_pair(i, j + 1));
      if (check(i, j - 1))
        q[idx].push(make_pair(i, j - 1));
      for (long long a = -2; a <= 2; a++) {
        for (long long b = -2; b <= 2; b++) {
          if (check(i + a, j + b))
            q[idx + 1].push(make_pair(i + a, j + b));
        }
      }
    }
  }
}

int main() {
  Input();
  Initialize();
  bfs();
  cout << dist[finishr - 1][finishc - 1] << '\\n';
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

long long R, C, startr, startc, finishr, finishc, dist[1010][1010];
string arr[1010];
bool visited[1010][1010];

void Input() {
  cin >> R >> C >> startr >> startc >> finishr >> finishc;
  for (long long i = 0; i < R; i++) {
    cin >> arr[i];
  }
}

void Initialize() {
  for (long long i = 0; i < R; i++) {
    for (long long j = 0; j < C; j++) {
      dist[i][j] = -1;
    }
  }
}

bool check(long long i, long long j) {
  if (i < 0 || i >= R || j < 0 || j >= C) {
    return false;
  }
  if (arr[i][j] == '.')
    return true;
  if (arr[i][j] == '#')
    return false;
}

void bfs() {
  queue<pair<long long, long long>> q[1001];
  q[0].push(make_pair(startr - 1, startc - 1));
  for (long long idx = 0; idx <= 1001; idx++) {
    while (!q[idx].empty()) {
      long long i = q[idx].front().first, j = q[idx].front().second;
      q[idx].pop();
      if (visited[i][j])
        continue;
      visited[i][j] = true;
      dist[i][j] = idx;
      if (check(i + 1, j))
        q[idx].push(make_pair(i + 1, j));
      if (check(i - 1, j))
        q[idx].push(make_pair(i - 1, j));
      if (check(i, j + 1))
        q[idx].push(make_pair(i, j + 1));
      if (check(i, j - 1))
        q[idx].push(make_pair(i, j - 1));
      for (long long a = -2; a <= 2; a++) {
        for (long long b = -2; b <= 2; b++) {
          if (check(i + a, j + b))
            q[idx + 1].push(make_pair(i + a, j + b));
        }
      }
    }
  }
}

int main() {
  Input();
  Initialize();
  bfs();
  cout << dist[finishr - 1][finishc - 1] << '\\n';
}