Language: C++

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using vl = vector<ll>;
using vvl = vector<vl>;
using vb = vector<bool>;
using P = pair<ll, ll>;
template <typename T> using pq = priority_queue<T>;
template <typename T> using minpq = priority_queue<T, vector<T>, greater<T>>;
template <typename T, typename K> using ump = unordered_map<T, K>;
const ll dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
const ll mod = 1000000007;
const ll inf = ll(1e9);
const ll e5 = ll(1e5);
const ll ll_inf = ll(1e9) * ll(1e9);

#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repr(i, n) for (ll i = ll(n - 1); i >= 0; i--)
#define each(i, mp) for (auto &i : mp)
#define eb emplace_back
#define F firstdd
#define S second
#define all(obj) (obj).begin(), (obj).end()

template <class T> ostream &operator<<(ostream &out, const vector<T> &list) {
    ll n = list.size();
    rep(i, n) out << list[i] << ' ';
    return out;
}

template <class T> istream &operator>>(istream &in, vector<T> &list) {
    ll n = list.size();
    rep(i, n) in >> list[i];
    return in;
}

template <class T> ostream &operator<<(ostream &out, const vector<vector<T>> &list) {
    ll n = list.size();
    rep(i, n) out << list[i] << '\n';
    return out;
}

/* ------------- ANSWER ------------- */
/* ---------------------------------- */

void solve() {
    ll h, w;
    cin >> h >> w;
    ll startx, starty, endx, endy;
    cin >> startx >> starty >> endx >> endy;
    startx--, starty--, endx--, endy--;

    vector<vector<char>> s(h, vector<char>(w));
    rep(i, h) rep(j, w) cin >> s[i][j];

    vector<vector<ll>> cnts(h, vector<ll>(w, ll_inf));  // 魔法使った回数
    queue<P> q;
    q.push(P(startx, starty));
    cnts[startx][starty] = 0;
    queue<P> nq;
    while (!q.empty()) {
        //        if (q.empty() && nq.empty()) {
        //            break;
        //        }
        ll nowx, nowy;
        //        if (!q.empty()) {
        nowx = q.front().first;
        nowy = q.front().second;
        q.pop();
        vector<P> go;
        rep(i, 4) {
            ll nx = nowx + dx[i], ny = nowy + dy[i];
            if (0 <= nx && nx <= h - 1 && 0 <= ny && ny <= w - 1) {
                if (s[nx][ny] == '#') continue;
                if (cnts[nowx][nowy] < cnts[nx][ny]) {
                    cnts[nx][ny] = cnts[nowx][nowy];
                    q.push(P(nx, ny));
                    go.eb(P(nx, ny));
                }
            }


        for (ll i = -2; i <= 2; ++i) {
            for (ll j = -2; j <= 2; ++j) {
                ll nx = nowx + i, ny = nowy + j;
                if (0 <= nx && nx <= h - 1 && 0 <= ny && ny <= w - 1) {
                    if (s[nx][ny] == '#') continue;
                    bool skip = false;
                    each(e, go) {
                        if (e.first == nx && e.second == ny) {
                            skip = true;
                        }
                    }
                    if (skip) continue;

                    if (cnts[nowx][nowy] + 1 < cnts[nx][ny]) {
                        cnts[nx][ny] = cnts[nowx][nowy] + 1;
                        q.push(P(nx, ny));
                    }
                }
            }
        }
        //            if (!go) {
        //                nq.push(P(nowx, nowy));
        //            }
        //        } else {
        //            nowx = nq.front().first;
        //            nowy = nq.front().second;
        //            nq.pop();
        //            for (ll i = -2; i <= 2; ++i) {
        //                for (ll j = -2; j <= 2; ++j) {
        //                    ll nx = nowx + i, ny = nowy + j;
        //                    if (0 <= nx && nx <= h - 1 && 0 <= ny && ny <= w - 1) {
        //                        if (s[nx][ny] == '#') continue;
        //
        //                        if (cnts[nowx][nowy] + 1 < cnts[nx][ny]) {
        //                            cnts[nx][ny] = cnts[nowx][nowy] + 1;
        //                            q.push(P(nx, ny));
        //                        }
        //                    }
        //                }
        //            }
        //        }
    }
    //    cout << cnts << '\n';
    //    cout << d << '\n';
    if (cnts[endx][endy] == ll_inf)
        cout << -1 << '\n';
    else
        cout << cnts[endx][endy] << '\n';
}

int main() {
#ifdef MY_DEBUG
    while (true) {
#endif
        solve();
#ifdef MY_DEBUG
    }
#endif
    return 0;
}


Compiler output:
a.cc: In function 'void solve()':
a.cc:135:9: warning: empty parentheses were disambiguated as a function declaration [-Wvexing-parse]
  135 | int main() {
      |         ^~
a.cc:135:9: note: remove parentheses to default-initialize a variable
  135 | int main() {
      |         ^~
      |         --
a.cc:135:9: note: or replace parentheses with braces to value-initialize a variable
a.cc:135:12: error: a function-definition is not allowed here before '{' token
  135 | int main() {
      |            ^
a.cc:144:2: error: expected '}' at end of input
  144 | }
      |  ^
a.cc:48:14: note: to match this '{'
   48 | void solve() {
      |              ^