Language: C++

#include "bits/stdc++.h"
#define ll long long int
#define ld long double
#define fastIO ios_base::sync_with_stdio(false); cin.tie(nullptr)
#define F first
#define S second
#define INF 1000000

using namespace std;
typedef pair<int, int> pii;

int r, c;
vector<vector<bool>> g;
vector<vector<bool>> vis;
bool isSafe(int row, int col) {
    return row < r && row >= 0 && col < c && col >= 0 && g[row][col];
}

int main() {
    fastIO;
    int startX, startY, endX, endY;
    cin >> r >> c >> startX >> startY >> endX >> endY;
    startX--, startY--, endX--, endY--;

    g = vis = vector<vector<bool>>(r, vector<bool>(c, false));
    int dist[r][c];

    for (int i = 0; i < r; i++) {
        for (int j = 0; j < c; j++) {
            dist[i][j] = INF;
            char temp;
            cin >> temp;
            if (temp == '.') g[i][j] = true;
        }
    }

    priority_queue<pair<int, pii>, vector<pair<int, pii>>, less<int>> q;
    q.push({0, {startX, startY}});

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

    dist[startX][startY] = 0;

    while (!q.empty()) {
        pair<int, pii> p = q.top();
        q.pop();
        int x = p.S.F, y = p.S.S;

        for (int i = 0; i < 4; i++) {
            int nx = x + dx[i], ny = y + dy[i];
            if (isSafe(nx, ny) && dist[x][y] < dist[nx][ny]) {
                dist[nx][ny] = dist[x][y];
                q.push({0, {nx, ny}});
            }
        }

        for (int i = x - 2; i <= x + 2; i++) {
            for (int j = y - 2; j <= y + 2; j++) {
                if (i == x && j == y) continue;
                if (isSafe(i, j) && dist[x][y] + 1 < dist[i][j]) {
                    dist[i][j] = dist[x][y] + 1;
                    q.push({1, {i, j}});
                }
            }
        }
    }


    if (dist[endX][endY] >= 100000) cout << -1;
    else cout << dist[endX][endY];

}


Compiler output:
In file included from /usr/include/c++/14/bits/stl_algobase.h:71,
                 from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/predefined_ops.h: In instantiation of 'bool __gnu_cxx::__ops::_Iter_comp_val<_Compare>::operator()(_Iterator, _Value&) [with _Iterator = __gnu_cxx::__normal_iterator<std::pair<int, std::pair<int, int> >*, std::vector<std::pair<int, std::pair<int, int> > > >; _Value = std::pair<int, std::pair<int, int> >; _Compare = std::less<int>]':
/usr/include/c++/14/bits/stl_heap.h:140:48:   required from 'void std::__push_heap(_RandomAccessIterator, _Distance, _Distance, _Tp, _Compare&) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<pair<int, pair<int, int> >*, vector<pair<int, pair<int, int> > > >; _Distance = long int; _Tp = pair<int, pair<int, int> >; _Compare = __gnu_cxx::__ops::_Iter_comp_val<less<int> >]'
  140 |       while (__holeIndex > __topIndex && __comp(__first + __parent, __value))
      |                                          ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_heap.h:216:23:   required from 'void std::push_heap(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<pair<int, pair<int, int> >*, vector<pair<int, pair<int, int> > > >; _Compare = less<int>]'
  216 |       std::__push_heap(__first, _DistanceType((__last - __first) - 1),
      |       ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  217 |                        _DistanceType(0), _GLIBCXX_MOVE(__value), __cmp);
      |                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_queue.h:747:16:   required from 'void std::priority_queue<_Tp, _Sequence, _Compare>::push(value_type&&) [with _Tp = std::pair<int, std::pair<int, int> >; _Sequence = std::vector<std::pair<int, std::pair<int, int> > >; _Compare = std::less<int>; value_type = std::pair<int, std::pair<int, int> >]'
  747 |         std::push_heap(c.begin(), c.end(), comp);
      |         ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:38:11:   required from here
   38 |     q.push({0, {startX, startY}});
      |     ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/predefined_ops.h:196:30: error: no match for call to '(std::less<int>) (std::pair<int, std::pair<int, int> >&, std::pair<int, std::pair<int, int> >&)'
  196 |         { return bool(_M_comp(*__it, __val)); }
      |                       ~~~~~~~^~~~~~~~~~~~~~
In file included from /usr/include/c++/14/string:49,
                 from /usr/include/c++/14/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:52:
/usr/include/c++/14/bits/stl_function.h:404:7: note: candidate: 'constexpr bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = int]'
  404 |       operator()(const _Tp& __x, const _Tp& __y) const
      |       ^~~~~~~~
/usr/include/c++/14/bits/stl_function.h:404:29: note:   no known conversion for argument 1 from 'std::pair<int, std::pair<int, int> >' to 'const int&'
  404 |       operator()(const _Tp& __x, const _Tp& __y) const
      |                  ~~~~~~~~~~~^~~
/usr/include/c++/14/bits/predefined_ops.h: In instantiation of 'constexpr bool __gnu_cxx::__ops::_Iter_comp_iter<_Compare>::operator()(_Iterator1, _Iterator2) [with _Iterator1 = __gnu_cxx::__normal_iterator<std::pair<int, std::pair<int, int> >*, std::vector<std::pair<int, std::pair<int, int> > > >; _Iterator2 = __gnu_cxx::__normal_iterator<std::pair<int, std::pair<int, int> >*, std::vector<std::pair<int, std::pair<int, int> > > >; _Compare = std::less<int>]':
/usr/include/c++/14/bits/stl_heap.h:232:14:   required from 'void std::__adjust_heap(_RandomAccessIterator, _Distance, _Distance, _Tp, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<pair<int, pair<int, int> >*, vector<pair<int, pair<int, int> > > >; _Distance = long int; _Tp = pair<int, pair<int, int> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<less<int> >]'
  232 |           if (__comp(__first + __secondChild,
      |               ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
  233 |                      __first + (__secondChild - 1)))
      |                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_heap.h:264:25:   required from 'void std::__pop_heap(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, _Compare&) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<pair<int, pair<int, int> >*, vector<pair<int, pair<int, int> > > >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<less<int> >]'
  264 |       std::__adjust_heap(__first, _DistanceType(0),
      |       ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
  265 |                          _DistanceType(__last - __first),
      |                          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  266 |                          _GLIBCXX_MOVE(__value), __comp);
      |                          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_heap.h:333:19:   required from 'void std::pop_heap(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<pair<int, pair<int, int> >*, vector<pair<int, pair<int, int> > > >; _Compare = less<int>]'
  333 |           std::__pop_heap(__first, __last, __last, __cmp);
      |           ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_queue.h:774:15:   required from 'void std::priority_queue<_Tp, _Sequence, _Compare>::pop() [with _Tp = std::pair<int, std::pair<int, int> >; _Sequence = std::vector<std::pair<int, std::pair<int, int> > >; _Compare = std::less<int>]'
  774 |         std::pop_heap(c.begin(), c.end(), comp);
      |         ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:47:14:   required from here
   47 |         q.pop();
      |         ~~~~~^~
/usr/include/c++/14/bits/predefined_ops.h:158:30: error: no match for call to '(std::less<int>) (std::pair<int, std::pair<int, int> >&, std::pair<int, std::pair<int, int> >&)'
  158 |         { return bool(_M_comp(*__it1, *__it2)); }
      |                       ~~~~~~~^~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_function.h:404:7: note: candidate: 'constexpr bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = int]'
  404 |       operator()(const _Tp& __x, const _Tp& __y) const
      |       ^~~~~~~~
/usr/include/c++/14/bits/stl_function.h:404:29: note:   no known conversion for argument 1 from 'std::pair<int, std::pair<int, int> >' to 'const int&'
  404 |       operator()(const _Tp& __x, const _Tp& __y) const
      |                  ~~~~~~~~~~~^~~