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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, int> pdi;
typedef pair<double, double> pdd;
typedef pair<double, ll> pdl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vii;
typedef vector<pll> vll;
typedef vector<vl> vvl;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<pdd> vdd;
typedef vector<pdi> vdi;
typedef vector<pdl> vdl;
typedef vector<string> vs;
#define fi first
#define se second
const ll INFLL = LLONG_MAX;
const int INF = INT_MAX;
const ll MAXLL = 0x3f3f3f3f3f3f3f3f;
const int MAX = 0x3f3f3f3f;
const ll MOD = 1000000007;
const ll mod = 998244353;
#define eb emplace_back
#define emp emplace
#define mp(a, b) make_pair(a, b)
template <class T> using min_heap = priority_queue<T, vector<T>, greater<T>>;
template <class T> void sort(vector<T> &v) { sort(v.begin(), v.end()); }
template <class T, class U> void sort(vector<T> &v, U func) {
  sort(v.begin(), v.end(), func);
}
template <class T> void rsort(vector<T> &v) { sort(v.rbegin(), v.rend()); }
template <class T> int lb_index(vector<T> &v, T k) {
  return lower_bound(v.begin(), v.end(), k) - v.begin();
}
template <class T> int ub_index(vector<T> &v, T k) {
  return upper_bound(v.begin(), v.end(), k) - v.begin();
}
template <class T> bool is_sorted(vector<T> &v) {
  return is_sorted(v.begin(), v.end());
}
template <class T> bool sorted(vector<T> &v) { return is_sorted(v); }
void precompute() { return; }
void solve() {
  int n, m;
  cin >> n >> m;
  vb vis(n * m);
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  a--;
  b--;
  c--;
  d--;
  vvb v(n, vb(m));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      char k;
      cin >> k;
      if (k == '.')
        v[i][j] = 1;
    }
  }
  //	for(int i=0;i<n;i++){
  //		for(int j=0;j<m;j++){
  //			cout<<v[i][j];
  //		}
  //		cout<<endl;
  //	}
  deque<pii> q;
  q.emplace_front(a * m + b, 0);
  while (!q.empty()) {
    int u = q.front().fi;
    int dist = q.front().se;
    //		cerr<<u<<' '<<dist<<endl;
    q.pop_front();
    if (u == c * m + d) {
      cout << dist;
      return;
    }
    if (vis[u])
      continue;
    vis[u] = 1;
    if (u / m + 1 < n && v[u / m + 1][u % m]) {
      if (!vis[u + m])
        q.emplace_front(u + m, dist);
    }
    if (u / m - 1 >= 0 && v[u / m - 1][u % m])
      if (!vis[u - m])
        q.emplace_front(u - m, dist);
    if (u % m + 1 < m && v[u / m][u % m + 1])
      if (!vis[u + 1])
        q.emplace_front(u + 1, dist);
    if (u % m - 1 >= 0 && v[u / m][u % m - 1])
      if (!vis[u - 1])
        q.emplace_front(u - 1, dist);
    for (int i = -2; i < 3; i++) {
      for (int j = -2; j < 3; j++) {
        if (u / m + i < 0 || u / m + i >= n)
          continue;
        if (u % m + j < 0 || u % m + j >= m)
          continue;
        if (i == 0 && abs(j) <= 1)
          continue;
        if (j == 0 && abs(i) <= 1)
          continue;
        if (v[u / m + i][u % m + j]) {
          if (!vis[u + i * m + j])
            q.emplace_back(u + i * m + j, dist + 1);
        }
      }
    }
  }
  cout << -1;
  return;
}

int main() {
  ios::sync_with_stdio(0);
  int numberofsubtestcases = 1;
  //	cin>>numberofsubtestcases;
  precompute();
  for (int looping = 1; looping <= numberofsubtestcases; looping++) {
    //		cout<<"Case #"<<looping<<": ";
    solve();
  }
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, int> pdi;
typedef pair<double, double> pdd;
typedef pair<double, ll> pdl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vii;
typedef vector<pll> vll;
typedef vector<vl> vvl;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<pdd> vdd;
typedef vector<pdi> vdi;
typedef vector<pdl> vdl;
typedef vector<string> vs;
#define fi first
#define se second
const ll INFLL = LLONG_MAX;
const int INF = INT_MAX;
const ll MAXLL = 0x3f3f3f3f3f3f3f3f;
const int MAX = 0x3f3f3f3f;
const ll MOD = 1000000007;
const ll mod = 998244353;
#define eb emplace_back
#define emp emplace
#define mp(a, b) make_pair(a, b)
template <class T> using min_heap = priority_queue<T, vector<T>, greater<T>>;
template <class T> void sort(vector<T> &v) { sort(v.begin(), v.end()); }
template <class T, class U> void sort(vector<T> &v, U func) {
  sort(v.begin(), v.end(), func);
}
template <class T> void rsort(vector<T> &v) { sort(v.rbegin(), v.rend()); }
template <class T> int lb_index(vector<T> &v, T k) {
  return lower_bound(v.begin(), v.end(), k) - v.begin();
}
template <class T> int ub_index(vector<T> &v, T k) {
  return upper_bound(v.begin(), v.end(), k) - v.begin();
}
template <class T> bool is_sorted(vector<T> &v) {
  return is_sorted(v.begin(), v.end());
}
template <class T> bool sorted(vector<T> &v) { return is_sorted(v); }
void precompute() { return; }
void solve() {
  int n, m;
  cin >> n >> m;
  vb vis(n * m);
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  a--;
  b--;
  c--;
  d--;
  vvb v(n, vb(m));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      char k;
      cin >> k;
      if (k == '.')
        v[i][j] = 1;
    }
  }
  deque<pii> q;
  q.emplace_front(a * n + b, 0);
  while (!q.empty()) {
    int u = q.front().fi;
    int dist = q.front().se;
    q.pop_front();
    if (u == c * n + d) {
      cout << dist;
      return;
    }
    if (vis[u])
      continue;
    vis[u] = 1;
    if (u / n + 1 < n && v[u / n + 1][u % n]) {
      if (!vis[u + n])
        q.emplace_front(u + n, dist);
    }
    if (u / n - 1 >= 0 && v[u / n - 1][u % n])
      if (!vis[u - n])
        q.emplace_front(u - n, dist);
    if (u % n + 1 < m && v[u / n][u % n + 1])
      if (!vis[u + 1])
        q.emplace_front(u + 1, dist);
    if (u % n - 1 >= 0 && v[u / n][u % n - 1])
      if (!vis[u - 1])
        q.emplace_front(u - 1, dist);
    for (int i = -2; i < 3; i++) {
      for (int j = -2; j < 3; j++) {
        if (u / n + i < 0 || u / n + i >= n)
          continue;
        if (u % n + j < 0 || u % n + j >= m)
          continue;
        if (i == 0 && abs(j) == 1)
          continue;
        if (j == 0 && abs(i) == 1)
          continue;
        if (v[u / n + i][u % n + j]) {
          if (!vis[u + i + i + j])
            q.emplace_back(u + i * 2 + j, dist + 1);
        }
      }
    }
  }
  cout << -1;
  return;
}

int main() {
  ios::sync_with_stdio(0);
  int numberofsubtestcases = 1;
  //	cin>>numberofsubtestcases;
  precompute();
  for (int looping = 1; looping <= numberofsubtestcases; looping++) {
    //		cout<<"Case #"<<looping<<": ";
    solve();
  }
  return 0;
}