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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma GCC optimize("unroll-loops")
using namespace std;
using ll = long long;
using ull = unsigned long long;

#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define pb(x) push_back(x)
ll mod = 1e14 + 7;
ll inf = 1e14 + 7;
template <typename T, typename U> T binpow(T a, U n) {
  if (n == 0)
    return 1;
  if (n % 2 == 1)
    return (binpow(a, n - 1) * a) % mod;
  else {
    ll b = binpow(a, n / 2) % mod;
    return (b * b) % mod;
  }
}
template <typename T> T gcd(T a, T b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
template <typename T> istream &operator>>(istream &str, vector<T> &vec) {
  for (auto &w : vec)
    str >> w;
  return str;
}
template <typename T> ostream &operator<<(ostream &str, vector<T> &vec) {
  for (auto &w : vec)
    str << w << ' ';
  return str;
}

int main() {
  ll h, w, ch, cw, dh, dw;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  vector<vector<ll>> vec(h, vector<ll>(w, inf));
  vector<vector<bool>> mapp(h, vector<bool>(w));
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      char c;
      cin >> c;
      if (c == '.')
        mapp[i][j] = 1;
    }
  }
  set<pair<ll, ll>> bfs;
  bfs.insert({0, ch * w + cw});
  while (!bfs.empty()) {
    auto g = *bfs.begin();
    bfs.erase(bfs.begin());
    int x = g.second % w;
    int y = g.second / w;
    vec[y][x] = min(g.first, vec[y][x]);
    if (x + 1 < w && x + 1 >= 0 && mapp[y][x + 1] == 1) {
      if (vec[y][x + 1] > vec[y][x]) {
        bfs.insert({
            vec[y][x],
            (y)*w + x + 1,
        });
      }
    }
    if (x - 1 < w && x - 1 >= 0 && mapp[y][x - 1] == 1) {
      if (vec[y][x - 1] > vec[y][x]) {
        bfs.insert({vec[y][x], (y)*w + x - 1});
      }
    }
    if (y + 1 < h && y + 1 >= 0 && mapp[y + 1][x] == 1) {
      if (vec[y + 1][x] > vec[y][x]) {
        bfs.insert({vec[y][x], (y + 1) * w + x});
      }
    }
    if (y - 1 < h && y - 1 >= 0 && mapp[y - 1][x] == 1) {
      if (vec[y - 1][x] > vec[y][x]) {
        bfs.insert({vec[y][x], (y - 1) * w + x});
      }
    }
    for (int i = -2; i <= 2; ++i)
      for (int j = -2; j <= 2; ++j) {
        if (x + i < w && x + i >= 0 && y + j < h && y + j >= 0 &&
            vec[y + j][x + i] > vec[y][x] + 1 && mapp[y + j][x + i] == 1) {
          bfs.insert({vec[y][x] + 1, (y + j) * w + x + i});
        }
      }
  }
  if (vec[dh][dw] >= inf)
    cout << -1;
  else
    cout << vec[dh][dw];
}

Error: WA

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma GCC optimize("unroll-loops")
using namespace std;
using ll = long long;
using ull = unsigned long long;

#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define pb(x) push_back(x)
ll mod = 1e9 + 7;
ll inf = 1e9 + 7;
template <typename T, typename U> T binpow(T a, U n) {
  if (n == 0)
    return 1;
  if (n % 2 == 1)
    return (binpow(a, n - 1) * a) % mod;
  else {
    ll b = binpow(a, n / 2) % mod;
    return (b * b) % mod;
  }
}
template <typename T> T gcd(T a, T b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
template <typename T> istream &operator>>(istream &str, vector<T> &vec) {
  for (auto &w : vec)
    str >> w;
  return str;
}
template <typename T> ostream &operator<<(ostream &str, vector<T> &vec) {
  for (auto &w : vec)
    str << w << ' ';
  return str;
}

int main() {
  ll h, w, ch, cw, dh, dw;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  vector<vector<ll>> vec(h, vector<ll>(w, inf));
  vector<vector<bool>> mapp(h, vector<bool>(w));
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      char c;
      cin >> c;
      if (c == '.')
        mapp[i][j] = 1;
    }
  }
  vec[ch][cw] = 0;
  for (int i = 0; i < 1e3; ++i) {
    ll u = ch - i, d = ch + i, l = cw - i, r = cw + i;
    for (int ud = max(0ll, u); ud <= min(d, h - 1); ud++) {
      if (l < w && l >= 0) {
        if (ud + 1 < h && ud + 1 >= 0 && mapp[ud + 1][l] == 1)
          vec[ud + 1][l] = min(vec[ud + 1][l], vec[ud][l]);
        if (ud - 1 < h && ud - 1 >= 0 && mapp[ud - 1][l] == 1)
          vec[ud - 1][l] = min(vec[ud - 1][l], vec[ud][l]);
        if (l + 1 < w && l + 1 >= 0 && mapp[ud][l + 1] == 1)
          vec[ud][l + 1] = min(vec[ud][l + 1], vec[ud][l]);
        if (l - 1 < w && l - 1 >= 0 && mapp[ud][l - 1] == 1)
          vec[ud][l - 1] = min(vec[ud][l - 1], vec[ud][l]);
        for (int q = -2; q <= 2; ++q) {
          for (int g = -2; g <= 2; ++g) {
            if (ud + q < h && ud + q >= 0 && l + g < w && l + g >= 0 &&
                mapp[ud + q][l + g] == 1)
              vec[ud + q][l + g] = min(vec[ud + q][l + g], vec[ud][l] + 1);
          }
        }
      }
      if (r < w && r >= 0) {
        if (ud + 1 < h && ud + 1 >= 0 && mapp[ud + 1][r] == 1)
          vec[ud + 1][r] = min(vec[ud + 1][r], vec[ud][r]);
        if (ud - 1 < h && ud - 1 >= 0 && mapp[ud - 1][r] == 1)
          vec[ud - 1][r] = min(vec[ud - 1][r], vec[ud][r]);
        if (r + 1 < w && r + 1 >= 0 && mapp[ud][r + 1] == 1)
          vec[ud][r + 1] = min(vec[ud][r + 1], vec[ud][r]);
        if (r - 1 < w && r - 1 >= 0 && mapp[ud][r - 1] == 1)
          vec[ud][r - 1] = min(vec[ud][r - 1], vec[ud][r]);
        for (int q = -2; q <= 2; ++q) {
          for (int g = -2; g <= 2; ++g) {
            if (ud + q < h && ud + q >= 0 && r + g < w && r + g >= 0 &&
                mapp[ud + q][r + g] == 1)
              vec[ud + q][r + g] = min(vec[ud + q][r + g], vec[ud][r] + 1);
          }
        }
      }
    }
    for (int lr = max(l, 0ll); lr <= min(r, w - 1); lr++) {
      if (u < h && u >= 0) {
        if (u + 1 < w && u + 1 >= 0 && mapp[u + 1][lr] == 1)
          vec[u + 1][lr] = min(vec[u + 1][lr], vec[u][lr]);
        if (u - 1 < h && u - 1 >= 0 && mapp[u - 1][l] == 1)
          vec[u - 1][lr] = min(vec[u - 1][lr], vec[u][lr]);
        if (lr + 1 < w && lr + 1 >= 0 && mapp[u][lr + 1] == 1)
          vec[u][lr + 1] = min(vec[u][lr + 1], vec[u][lr]);
        if (lr - 1 < w && lr - 1 >= 0 && mapp[u][lr - 1] == 1)
          vec[u][lr - 1] = min(vec[u][lr - 1], vec[u][lr]);
        for (int q = -2; q <= 2; ++q) {
          for (int g = -2; g <= 2; ++g) {
            if (u + q < h && u + q >= 0 && lr + g < w && lr + g >= 0 &&
                mapp[u + q][lr + g] == 1)
              vec[u + q][lr + g] = min(vec[u + q][lr + g], vec[u][lr] + 1);
          }
        }
      }
      if (d < h && d >= 0) {
        if (d + 1 < h && d + 1 >= 0 && mapp[d + 1][lr] == 1)
          vec[d + 1][lr] = min(vec[d + 1][lr], vec[d][lr]);
        if (d - 1 < h && d - 1 >= 0 && mapp[d - 1][lr] == 1)
          vec[d - 1][lr] = min(vec[d - 1][lr], vec[d][lr]);
        if (lr + 1 < w && lr + 1 >= 0 && mapp[d][lr + 1] == 1)
          vec[d][lr + 1] = min(vec[d][lr + 1], vec[d][lr]);
        if (lr - 1 < w && lr - 1 >= 0 && mapp[d][lr - 1] == 1)
          vec[d][lr - 1] = min(vec[d][lr - 1], vec[d][lr]);
        for (int q = -2; q <= 2; ++q) {
          for (int g = -2; g <= 2; ++g) {
            if (d + q < h && d + q >= 0 && lr + g < w && lr + g >= 0 &&
                mapp[d + q][lr + g] == 1)
              vec[d + q][lr + g] = min(vec[d + q][lr + g], vec[d][lr] + 1);
          }
        }
      }
    }
  }
  if (vec[dh][dw] >= inf) {
    cout << -1;
  } else {
    cout << vec[dh][dw];
  }
}