Language: C++

#include<iostream>
#include<vector>
#include<bits/stdc++.h>
#define lli long long int
#define mod 1000000007
#define pb push_back
#define mk make_pair
#define fastio ios_base::sync_with_stdio(false); cout.tie(NULL);
using namespace std;

int main()
{
	fastio;
	bool reachable=false;
	int n,m,i,j,x,y,steps=0;
	pair<int,int> start,dest,src={-1,-1};
	
	cin>>n>>m;
	cin>>x>>y;
	start={x-1,y-1};
	cin>>x>>y;
	dest={x-1,y-1};
	char a[n][m];
	
	for(i=0;i<n;i++)
		for(j=0;j<m;j++)
			cin>>a[i][j];
	
	vector<vector<pair<int,int>>> v(n,vector<pair<int,int>> (m,{0,0}));
	map<pair<int,int>,int> mp;
	queue<pair<int,int>> q;
	q.push(start);
	v[start.first][start.second]={-1,-1};
	
	int vis[n][m];
	for(i=0;i<n;i++)
		for(j=0;j<m;j++)
			vis[i][j]=0;
	
	vis[start.first][start.second]=1;
	int unwn=2;
	while(!q.empty())
	{
		int f=0;
		pair<int,int> cur=q.front();
		q.pop();
//		cout<<cur.first<<" "<<cur.second<<endl;
		if(cur.first==dest.first && cur.second==dest.second)
		{
			reachable=true;
			break;
		}
		if(cur.first-1>=0 && (a[cur.first-1][cur.second]=='.') && vis[cur.first-1][cur.second]==0 )
		{
			f=1;
			vis[cur.first-1][cur.second]=1;
			q.push({cur.first-1,cur.second});
			v[cur.first-1][cur.second]=cur;
		}
		if(cur.first+1<n && (a[cur.first+1][cur.second]=='.') && vis[cur.first+1][cur.second]==0)
		{
			f=1;
			vis[cur.first+1][cur.second]=1;
			q.push({cur.first+1,cur.second});
			v[cur.first+1][cur.second]=cur;
		}
		if(cur.second+1<m && (a[cur.first][cur.second+1]=='.') && vis[cur.first][cur.second+1]==0)
		{
			f=1;
			vis[cur.first][cur.second+1]=1;
			q.push({cur.first,cur.second+1});
			v[cur.first][cur.second+1]=cur;
		}
		if(cur.second-1>=0 && (a[cur.first][cur.second-1]=='.') && vis[cur.first][cur.second-1]==0)
		{
			f=1;
			vis[cur.first][cur.second-1]=1;
			q.push({cur.first,cur.second-1});
			v[cur.first][cur.second-1]=cur;
		}
		if(f==0)
			unwn--;
		if(f==0)
		{
			if(n*m>12345 && unwn>0)
				continue;
			unwn=2;
			if(a[cur.first][cur.second]!='#' && cur.second+2<m && vis[cur.first][cur.second+2]==0 && a[cur.first][cur.second+2]!='#')
			{
				vis[cur.first][cur.second+2]=1;
				q.push({cur.first,cur.second+2});
				v[cur.first][cur.second+2]=cur;
//				cout<<cur.first<<" .... "<<cur.second+2<<endl;
			}
			if(a[cur.first][cur.second]!='#' && cur.second-2>=0 && vis[cur.first][cur.second-2]==0 && a[cur.first][cur.second-2]!='#')
			{
				vis[cur.first][cur.second-2]=1;
				q.push({cur.first,cur.second-2});
				v[cur.first][cur.second-2]=cur;
//				cout<<cur.first<<" .... "<<cur.second-2<<endl;
			}
			if(a[cur.first][cur.second]!='#' && cur.first+2<n && vis[cur.first+2][cur.second]==0 && a[cur.first+2][cur.second]!='#')
			{
				vis[cur.first+2][cur.second]=1;
				q.push({cur.first+2,cur.second});
				v[cur.first+2][cur.second]=cur;
//				cout<<cur.first+2<<" .... "<<cur.second<<endl;
			}
			if(a[cur.first][cur.second]!='#' && cur.first-2>=0 && vis[cur.first-2][cur.second]==0 && a[cur.first-2][cur.second]!='#')
			{
				vis[cur.first-2][cur.second]=1;
				q.push({cur.first-2,cur.second});
				v[cur.first-2][cur.second]=cur;
//				cout<<cur.first-2<<" .... "<<cur.second<<endl;
			}
			if(a[cur.first][cur.second]!='#' && cur.first+2<n && cur.second+2<m &&  vis[cur.first+2][cur.second+2]==0 && a[cur.first+2][cur.second+2]!='#')
			{
				vis[cur.first+2][cur.second+2]=1;
				q.push({cur.first+2,cur.second+2});
				v[cur.first+2][cur.second+2]=cur;
//				cout<<cur.first+2<<" .... "<<cur.second+2<<endl;
			}
			if(a[cur.first][cur.second]!='#' && cur.first-2>=0 && cur.second-2>=0 &&  vis[cur.first-2][cur.second-2]==0 && a[cur.first-2][cur.second-2]!='#')
			{
				vis[cur.first-2][cur.second-2]=1;
				q.push({cur.first-2,cur.second-2});
				v[cur.first-2][cur.second-2]=cur;
//				cout<<cur.first-2<<" .... "<<cur.second-2<<endl;
			}
			if(a[cur.first][cur.second]!='#' && cur.first-2>=0 && cur.second+2<m &&  vis[cur.first-2][cur.second+2]==0 && a[cur.first-2][cur.second+2]!='#')
			{
				vis[cur.first-2][cur.second+2]=1;
				q.push({cur.first-2,cur.second+2});
				v[cur.first-2][cur.second+2]=cur;
//				cout<<cur.first-2<<" .... "<<cur.second+2<<endl;
			}
			if(a[cur.first][cur.second]!='#' && cur.first+2<n && cur.second-2>=0 &&  vis[cur.first+2][cur.second-2]==0 && a[cur.first+2][cur.second-2]!='#')
			{
				vis[cur.first+2][cur.second-2]=1;
				q.push({cur.first+2,cur.second-2});
				v[cur.first+2][cur.second-2]=cur;
//				cout<<cur.first+2<<" .... "<<cur.second-2<<endl;
			}
			
			if(a[cur.first][cur.second]!='#' && cur.first+1<n && cur.second+1<m &&  vis[cur.first+1][cur.second+1]==0 && a[cur.first+1][cur.second+1]!='#')
			{
				vis[cur.first+1][cur.second+1]=1;
				q.push({cur.first+1,cur.second+1});
				v[cur.first+1][cur.second+1]=cur;
//				cout<<cur.first+1<<" .... "<<cur.second+1<<endl;
			}
			if(a[cur.first][cur.second]!='#' && cur.first-1>=0 && cur.second-1>=0 &&  vis[cur.first-1][cur.second-1]==0 && a[cur.first-1][cur.second-1]!='#')
			{
				vis[cur.first-1][cur.second-1]=1;
				q.push({cur.first-1,cur.second-1});
				v[cur.first-1][cur.second-1]=cur;
//				cout<<cur.first-1<<" .... "<<cur.second-1<<endl;
			}
			if(a[cur.first][cur.second]!='#' && cur.first-1>=0 && cur.second+1<m &&  vis[cur.first-1][cur.second+1]==0 && a[cur.first-1][cur.second+1]!='#')
			{
				vis[cur.first-1][cur.second+1]=1;
				q.push({cur.first-1,cur.second+1});
				v[cur.first-1][cur.second+1]=cur;
//				cout<<cur.first-1<<" .... "<<cur.second+1<<endl;
			}
			if(a[cur.first][cur.second]!='#' && cur.first+1<n && cur.second-1>=0 &&  vis[cur.first+1][cur.second-1]==0 && a[cur.first+1][cur.second-1]!='#')
			{
				vis[cur.first+1][cur.second-1]=1;
				q.push({cur.first+1,cur.second-1});
				v[cur.first+1][cur.second-1]=cur;
//				cout<<cur.first+1<<" .... "<<cur.second-1<<endl;
			}
			
			if(a[cur.first][cur.second]!='#' && cur.first+1<n && cur.second+2<m &&  vis[cur.first+1][cur.second+2]==0 && a[cur.first+1][cur.second+2]!='#')
			{
				vis[cur.first+1][cur.second+2]=1;
				q.push({cur.first+1,cur.second+2});
				v[cur.first+1][cur.second+2]=cur;
//				cout<<cur.first+1<<" .... "<<cur.second+2<<endl;
			}
			if(a[cur.first][cur.second]!='#' && cur.first-1>=0 && cur.second-2>=0 &&  vis[cur.first-1][cur.second-2]==0 && a[cur.first-1][cur.second-2]!='#')
			{
				vis[cur.first-1][cur.second-2]=1;
				q.push({cur.first-1,cur.second-2});
				v[cur.first-1][cur.second-2]=cur;
//				cout<<cur.first-1<<" .... "<<cur.second-2<<endl;
			}
			if(a[cur.first][cur.second]!='#' && cur.first-1>=0 && cur.second+2<m &&  vis[cur.first-1][cur.second+2]==0 && a[cur.first-1][cur.second+2]!='#')
			{
				vis[cur.first-1][cur.second+2]=1;
				q.push({cur.first-1,cur.second+2});
				v[cur.first-1][cur.second+2]=cur;
//				cout<<cur.first-1<<" .... "<<cur.second+2<<endl;
			}
			if(a[cur.first][cur.second]!='#' && cur.first+1<n && cur.second-2>=0 &&  vis[cur.first+1][cur.second-2]==0 && a[cur.first+2][cur.second-2]!='#')
			{
				vis[cur.first+1][cur.second-2]=1;
				q.push({cur.first+1,cur.second-2});
				v[cur.first+1][cur.second-2]=cur;
//				cout<<cur.first+1<<" .... "<<cur.second-2<<endl;
			}
			
			if(a[cur.first][cur.second]!='#' && cur.first+2<n && cur.second+1<m &&  vis[cur.first+2][cur.second+1]==0 && a[cur.first+2][cur.second+1]!='#')
			{
				vis[cur.first+2][cur.second+1]=1;
				q.push({cur.first+2,cur.second+1});
				v[cur.first+2][cur.second+1]=cur;
//				cout<<cur.first+2<<" .... "<<cur.second+1<<endl;
			}
			if(a[cur.first][cur.second]!='#' && cur.first-2>=0 && cur.second-1>=0 &&  vis[cur.first-2][cur.second-1]==0 && a[cur.first-2][cur.second-1]!='#')
			{
				vis[cur.first-2][cur.second-1]=1;
				q.push({cur.first-2,cur.second-1});
				v[cur.first-2][cur.second-1]=cur;
//				cout<<cur.first-2<<" .... "<<cur.second-1<<endl;
			}
			if(a[cur.first][cur.second]!='#' && cur.first-2>=0 && cur.second+1<m &&  vis[cur.first-2][cur.second+1]==0 && a[cur.first-2][cur.second+1]!='#')
			{
				vis[cur.first-2][cur.second+1]=1;
				q.push({cur.first-2,cur.second+1});
				v[cur.first-2][cur.second+1]=cur;
//				cout<<cur.first-2<<" .... "<<cur.second+1<<endl;
			}
			if(a[cur.first][cur.second]!='#' && cur.first+2<n && cur.second-1>=0 &&  vis[cur.first+2][cur.second-1]==0 && a[cur.first+2][cur.second-1]!='#')
			{
				vis[cur.first+2][cur.second-1]=1;
				q.push({cur.first+2,cur.second-1});
				v[cur.first+2][cur.second-1]=cur;
//				cout<<cur.first+2<<" .... "<<cur.second-1<<endl;
			}
		}
	}
	if(reachable)
	{
		vector<pair<int,int>> path;
		i=dest.first,j=dest.second;
		pair<int,int> last=dest;
		while(v[i][j].first!=-1 && v[i][j].second!=-1)
		{
			path.pb({i,j});
			int temp=i;
			i=v[i][j].first;
			j=v[temp][j].second;
			if((last.first==i && last.second-j==1) || (last.first==i && last.second-j==-1) || (last.first-i==1 && last.second==j) || (last.first-i==-1 && last.second==j))
				int do_nothing;
			else
				steps+=1;
			last={i,j};
		}
		cout<<steps<<"\n"<<ans;
	}
	else
		cout<<"-1";
	return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:250:36: error: 'ans' was not declared in this scope; did you mean 'abs'?
  250 |                 cout<<steps<<"\n"<<ans;
      |                                    ^~~
      |                                    abs