Minimum number of moves required to reach the destination by the king in a chess board | Java program to Find the 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
import java . io . * ; class GFG { static void MinSteps ( int SourceX , int SourceY , int DestX , int DestY ) { System . out . println ( Math . max ( Math . abs ( SourceX - DestX ) , Math . abs ( SourceY - DestY ) ) ) ; while ( ( SourceX != DestX ) || ( SourceY != DestY ) ) { if ( SourceX < DestX ) { System . out . print ( ' U ' ) ; SourceX ++ ; } if ( SourceX > DestX ) { System . out . println ( ' D ' ) ; SourceX -- ; } if ( SourceY > DestY ) { System . out . print ( ' L ' ) ; SourceY -- ; } if ( SourceY < DestY ) { System . out . print ( ' R ' ) ; SourceY ++ ; } System . out . println ( ) ; } } public static void main ( String [ ] args ) { int sourceX = 4 , sourceY = 4 ; int destinationX = 7 , destinationY = 0 ; MinSteps ( sourceX , sourceY , destinationX , destinationY ) ; } }