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 email@example.com. 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
- Print the longest prefix of the given string which is also the suffix of the same string
- Print all subsequences of a string
- Print Concatenation of Zig-Zag String in 'n' Rows
- Print all permutations of a string in Java
- Print all funny words in a string
- Program to print all substrings of a given string
- Print all the duplicates in the input string
- Print string of odd length in 'X' format
- Print all palindromic partitions of a string
- Print reverse of a string using recursion
- Given a string, print all possible palindromic partitions
- Print last character of each word in a string
- Print all permutation of a string using ArrayList
- Print the first and last character of each word in a String
- Print all palindrome permutations of a string