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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define dd double
#define endl "\\n"
#define pb push_back
#define all(v) v.begin(), v.end()
#define mp make_pair
#define fi first
#define se second
#define vll vector<ll>
#define pll pair<ll, ll>
#define fo(i, n) for (int i = 0; i < n; i++)
#define fo1(i, n) for (int i = 1; i <= n; i++)
ll mod = 1000000007;
ll n, k, t, m, q, flag = 0;
ll power(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// #define ordered_set tree<int, null_type,less<int>,
// rb_tree_tag,tree_order_statistics_node_update> ordered_set s ;
// s.order_of_key(a) -- no. of elements strictly less than a s.find_by_order(i)
// -- itertor to ith element (0 indexed)
ll min(ll a, ll b) {
  if (a > b)
    return b;
  else
    return a;
}
ll max(ll a, ll b) {
  if (a > b)
    return a;
  else
    return b;
}
ll gcd(ll a, ll b) {
  if (b > a)
    return gcd(b, a);
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll l1, r1, l2, r2;
char c[1001][1001];
ll is[1001][1001];
vector<pair<ll, ll>> v[1000001];

void dfs(ll l, ll r, ll h) {
  if (is[l][r] == -1)
    is[l][r] = h;
  else
    return;
  if (l > 0)
    if (c[l - 1][r] == '.' && is[l - 1][r] == -1) {
      dfs(l - 1, r, h);
    }
  if (l < n - 1)
    if (c[l + 1][r] == '.' && is[l + 1][r] == -1) {
      dfs(l + 1, r, h);
    }
  if (r > 0)
    if (c[l][r - 1] == '.' && is[l][r - 1] == -1) {
      dfs(l, r - 1, h);
    }
  if (r < m - 1)
    if (c[l][r + 1] == '.' && is[l][r + 1] == -1) {
      dfs(l, r + 1, h);
    }

  for (int i = l - 2; i <= l + 2; i++)
    for (int j = r - 2; j <= r + 2; j++) {
      if (i >= 0 && i < n && j >= 0 && j < m)
        if (is[i][j] == -1 && c[i][j] == '.') {
          v[h + 1].pb(mp(i, j));
        }
    }
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifdef NOOBxCODER
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#else
#define NOOBxCODER 0
#endif
  // cin>>t;
  t = 1;
  while (t--) {
    cin >> n >> m;

    cin >> l1 >> r1 >> l2 >> r2;
    l1--;
    r1--;
    l2--;
    r2--;
    fo(i, n) fo(j, m) {
      is[i][j] = -1;
      cin >> c[i][j];
    }
    if (c[l1][r1] == '#') {
      cout << -1;
      return 0;
    }
    // is[l1][r1]=0;
    v[0].pb(mp(l1, r1));
    fo(i, 1000000) {
      fo(j, v[i].size()) { dfs(v[i][j].fi, v[i][j].se, i); }
    }
    cout << is[l2][r2];
  }
  cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC
       << "ms\\n";
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define dd double
#define endl "\\n"
#define pb push_back
#define all(v) v.begin(), v.end()
#define mp make_pair
#define fi first
#define se second
#define vll vector<ll>
#define pll pair<ll, ll>
#define fo(i, n) for (int i = 0; i < n; i++)
#define fo1(i, n) for (int i = 1; i <= n; i++)
ll mod = 1000000007;
ll n, k, t, m, q, flag = 0;
ll power(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// #define ordered_set tree<int, null_type,less<int>,
// rb_tree_tag,tree_order_statistics_node_update> ordered_set s ;
// s.order_of_key(a) -- no. of elements strictly less than a s.find_by_order(i)
// -- itertor to ith element (0 indexed)
ll min(ll a, ll b) {
  if (a > b)
    return b;
  else
    return a;
}
ll max(ll a, ll b) {
  if (a > b)
    return a;
  else
    return b;
}
ll gcd(ll a, ll b) {
  if (b > a)
    return gcd(b, a);
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll l1, r1, l2, r2;
char c[1001][1001];
ll is[1001][1001];
vector<pair<ll, ll>> v[10001];

void dfs(ll l, ll r, ll h) {
  if (is[l][r] == -1)
    is[l][r] = h;
  else
    return;
  if (l > 0)
    if (c[l - 1][r] == '.' && is[l - 1][r] == -1) {
      dfs(l - 1, r, h);
    }
  if (l < n - 1)
    if (c[l + 1][r] == '.' && is[l + 1][r] == -1) {
      dfs(l + 1, r, h);
    }
  if (r > 0)
    if (c[l][r - 1] == '.' && is[l][r - 1] == -1) {
      dfs(l, r - 1, h);
    }
  if (r < m - 1)
    if (c[l][r + 1] == '.' && is[l][r + 1] == -1) {
      dfs(l, r + 1, h);
    }

  for (int i = l - 2; i <= l + 2; i++)
    for (int j = r - 2; j <= r + 2; j++) {
      if (i >= 0 && i < n && j >= 0 && j < m)
        if (is[i][j] == -1 && c[i][j] == '.') {
          v[h + 1].pb(mp(i, j));
        }
    }
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifdef NOOBxCODER
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#else
#define NOOBxCODER 0
#endif
  // cin>>t;
  t = 1;
  while (t--) {
    cin >> n >> m;

    cin >> l1 >> r1 >> l2 >> r2;
    l1--;
    r1--;
    l2--;
    r2--;
    fo(i, n) fo(j, m) {
      is[i][j] = -1;
      cin >> c[i][j];
    }
    if (c[l1][r1] == '#') {
      cout << -1;
      return 0;
    }
    // is[l1][r1]=0;
    v[0].pb(mp(l1, r1));
    fo(i, 10001) {
      fo(j, v[i].size()) { dfs(v[i][j].fi, v[i][j].se, i); }
    }
    cout << is[l2][r2];
  }
  cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC
       << "ms\\n";
  return 0;
}