what’s wrong with the code?
why is it not backtracking?
not able to get back to the old cell
`
public class Solution {
public static int min=Integer.MAX_VALUE;
public int knight(int A, int B, int C, int D, int E, int F) {
int dx[] = { -2, -1, 1, 2, -2, -1, 1, 2 };
int dy[] = { -1, -2, -2, -1, 1, 2, 2, 1 };
boolean[][] visited=new boolean[A+1][B+1];
int count=0;
dfs(A,B,C,D,E,F,dx,dy,visited,count);
return min!=Integer.MAX_VALUE?min:-1;
}
public static boolean safe(int C,int D,int A,int B){
if(!(C>=1 && C<=A)) return false;
if(!(D>=1 && D<=B)) return false;
// if(visited[C][D]) return false;
return true;
}
public static void dfs(int A, int B,int C, int D, int E, int F, int[] dx,int[] dy,
boolean[][] visited, int count){
// count++;
if(visited[C][D]) return;
visited[C][D]=true;
if(C==E && D==F ){ min=min<count?min:count; return;}
System.out.println("Currently at"+C+" "+D+" "+count+" "+min);
for(int i=0;i<8;i++){
if(safe(C+dx[i],D+dy[i],A,B)){
if(!visited[C+dx[i]][D+dy[i]])
dfs(A,B,C+dx[i],D+dy[i],E,F,dx,dy,visited,count+1);
}
}
}`