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

#include <bits/stdc++.h>
typedef long long ll;
#define mod 1000000007
#define INF 1000000000
#define endl "\\n"
#define all(s) s.begin(), s.end()
#define pb push_back
#define uno unordered_map
#define pi 3.1415926536
#define dig(a) fixed << setprecision(a)
#define w(zz)                                                                  \\
  int zz;                                                                      \\
  cin >> zz;                                                                   \\
  while (zz--)
#define foreach(var, i) for (auto i = var.begin(); i != var.end(); i++)
#define fast()                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
ll power(ll a, ll n) {
  ll res = 1;
  while (n) {
    if (n % 2) {
      res *= a;
    }
    n >>= 1;
    a *= a;
  }
  return res;
}
ll powerm(ll a, ll n, ll m) {
  ll res = 1;
  while (n) {
    if (n % 2) {
      res = (res * a) % m;
    }
    n >>= 1;
    a = (a * a) % m;
  }
  return res;
}
using namespace std;

ll n, m;

ll mat[1005][1005];
ll vis[1005][1005];

ll dx[4] = {0, 0, 1, -1};
ll dy[4] = {1, -1, 0, 0};

string s;

bool valid(ll r, ll c) { return (r >= 0 && r < n && c >= 0 && c < m); }

int main() {
  fast();

  cin >> n >> m;
  ll a, b, c, d;
  cin >> a >> b >> c >> d;
  --a;
  --b;
  --c;
  --d;
  for (ll i = 0; i < n; i++) {
    cin >> s;
    for (ll j = 0; j < m; j++) {
      mat[i][j] = (s[j] == '.');
    }
  }

  deque<pair<pair<ll, ll>, ll>> dq;
  dq.push_front(make_pair(make_pair(a, b), 0));

  bool f = true;

  while (!dq.empty()) {

    pair<pair<ll, ll>, ll> x = dq.front();
    dq.pop_front();

    pair<ll, ll> pos = x.first;

    if (vis[pos.first][pos.second])
      continue;
    vis[pos.first][pos.second] = 1;

    if (pos.first == c && pos.second == d) {
      cout << x.second << endl;
      f = false;
      break;
    }

    for (ll i = 0; i < 4; i++) {

      ll xx = pos.first + dx[i];
      ll yy = pos.second + dy[i];

      if (valid(xx, yy) && mat[xx][yy] && !vis[xx][yy]) {
        dq.push_front(make_pair(make_pair(xx, yy), x.second));
      }
    }
    for (ll i = -2; i <= 2; i++) {
      for (ll j = -2; j <= 2; j++) {
        ll xx = pos.first + i;
        ll yy = pos.second + j;

        if (valid(xx, yy) && mat[xx][yy] && !vis[xx][yy]) {
          dq.push_back(make_pair(make_pair(xx, yy), x.second + 1));
        }
      }
    }
  }
  if (f)
    cout << -1 << endl;

  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
typedef long long ll;
#define mod 1000000007
#define INF 1000000000
#define endl "\\n"
#define all(s) s.begin(), s.end()
#define pb push_back
#define uno unordered_map
#define pi 3.1415926536
#define dig(a) fixed << setprecision(a)
#define w(zz)                                                                  \\
  int zz;                                                                      \\
  cin >> zz;                                                                   \\
  while (zz--)
#define foreach(var, i) for (auto i = var.begin(); i != var.end(); i++)
#define fast()                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
ll power(ll a, ll n) {
  ll res = 1;
  while (n) {
    if (n % 2) {
      res *= a;
    }
    n >>= 1;
    a *= a;
  }
  return res;
}
ll powerm(ll a, ll n, ll m) {
  ll res = 1;
  while (n) {
    if (n % 2) {
      res = (res * a) % m;
    }
    n >>= 1;
    a = (a * a) % m;
  }
  return res;
}
using namespace std;

ll n, m, a, b, c, d;

ll mat[1005][1005];
ll vis[1005][1005];

ll dx[4] = {0, 0, 1, -1};
ll dy[4] = {1, -1, 0, 0};

bool valid(ll r, ll c) { return (r >= 0 && r < n && c >= 0 && c < m); }

int main() {
  fast();

  cin >> n >> m >> a >> b >> c >> d;
  string s;
  for (ll i = 0; i < n; i++) {
    cin >> s;
    for (ll j = 0; i < m; j++) {
      mat[i][j] = (s[j] == '.');
    }
  }
  --a;
  --b;
  --c;
  --d;

  deque<pair<pair<ll, ll>, ll>> dq;
  dq.push_front(make_pair(make_pair(a, b), 0));

  bool f = true;

  while (!dq.empty()) {

    pair<pair<ll, ll>, ll> x = dq.front();
    dq.pop_front();

    pair<ll, ll> pos = x.first;

    if (vis[pos.first][pos.second])
      continue;
    vis[pos.first][pos.second] = 1;

    if (pos.first == c && pos.second == d) {
      cout << x.second << endl;
      f = false;
      break;
    }

    for (ll i = 0; i < 4; i++) {

      ll xx = pos.first + dx[i];
      ll yy = pos.second + dy[i];

      if (valid(xx, yy) && mat[xx][yy] && !vis[xx][yy]) {
        dq.push_front(make_pair(make_pair(xx, yy), x.second));
      }
    }
    for (ll i = -2; i <= 2; i++) {
      for (ll j = -2; j <= 2; j++) {
        ll xx = pos.first + i;
        ll yy = pos.second + j;

        if (valid(xx, yy) && mat[xx][yy] && !vis[xx][yy]) {
          dq.push_back(make_pair(make_pair(xx, yy), x.second + 1));
        }
      }
    }
  }
  if (f)
    cout << -1 << endl;

  return 0;
}