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
- Expected number of moves to reach the end of a board | Dynamic programming
- Expected number of moves to reach the end of a board | Matrix Exponentiation
- Total position where king can reach on a chessboard in exactly M moves
- Total position where king can reach on a chessboard in exactly M moves | Set 2
- Number of ways to place two queens on a N*N chess-board
- Minimum number of moves to reach N starting from (1, 1)
- 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 possible modifications in the matrix to reach destination
- Draw a Chess Board using Graphics Programming in C
- 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.
- Count minimum factor jumps required to reach the end of an Array
- Minimum broadcast range required by M towers to reach N houses
- 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
- Minimize the number of steps required to reach the end of the array | Set 2
- Minimum number of jumps to reach end
- Find the minimum number of steps to reach M from N
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.