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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define ll long long
#define pal pair<ll, ll>
#define pad pair<double, double>
#define PB push_back
#define MP make_pair
#define F first
#define S second

int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int dist[1002][1002];
int h, ww;
int x, y, z, w;
deque<pair<int, int>> que;

int rec() {
  dist[x][y] = 0;
  que.PB(MP(x, y));
  while (que.size()) {
    pal t = que.front();
    que.pop_front();
    for (int i = 0; i < 4; i++) {
      if (dist[t.F + dx[i]][t.S + dy[i]] > dist[t.F][t.S]) {
        if (t.F + dx[i] > 1001 || t.F + dx[i] < 1 || t.S + dy[i] > 1001 ||
            t.S + dy[i] < 1)
          continue;
        dist[t.F + dx[i]][t.S + dy[i]] = dist[t.F][t.S];
        que.push_front(MP(t.F + dx[i], t.S + dy[i]));
      }
    }
    for (int i = -2; i < 3; i++) {
      for (int j = -2; j < 3; j++) {
        if (i == 0 && j == 0)
          continue;
        if (t.F + i > 1001 || t.F + i < 1 || t.S + j > 1001 || t.S + j < 1)
          continue;
        if (dist[t.F + i][t.S + j] > dist[t.F][t.S] + 1) {

          dist[t.F + i][t.S + j] = dist[t.F][t.S] + 1;
          que.push_back(MP(t.F + i, t.S + j));
        }
      }
    }
  }

  return 0;
}

int main() {

  cin >> h >> ww >> x >> y >> z >> w;

  fill(dist[0], dist[0] + 1002 * 1002, -1);

  for (ll i = 1; i <= h; i++) {
    for (ll j = 1; j <= ww; j++) {
      char tmp;
      cin >> tmp;
      if (tmp == '.') {
        dist[i][j] = 1000000;
      }
    }
  }
  rec();

  if (dist[z][w] == 1000000) {
    cout << -1 << endl;
  } else {
    cout << dist[z][w] << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define ll long long
#define pal pair<ll, ll>
#define pad pair<double, double>
#define PB push_back
#define MP make_pair
#define F first
#define S second

int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int dist[1002][1002];
int h, ww;
int x, y, z, w;
deque<pair<int, int>> que;

int rec() {
  dist[x][y] = 0;
  que.PB(MP(x, y));
  while (que.size()) {
    pal t = que.front();
    que.pop_front();
    for (int i = 0; i < 4; i++) {
      if (dist[t.F + dx[i]][t.S + dy[i]] > dist[t.F][t.S]) {
        if (t.F + dx[i] > 1001 || t.F + dx[i] < 1 || t.S + dy[i] > 1001 ||
            t.S + dy[i] < 1)
          continue;
        dist[t.F + dx[i]][t.S + dy[i]] = dist[t.F][t.S];
        que.push_front(MP(t.F + dx[i], t.S + dy[i]));
      }
    }
    for (int i = -2; i < 3; i++) {
      for (int j = -2; j < 3; j++) {
        if (dist[t.F + i][t.S + j] > dist[t.F][t.S] + 1) {
          if (i == 0 && j == 0)
            continue;
          if (t.F + i > 1001 || t.F + i < 1 || t.S + j > 1001 || t.S + j < 1)
            continue;
          dist[t.F + i][t.S + j] = dist[t.F][t.S] + 1;
          que.push_back(MP(t.F + i, t.S + j));
        }
      }
    }
  }

  return 0;
}

int main() {

  cin >> h >> ww >> x >> y >> z >> w;

  fill(dist[0], dist[0] + 1002 * 1002, -1);

  for (ll i = 1; i <= h; i++) {
    for (ll j = 1; j <= ww; j++) {
      char tmp;
      cin >> tmp;
      if (tmp == '.') {
        dist[i][j] = 1000000;
      }
    }
  }
  rec();

  if (dist[z][w] == 1000000) {
    cout << -1 << endl;
  } else {
    cout << dist[z][w] << endl;
  }
  return 0;
}