Problem with test cases and submission


#1

For input
7
13
2
8
3
4
Even though function return 3 while submitting it gives wrong answer

My code is …

int dx[] = {-2,-1,1,2,2,1,-1,-2};
int dy[] = {1,2,2,1,-1,-2,-2,-1};
int row;
int col;
int vis[501][501];
int dis[501][501];

bool isValid(int x ,int y){
if(x>row || x<1 || y<1 || y > col) return false;
if(vis[x][y] ) return false;
return true;
}
void bfs(int srcX, int srcY, int endX, int endY){
vis[srcX][srcY]=1;
dis[srcX][srcY]=0;
queue<pair<int,int>> q;
q.push({srcX, srcY});
while(!q.empty()){
int x = q.front().first;
int y= q.front().second;
q.pop();
for(int i=0;i<8;i++){
if(isValid(x+dx[i],y+dy[i])){
vis[x+dx[i]][y+dy[i]]=1;
dis[x+dx[i]][y+dy[i]]=1+ dis[x][y];
// if(x+dx[i]==endX && y+dy[i]==endY) return ;
q.push({x+dx[i],y+dy[i]});
}
}
}
}
int Solution::knight(int A, int B, int C, int D, int E, int F) {
row=A;
col=B;
dis[E][F] = -1;
bfs(C,D,E,F);
// for(int i=1;i<=A;i++){
// for(int j=1;j<=B;j++){
// cout<<dis[i][j]<<" ";
// }
// cout<<endl;
// }
return dis[E][F];
}