Simple and easy to understand :)


#1

int Solution::knight(int A, int B, int C, int D, int E, int F)
{
int m =A,n = B,i = C-1,j = D-1,x = E-1,y = F-1;
vector<vector> vis(m,vector (n,false));
vis[i][j] = true;
queue<pair<int,int>> q;
q.push({i,j});
int ans = 0;
while(!q.empty())
{
ans++;
int len = q.size();
while(len–)
{
pair<int,int> temp = q.front();
if(temp.first== x && temp.second == y)
return ans-1;
q.pop();
vector<pair<int,int>> dir= { {2,1},{2,-1},{-2,1},{-2,-1},{1,2},{1,-2},{-1,2},{-1,-2}};
for(int k = 0;k<8;k++)
{
int ni = temp.first + dir[k].first;
int nj = temp.second+dir[k].second;
if(ni>=0 && ni<m && nj>=0 && nj< n && !vis[ni][nj])
{
vis[ni][nj] = true;
q.push({ni,nj});
}
}
}
}
return -1;
}