Move the knight from position 1 to position 2 with less than 10 moves

I have a question.
here is my chess board , ok?
enter image description here

I wanna move knight from postion 1 to postion 2 with all possible ways.
but with less than 10 moves.
can any one help me have to do this?

function isSafe(x,y,board){
if(x >= 0 && y >= 0 && x < 8 && y < 8 && board[y][x] == 0){return true}
else{return false}
}


function printBoard(board){
for(let i = 0; i < 8; i++){
    for(let x = 0; x < 8; x++){
        console.log(board[i][x] + "  ");
    }
}
}


function solve(){
let board = [];
for(let i = 0; i < 8; i++){
    board[i] = [];
    for(let x = 0; x < 8; x++){
        board[i][x] = 0;
    }
}
var move_x = [2, 1, -1, -2, -2, -1, 1, 2];
var move_y = [1, 2, 2, 1, -1, -2, -2, -1];

board[0][0] = 1;

var pos = 1;

 if(solveKTUtil(board,0,0,move_x,move_y,pos)){
    printBoard(board);
  }
   else{console.log('no answer')}
    console.log(board)
 }


function solveKTUtil(board,curr_x,curr_y,move_x,move_y,pos){
if(curr_x == 7 && curr_y == 7){return true}
for(let i = 0; i < 8; i++){
    var new_y = curr_y + move_y[i] ;
    var new_x = curr_x + move_x[i] ;
    
    if(isSafe(new_x,new_y,board)){
        console.log(new_x,new_y)
        board[new_y][new_x] = pos;
        if(board[7][7] === 1){return true}
        else{
           solveKTUtil(board,curr_x,curr_y,move_x,move_y,pos);
            
        }
    }
   }
   return false

}
solve();

now it’s nowt working fine . please help me

23 thoughts on “Move the knight from position 1 to position 2 with less than 10 moves”

Leave a Comment