Language: C++

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

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

bool mp[1005][1005];
bool visit[1005][1005];
pair<int, int> path[1005][1005];

int h, w;
int ch, cw, dh, dw;

queue<pair<int, int> > qu;

void reset(void)
{
	for (int i = 1; i <= h; i++)
		for (int j = 1; j <= w; j++)
		{
			visit[i][j] = false;
			path[i][j] = make_pair(0, 0);
		}

	while (!qu.empty())
		qu.pop();
}

bool push(int y, int x, int my_y, int my_x, bool magic)
{
	if (visit[y][x] || mp[y][x] == false)
		return false;
	if (y < 1 || y > h || x < 1 || x > w)
		return false;

	qu.push(make_pair(y, x));
	visit[y][x] = true;
	path[y][x] = make_pair(my_y, my_x);

	return true;
}

int get_a(int y, int x)
{
	if (y == 0 && x == 0)
		return 0;

	if (abs(path[y][x].first - y) > 1 || abs(path[y][x].second - x) > 1)
		return get_a(path[y][x].first, path[y][x].second) + 1;

	return get_a(path[y][x].first, path[y][x].second);
}

bool no_magic(void)
{
	push(ch, cw, 0, 0, false);

	while (!qu.empty())
	{
		int my_y = qu.front().first;
		int my_x = qu.front().second;

		qu.pop();

		if (my_y == dh && my_x == dw)
		{
			printf("0");
			exit(0);
		}

		for (int i = 0; i < 4; i++)
			push(my_y + dy[i], my_x + dx[i], my_y, my_x, false);
	}

	return false;
}

void bfs(void)
{
	push(ch, cw, 0, 0, false);

	while (!qu.empty())
	{
		bool flag = false;
		int my_y = qu.front().first;
		int my_x = qu.front().second;

		qu.pop();

		if (my_y == dh && my_x == dw)
		{
			printf("%d", get_a(my_y, my_x));
			return;
		}

		for (int i = 0; i < 4; i++)
		{
			if (flag == false)
				flag = push(my_y + dy[i], my_x + dx[i], my_y, my_x, false);
		}

		if (flag == false)
		{
			push(my_y - 2, my_x - 2, my_y, my_x, true);
			push(my_y - 2, my_x - 1, my_y, my_x, true);
			push(my_y - 2, my_x, my_y, my_x, true);
			push(my_y - 2, my_x + 1, my_y, my_x, true);
			push(my_y - 2, my_x + 2, my_y, my_x, true);

			push(my_y - 1, my_x - 2, my_y, my_x, true);
			push(my_y - 1, my_x - 1, my_y, my_x, true);
			push(my_y - 1, my_x + 1, my_y, my_x, true);
			push(my_y - 1, my_x + 2, my_y, my_x, true);

			push(my_y, my_x - 2, my_y, my_x, true);
			push(my_y, my_x + 2, my_y, my_x, true);

			push(my_y + 1, my_x - 2, my_y, my_x, true);
			push(my_y + 1, my_x - 1, my_y, my_x, true);
			push(my_y + 1, my_x + 1, my_y, my_x, true);
			push(my_y + 1, my_x + 2, my_y, my_x, true);

			push(my_y + 2, my_x - 2, my_y, my_x, true);
			push(my_y + 2, my_x - 1, my_y, my_x, true);
			push(my_y + 2, my_x, my_y, my_x, true);
			push(my_y + 2, my_x + 1, my_y, my_x, true);
			push(my_y + 2, my_x + 2, my_y, my_x, true);
		}
	}

	printf("-1");
	return;
}

int main(void)
{
	scanf("%d %d %d %d %d %d", &h, &w, &ch, &cw, &dh, &dw);
	for (int i = 1; i <= h; i++)
	{
		string input;
		cin >> input;
		for (int j = 0; j < w; j++)
		{
			if (input[j] == '.')
				mp[i][j + 1] = true;
			else mp[i][j + 1] = false;
		}
	}

	if (!no_magic())
	{
		reset();
		bfs();
	}

	return 0;
}

Compiler output:
a.cc: In function 'void reset()':
a.cc:21:25: error: reference to 'visit' is ambiguous
   21 |                         visit[i][j] = false;
      |                         ^~~~~
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:8:6: note:                 'bool visit [1005][1005]'
    8 | bool visit[1005][1005];
      |      ^~~~~
a.cc: In function 'bool push(int, int, int, int, bool)':
a.cc:31:13: error: reference to 'visit' is ambiguous
   31 |         if (visit[y][x] || mp[y][x] == false)
      |             ^~~~~
/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:8:6: note:                 'bool visit [1005][1005]'
    8 | bool visit[1005][1005];
      |      ^~~~~
a.cc:37:9: error: reference to 'visit' is ambiguous
   37 |         visit[y][x] = true;
      |         ^~~~~
/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:8:6: note:                 'bool visit [1005][1005]'
    8 | bool visit[1005][1005];
      |      ^~~~~