Minimum number of moves required to reach the destination by the king in a chess board | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void MinSteps ( int SourceX , int SourceY , int DestX , int DestY ) { cout << max ( abs ( SourceX - DestX ) , abs ( SourceY - DestY ) ) << endl ; while ( ( SourceX != DestX ) || ( SourceY != DestY ) ) { if ( SourceX < DestX ) { cout << ' U ' ; SourceX ++ ; } if ( SourceX > DestX ) { cout << ' D ' ; SourceX -- ; } if ( SourceY > DestY ) { cout << ' L ' ; SourceY -- ; } if ( SourceY < DestY ) { cout << ' R ' ; SourceY ++ ; } cout << endl ; } } int main ( ) { int sourceX = 4 , sourceY = 4 ; int destinationX = 7 , destinationY = 0 ; MinSteps ( sourceX , sourceY , destinationX , destinationY ) ; return 0 ; }