Minimum number of moves required to reach the destination by the king in a chess board - GeeksforGeeks

17 Jan, 2022
Given four integers sourceX, sourceY, destinationX and destinationY which represent the source and destination coordinates on a chessboard. The task is to find the minimum number of moves required by the king to reach from source to destination. A king can move to the square that has a common side or a common vertex with the square the king is currently in (generally there are 8 different squares he can move to).
Print path using L, R, U, D, LU, LD, RU and RD where L, R, U and D represent left, right, up and down respectively.
Examples: 
Input: sourceX = 4, sourceY = 4, destinationX = 3, destinationY = 5 Output: 1 DR
Input: sourceX = 4, sourceY = 4, destinationX = 7, destinationY = 0 Output: 4 UL UL UL L 
Approach: Move in the diagonal direction towards the destination until the king reaches same column or same row as the destination, then move towards the destination in a straight line.
Below is the implementation of the above approach: 
C++
Java
Python3
C#
PHP
Javascript
// C++ program to Find the minimum number of moves required to// reach the destination by the king in a chess board#include <bits/stdc++.h>using namespace std; // function to Find the minimum number of moves required to// reach the destination by the king in a chess boardvoid MinSteps(int SourceX, int SourceY, int DestX, int DestY){    // minimum number of steps    cout << max(abs(SourceX - DestX), abs(SourceY - DestY)) << endl;     // while the king is not in the same row or column    // as the destination    while ((SourceX != DestX) || (SourceY != DestY)) {         // Go up        if (SourceX < DestX) {            cout << 'U';            SourceX++;        }         // Go down        if (SourceX > DestX) {            cout << 'D';            SourceX--;        }         // Go left        if (SourceY > DestY) {            cout << 'L';            SourceY--;        }         // Go right        if (SourceY < DestY) {            cout << 'R';            SourceY++;        }         cout << endl;    }} // Driver codeint main(){    int sourceX = 4, sourceY = 4;    int destinationX = 7, destinationY = 0;     MinSteps(sourceX, sourceY, destinationX, destinationY);     return 0;}
// Java program to Find the minimum// number of moves required to reach// the destination by the king in a// chess boardimport java.io.*; class GFG{ // function to Find the minimum number// of moves required to reach the// destination by the king in a chess boardstatic void MinSteps(int SourceX, int SourceY,                     int DestX, int DestY){    // minimum number of steps    System.out.println(Math.max(Math.abs(SourceX - DestX),                     Math.abs(SourceY - DestY)));     // while the king is not in the same    // row or column as the destination    while ((SourceX != DestX) ||           (SourceY != DestY))    {         // Go up        if (SourceX < DestX)        {            System.out.print( 'U');            SourceX++;        }         // Go down        if (SourceX > DestX)        {            System.out.println( 'D');            SourceX--;        }         // Go left        if (SourceY > DestY)        {            System.out.print( 'L');            SourceY--;        }         // Go right        if (SourceY < DestY)        {            System.out.print( 'R');            SourceY++;        }         System.out.println();    }} // Driver codepublic static void main (String[] args){    int sourceX = 4, sourceY = 4;    int destinationX = 7, destinationY = 0;     MinSteps(sourceX, sourceY,             destinationX, destinationY);}} // This code is contributed by inder_verma
# Python 3 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 boarddef MinSteps(SourceX, SourceY, DestX, DestY):    # minimum number of steps    print(max(abs(SourceX - DestX), abs(SourceY - DestY)))     # while the king is not in the same row or column    # as the destination    while ((SourceX != DestX) or (SourceY != DestY)):        # Go up        if (SourceX < DestX):            print('U',end = "")            SourceX += 1                 # Go down        if (SourceX > DestX):            print('D',end = "")            SourceX -= 1                 # Go left        if (SourceY > DestY):            print('L')            SourceY -= 1                 # Go right        if (SourceY < DestY):            print('R',end = "")            SourceY += 1          # Driver codeif __name__ == '__main__':    sourceX = 4    sourceY = 4    destinationX = 7    destinationY = 0     MinSteps(sourceX, sourceY, destinationX, destinationY) # This code is contributed by# Surendra_Gangwar
// C# program to Find the minimum// number of moves required to reach// the destination by the king in a// chess boardusing System; class GFG{ // function to Find the minimum number// of moves required to reach the// destination by the king in a chess boardstatic void MinSteps(int SourceX, int SourceY,                     int DestX, int DestY){    // minimum number of steps    Console.WriteLine(Math.Max(Math.Abs(SourceX - DestX),                               Math.Abs(SourceY - DestY)));     // while the king is not in the same    // row or column as the destination    while ((SourceX != DestX) ||           (SourceY != DestY))    {         // Go up        if (SourceX < DestX)        {            Console.Write( 'U');            SourceX++;        }         // Go down        if (SourceX > DestX)        {            Console.Write( 'D');            SourceX--;        }         // Go left        if (SourceY > DestY)        {            Console.Write( 'L');            SourceY--;        }         // Go right        if (SourceY < DestY)        {            Console.Write( 'R');            SourceY++;        }         Console.WriteLine();    }} // Driver codepublic static void Main (){    int sourceX = 4, sourceY = 4;    int destinationX = 7, destinationY = 0;     MinSteps(sourceX, sourceY,             destinationX, destinationY);}} // This code is contributed by inder_verma
<?php// PHP 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 boardfunction MinSteps($SourceX, $SourceY,                  $DestX, $DestY){    // minimum number of steps    echo max(abs($SourceX - $DestX),             abs($SourceY - $DestY)) . "\n";     // while the king is not in the    // same row or column as the destination    while (($SourceX != $DestX) ||           ($SourceY != $DestY))    {         // Go up        if ($SourceX < $DestX)        {            echo 'U';            $SourceX++;        }         // Go down        if ($SourceX > $DestX)        {            echo 'D';            $SourceX--;        }         // Go left        if ($SourceY > $DestY)        {            echo 'L';            $SourceY--;        }         // Go right        if ($SourceY < $DestY)        {            echo 'R';            $SourceY++;        }         echo "\n";    }} // Driver code$sourceX = 4; $sourceY = 4;$destinationX = 7; $destinationY = 0; MinSteps($sourceX, $sourceY,         $destinationX, $destinationY);         // This code is contributed// by Akanksha Rai?>
<script> // Javascript 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 boardfunction MinSteps(SourceX, SourceY, DestX, DestY){         // Minimum number of steps    document.write(Math.max(Math.abs(SourceX - DestX),    Math.abs(SourceY - DestY)) + "<br>");     // While the king is not in the same row    // or column as the destination    while ((SourceX != DestX) ||           (SourceY != DestY))    {                 // Go up        if (SourceX < DestX)        {            document.write('U');            SourceX++;        }         // Go down        if (SourceX > DestX)        {            document.write('D');            SourceX--;        }         // Go left        if (SourceY > DestY) {            document.write('L');            SourceY--;        }         // Go right        if (SourceY < DestY)        {            document.write('R');            SourceY++;        }        document.write("<br>");    }} // Driver codelet sourceX = 4, sourceY = 4;let destinationX = 7, destinationY = 0; MinSteps(sourceX, sourceY, destinationX,         destinationY);          // This code is contributed by souravmahato348 </script>
4
UL
UL
UL
L
 
Time complexity: O(max(a, b)), where a = sourceX and b = sourceY
Auxiliary Space: O(1)
inderDuMCA
Akanksha_Rai
SURENDRA_GANGWAR
souravmahato348
sagar0719kumar
samim2000
chessboard-problems
Competitive Programming
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Prefix Sum Array - Implementation and Applications in Competitive Programming
Ordered Set and GNU C++ PBDS
Modulo 10^9+7 (1000000007)
Bits manipulation (Important tactics)
7 Best Coding Challenge Websites in 2020
Program for Fibonacci numbers
Write a program to print all permutations of a given string
C++ Data Types
Set in C++ Standard Template Library (STL)
Coin Change | DP-7