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
< ? php function MinSteps ( $ SourceX , $ SourceY , $ DestX , $ DestY ) { echo max ( abs ( $ SourceX - $ DestX ) , abs ( $ SourceY - $ DestY ) ) . " " ; while ( ( $ SourceX != $ DestX ) || ( $ SourceY != $ DestY ) ) { if ( $ SourceX < $ DestX ) { echo ' U ' ; $ SourceX ++ ; } if ( $ SourceX > $ DestX ) { echo ' D ' ; $ SourceX -- ; } if ( $ SourceY > $ DestY ) { echo ' L ' ; $ SourceY -- ; } if ( $ SourceY < $ DestY ) { echo ' R ' ; $ SourceY ++ ; } echo " STRNEWLINE " ; } } $ sourceX = 4 ; $ sourceY = 4 ; $ destinationX = 7 ; $ destinationY = 0 ; MinSteps ( $ sourceX , $ sourceY , $ destinationX , $ destinationY ) ; ? >