Minimum number of moves required to reach the destination by the king in a chess board | function to Find the minimum number of moves required to reach the destination by the king in a chess board ; Minimum number of steps ; While the king is not in the same row or column as the destination ; Go up ; Go down ; Go left ; Go right ; Driver code
function MinSteps ( SourceX , SourceY , DestX , DestY ) { document . write ( Math . max ( Math . abs ( SourceX - DestX ) , Math . abs ( SourceY - DestY ) ) + " " ) ; while ( ( SourceX != DestX ) || ( SourceY != DestY ) ) { if ( SourceX < DestX ) { document . write ( ' ' ) ; SourceX ++ ; } if ( SourceX > DestX ) { document . write ( ' ' ) ; SourceX -- ; } if ( SourceY > DestY ) { document . write ( ' ' ) ; SourceY -- ; } if ( SourceY < DestY ) { document . write ( ' ' ) ; SourceY ++ ; } document . write ( " " ) ; } } let sourceX = 4 , sourceY = 4 ; let destinationX = 7 , destinationY = 0 ; MinSteps ( sourceX , sourceY , destinationX , destinationY ) ;