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 repectively.
Input: sourceX = 4, sourceY = 4, destinationX = 3, destinationY = 5
Input: sourceX = 4, sourceY = 4, destinationX = 7, destinationY = 0
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:
4 UL UL UL L
- Total position where king can reach on a chessboard in exactly M moves | Set 2
- Total position where king can reach on a chessboard in exactly M moves
- Minimum number of given moves required to make N divisible by 25
- Find the minimum number of preprocess moves required to make two strings equal
- Minimum moves to reach target on a infinite line | Set 2
- Find minimum moves to reach target on an infinite line
- Number of moves required to guess a permutation.
- Minimum time to reach a point with +t and -t moves at time t
- Number of steps required to reach point (x,y) from (0,0) using zig-zag way
- Minimum number of jumps to reach end
- Find the minimum number of steps to reach M from N
- Minimum number of given powers of 2 required to represent a number
- Minimum number of operations required to reduce N to 1
- Minimum number of palindromes required to express N as a sum | Set 2
- Minimum number of palindromes required to express N as a sum | Set 1
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.