Given starting and ending position and a number N. Given that we are allowed to move in only four directions as shown in the image below. The directions of moves are U(), R, D and L. We need to write a program to determine if starting from the given starting position we can reach the given end position in exactly N moves in moving about any direction(Clockwise or Anticlockwise).
Input: start = U , end = L , N = 3 Output: Clockwise Explanation: Step 1: move clockwise to reach R Step 2: move clockwise to reach D Step 3: move clockwise to reach L So we reach from U to L in 3 steps moving in clockwise direction. Input: start = R , end = L , N = 3 Output: Not possible Explanation: It is not possible to start from R and end at L in 3 steps moving about in any direction. Input: start = D , end = R , N = 7 Output: Clockwise Explanation: Starting at D, we complete one complete clockwise round in 4 steps to reach D again, then it takes 3 step to reach R
The idea to solve this problem is to observe that we can complete one round in 4 steps by traveling in any direction (clockwise or anti-clockwise), so taking n%4 steps is equivalent to taking n steps from the starting point. Therefore n is reduced to n%4. Consider the values of ‘U’ as 0, ‘R’ as 1, ‘D’ as 2 and ‘L’ as 3. If the abs(value(a)-value(b)) is 2 and n is also 2, then we can move either in clockwise or anticlockwise direction to reach the end position from the start position. If moving k steps in clockwise direction take us to the end position from start position then we can say that the condition for clockwise move will be (value(a)+k)%4==value(b). Similarly, the condition for anticlockwise move will be (value(a)+k*3)%4==value(b) since taking k step from position a in clockwise direction is equivalent to taking (a + k*3)%4 steps in anticlockwise direction.
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Reflection of a point at 180 degree rotation of another point
- Minimum distance from a point to the line segment using Vectors
- Check whether given floating point number is even or odd
- Program to find the mid-point of a line
- Find the other end point of a line with given one end and mid
- Check whether a point lies inside a sphere or not
- Mirror of a point through a 3 D plane
- Perpendicular distance between a point and a Line in 2 D
- Distance between a point and a Plane in 3 D
- Check if a point lies on or inside a rectangle | Set-2
- C Program to Multiply two Floating Point Numbers
- Find the foot of perpendicular of a point in a 3 D plane
- Program to find the Break Even Point
- Check if any point overlaps the given Circle and Rectangle
- Print pattern using only one loop | Set 1 (Using setw)
- Print the pattern by using one loop | Set 2 (Using Continue Statement)
- Copying file using FileStreams in Java
- Java program to check palindrome (using library methods)
- Solving f(n)= (1) + (2*3) + (4*5*6) ... n using Recursion
- Delete a file using Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.