Language: C++

#include <bits/stdc++.h>

using namespace std;

struct P {
  int x;
  int y;
};

const int dx[4] = {-1, 0, +1, 0};
const int dy[4] = {0, -1, 0, +1};
int H, W, sy, sx, gy, gx;
vector<string> S;
vector<vector<int>> visit;

void dfs(int x, int y, int level) {
  queue<P> same, jumps;
  same.push({x, y});

  while (!same.empty()) {
    P p = same.front();
    same.pop();

    if (visit[p.y][p.x] != -1 && visit[p.y][p.x] <= level) continue;

    visit[p.y][p.x] = level;
    for (int d = 0; d < 4; d++) {
      int x2 = p.x + dx[d];
      int y2 = p.y + dy[d];
      if (x2 < 0 || W <= x2 || y2 < 0 || H <= y2) continue;
      if (S[y2][x2] == '#') continue;
      same.push({x2, y2});
    }

    for (int x2 = p.x - 2; x2 <= p.x + 2; x2++) {
      for (int y2 = p.y - 2; y2 <= p.y + 2; y2++) {
        if (x2 < 0 || W <= x2 || y2 < 0 || H <= y2) continue;
        if (S[y2][x2] == '#') continue;
        jumps.push({x2, y2});
      }
    }
  }

  while (!jumps.empty()) {
    P p = jumps.front();
    jumps.pop();

    if (visit[p.y][p.x] != -1 && visit[p.y][p.x] <= level) continue;
    dfs(p.x, p.y, level + 1);
  }
}

int main() {
  cin >> H >> W >> sy >> sx >> gy >> gx;
  sy--;
  sx--;
  gy--;
  gx--;
  S.resize(H);
  for (int y = 0; y < H; y++) cin >> S[y];
  visit = vector<vector<int>>(H, vector<int>(W, -1));
  dfs(sx, sy, 0);

  cout << visit[gy][gx] << endl;

  return 0;
}


Compiler output:
a.cc: In function 'void dfs(int, int, int)':
a.cc:24:9: error: reference to 'visit' is ambiguous
   24 |     if (visit[p.y][p.x] != -1 && visit[p.y][p.x] <= level) continue;
      |         ^~~~~
In file included from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:80,
                 from a.cc:1:
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:14:21: note:                 'std::vector<std::vector<int> > visit'
   14 | vector<vector<int>> visit;
      |                     ^~~~~
a.cc:24:34: error: reference to 'visit' is ambiguous
   24 |     if (visit[p.y][p.x] != -1 && visit[p.y][p.x] <= level) continue;
      |                                  ^~~~~
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:14:21: note:                 'std::vector<std::vector<int> > visit'
   14 | vector<vector<int>> visit;
      |                     ^~~~~
a.cc:26:5: error: reference to 'visit' is ambiguous
   26 |     visit[p.y][p.x] = level;
      |     ^~~~~
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:14:21: note:                 'std::vector<std::vector<int> > visit'
   14 | vector<vector<int>> visit;
      |                     ^~~~~
a.cc:48:9: error: reference to 'visit' is ambiguous
   48 |     if (visit[p.y][p.x] != -1 && visit[p.y][p.x] <= level) continue;
      |         ^~~~~
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:14:21: note:                 'std::vector<std::vector<int> > visit'
   14 | vector<vector<int>> visit;
      |                     ^~~~~
a.cc:48:34: error: reference to 'visit' is ambiguous
   48 |     if (visit[p.y][p.x] != -1 && visit[p.y][p.x] <= level) continue;
      |                                  ^~~~~
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:14:21: note:                 'std::vector<std::vector<int> > visit'
   14 | vector<vector<int>> visit;
      |                     ^~~~~
a.cc: In function 'int main()':
a.cc:61:3: error: reference to 'visit' is ambiguous
   61 |   visit = vector<vector<int>>(H, vector<int>(W, -1));
      |   ^~~~~
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:14:21: note:                 'std::vector<std::vector<int> > visit'
   14 | vector<vector<int>> visit;
      |                     ^~~~~
a.cc:64:11: error: reference to 'visit' is ambiguous
   64 |   cout << visit[gy][gx] << endl;
      |           ^~~~~
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:14:21: note:                 'std::vector<std::vector<int> > visit'
   14 | vector<vector<int>> visit;
      |                     ^~~~~