Language: C++

#include <bits/stdc++.h>
using namespace std;

const int dy[4] = { -1, 0, 1, 0 }, dx[4] = { 0, 1, 0, -1 };
const int INF = (int)1e9;

char a[1005][1005];
queue<pair<int, int> > Q;
int visit[1005][1005];

int main(void)
{
	int h, w, sy, sx, ey, ex, i, j, dir, magic;

	scanf("%d %d %d %d %d %d", &h, &w, &sy, &sx, &ey, &ex);
	for (i = 1; i <= h; i++)
		scanf("%s", a[i] + 1);

	for (i = 1; i <= h; i++)
		for (j = 1; j <= w; j++)
			visit[i][j] = INF;

	Q.push({ sy, sx });
	visit[sy][sx] = 0;

	while (!Q.empty())
	{
		int nowy = Q.front().first;
		int nowx = Q.front().second;
		Q.pop();

		for (dir = 0; dir < 4; dir++)
		{
			int nexty = nowy + dy[dir];
			int nextx = nowx + dx[dir];

			if (!(1 <= nexty && nexty <= h && 1 <= nextx && nextx <= w && a[nexty][nextx] == '.' && visit[nexty][nextx] > visit[nowy][nowx]))
				continue;

			Q.push({ nexty, nextx });
			visit[nexty][nextx] = visit[nowy][nowx];
		}

		for (i = -2; i <= 2; i++)
			for (j = -2; j <= 2; j++)
			{
				int nexty = nowy + i;
				int nextx = nowx + j;

				if (!(1 <= nexty && nexty <= h && 1 <= nextx && nextx <= w && a[nexty][nextx] == '.' && visit[nexty][nextx] > visit[nowy][nowx] + 1))
					continue;

				Q.push({ nexty, nextx });
				visit[nexty][nextx] = visit[nowy][nowx] + 1;
			}
	}

	printf("%d", visit[ey][ex] == INF ? -1 : visit[ey][ex]);
	return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:21:25: error: reference to 'visit' is ambiguous
   21 |                         visit[i][j] = INF;
      |                         ^~~~~
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:9:5: note:                 'int visit [1005][1005]'
    9 | int visit[1005][1005];
      |     ^~~~~
a.cc:24:9: error: reference to 'visit' is ambiguous
   24 |         visit[sy][sx] = 0;
      |         ^~~~~
/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:9:5: note:                 'int visit [1005][1005]'
    9 | int visit[1005][1005];
      |     ^~~~~
a.cc:37:113: error: reference to 'visit' is ambiguous
   37 |                         if (!(1 <= nexty && nexty <= h && 1 <= nextx && nextx <= w && a[nexty][nextx] == '.' && visit[nexty][nextx] > visit[nowy][nowx]))
      |                                                                                                                 ^~~~~
/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:9:5: note:                 'int visit [1005][1005]'
    9 | int visit[1005][1005];
      |     ^~~~~
a.cc:37:135: error: reference to 'visit' is ambiguous
   37 |                         if (!(1 <= nexty && nexty <= h && 1 <= nextx && nextx <= w && a[nexty][nextx] == '.' && visit[nexty][nextx] > visit[nowy][nowx]))
      |                                                                                                                                       ^~~~~
/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:9:5: note:                 'int visit [1005][1005]'
    9 | int visit[1005][1005];
      |     ^~~~~
a.cc:41:25: error: reference to 'visit' is ambiguous
   41 |                         visit[nexty][nextx] = visit[nowy][nowx];
      |                         ^~~~~
/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:9:5: note:                 'int visit [1005][1005]'
    9 | int visit[1005][1005];
      |     ^~~~~
a.cc:41:47: error: reference to 'visit' is ambiguous
   41 |                         visit[nexty][nextx] = visit[nowy][nowx];
      |                                               ^~~~~
/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:9:5: note:                 'int visit [1005][1005]'
    9 | int visit[1005][1005];
      |     ^~~~~
a.cc:50:121: error: reference to 'visit' is ambiguous
   50 |                                 if (!(1 <= nexty && nexty <= h && 1 <= nextx && nextx <= w && a[nexty][nextx] == '.' && visit[nexty][nextx] > visit[nowy][nowx] + 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:9:5: note:                 'int visit [1005][1005]'
    9 | int visit[1005][1005];
      |     ^~~~~
a.cc:50:143: error: reference to 'visit' is ambiguous
   50 |                                 if (!(1 <= nexty && nexty <= h && 1 <= nextx && nextx <= w && a[nexty][nextx] == '.' && visit[nexty][nextx] > visit[nowy][nowx] + 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:9:5: note:                 'int visit [1005][1005]'
    9 | int visit[1005][1005];
      |     ^~~~~
a.cc:54:33: error: reference to 'visit' is ambiguous
   54 |                                 visit[nexty][nextx] = visit[nowy][nowx] + 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:9:5: note:                 'int visit [1005][1005]'
    9 | int visit[1005][1005];
      |     ^~~~~
a.cc:54:55: error: reference to 'visit' is ambiguous
   54 |                                 visit[nexty][nextx] = visit[nowy][nowx] + 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:9:5: note:                 'int visit [1005][1005]'
    9 | int visit[1005][1005];
      |     ^~~~~
a.cc:58:22: error: reference to 'visit' is ambiguous
   58 |         printf("%d", visit[ey][ex] == INF ? -1 : visit[ey][ex]);
      |                      ^~~~~
/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:9:5: note:                 'int visit [1005][1005]'
    9 | int visit[1005][1005];
      |     ^~~~~
a.cc:58:50: error: reference to 'visit' is ambiguous
   58 |         printf("%d", visit[ey][ex] == INF ? -1 : visit[ey][ex]);
      |                                                  ^~~~~
/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:9:5: note:                 'int visit [1005][1005]'
    9 | int visit[1005][1005];
      |     ^~~~~