Given a screen containing alphabets from A-Z, we can go from one character to another characters using a remote. The remote contains left, right, top and bottom keys.
Find shortest possible path to type all characters of given string using the remote. Initial position is top left and all characters of input string should be printed in order.
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Input: “GEEK” Output: Move Down Move Right Press OK Move Up Move Right Move Right Move Right Press OK Press OK Move Left Move Left Move Left Move Left Move Down Move Down Press OK
The idea is to consider screen as 2D-matrix of characters. Then we consider all characters of given string one by one and print out the shortest path between current character and next character in the matrix. In order to find shortest path, we consider the coordinates of current character and next character in the matrix. Based on the difference between x and y values of current and next character’s coordinates, we move left, right, top or bottom. i.e.
If row difference is negative, we move up If row difference is positive, we move down If column difference is negative, we go left If column difference is positive, we go right
Below is implementation of above idea
Move Right Move Right Press OK Move Down Move Down Move Right Move Right Press OK Move Left Move Left Move Left Move Left Move Down Move Down Move Down Press OK Move Up Move Right Move Right Move Right Move Right Press OK
This article is contributed by Aditya Goel. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Print all possible combinations of the string by replacing '$' with any other digit from the string
- Print the longest prefix of the given string which is also the suffix of the same string
- Print string after removing all (“10” or “01”) from the binary string
- Print all subsequences of a string
- Print Concatenation of Zig-Zag String in 'n' Rows
- Print all palindrome permutations of a string
- Print last character of each word in a string
- Program to print all substrings of a given string
- Print string of odd length in 'X' format
- Print all permutation of a string using ArrayList
- Print all the duplicates in the input string
- Given a string, print all possible palindromic partitions
- Print reverse of a string using recursion
- Print all funny words in a string
- Print all permutations of a string in Java
- Print the first and last character of each word in a String
- Print all palindromic partitions of a string
- Some interesting shortest path questions | Set 1
- Dijkstra’s shortest path algorithm using set in STL
- Shortest path in a Binary Maze