Minimum number of moves required to reach the destination by the king in a chess board
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++
#include <bits/stdc++.h>
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;
}
|
Java
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);
}
}
|
Python3
def MinSteps(SourceX, SourceY, DestX, DestY):
print ( max ( abs (SourceX - DestX), abs (SourceY - DestY)))
while ((SourceX ! = DestX) or (SourceY ! = DestY)):
if (SourceX < DestX):
print ( 'U' ,end = "")
SourceX + = 1
if (SourceX > DestX):
print ( 'D' ,end = "")
SourceX - = 1
if (SourceY > DestY):
print ( 'L' )
SourceY - = 1
if (SourceY < DestY):
print ( 'R' ,end = "")
SourceY + = 1
if __name__ = = '__main__' :
sourceX = 4
sourceY = 4
destinationX = 7
destinationY = 0
MinSteps(sourceX, sourceY, destinationX, destinationY)
|
C#
using System;
class GFG
{
static void MinSteps( int SourceX, int SourceY,
int DestX, int DestY)
{
Console.WriteLine(Math.Max(Math.Abs(SourceX - DestX),
Math.Abs(SourceY - DestY)));
while ((SourceX != DestX) ||
(SourceY != DestY))
{
if (SourceX < DestX)
{
Console.Write( 'U' );
SourceX++;
}
if (SourceX > DestX)
{
Console.Write( 'D' );
SourceX--;
}
if (SourceY > DestY)
{
Console.Write( 'L' );
SourceY--;
}
if (SourceY < DestY)
{
Console.Write( 'R' );
SourceY++;
}
Console.WriteLine();
}
}
public static void Main ()
{
int sourceX = 4, sourceY = 4;
int destinationX = 7, destinationY = 0;
MinSteps(sourceX, sourceY,
destinationX, destinationY);
}
}
|
PHP
<?php
function MinSteps( $SourceX , $SourceY ,
$DestX , $DestY )
{
echo max( abs ( $SourceX - $DestX ),
abs ( $SourceY - $DestY )) . "\n" ;
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 "\n" ;
}
}
$sourceX = 4; $sourceY = 4;
$destinationX = 7; $destinationY = 0;
MinSteps( $sourceX , $sourceY ,
$destinationX , $destinationY );
?>
|
Javascript
<script>
function MinSteps(SourceX, SourceY, DestX, DestY)
{
document.write(Math.max(Math.abs(SourceX - DestX),
Math.abs(SourceY - DestY)) + "<br>" );
while ((SourceX != DestX) ||
(SourceY != DestY))
{
if (SourceX < DestX)
{
document.write( 'U' );
SourceX++;
}
if (SourceX > DestX)
{
document.write( 'D' );
SourceX--;
}
if (SourceY > DestY) {
document.write( 'L' );
SourceY--;
}
if (SourceY < DestY)
{
document.write( 'R' );
SourceY++;
}
document.write( "<br>" );
}
}
let sourceX = 4, sourceY = 4;
let destinationX = 7, destinationY = 0;
MinSteps(sourceX, sourceY, destinationX,
destinationY);
</script>
|
Time complexity: O(max(a, b)), where a = sourceX and b = sourceY
Auxiliary Space: O(1), since no extra space has been taken.
Last Updated :
11 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...