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
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.
- Print the longest prefix of the given string which is also the suffix of the same string
- Print all possible combinations of the string by replacing '$' with any other digit from the string
- Print string after removing all (“10” or “01”) from the binary string
- Print all subsequences of a string
- Print all funny words in a string
- Given a string, print all possible palindromic partitions
- Print all permutation of a string using ArrayList
- Print string of odd length in 'X' format
- Print all palindromic partitions of a string
- Print all permutations of a string in Java
- Print Concatenation of Zig-Zag String in 'n' Rows
- Print all palindrome permutations of a string
- Program to print all substrings of a given string
- Print the first and last character of each word in a String
- Print last character of each word in a string
- Print reverse of a string using recursion
- Print all the duplicates in the input string
- Print a number as string of 'A' and 'B' in lexicographic order
- Print the longest palindromic prefix of a given string
- Python | Print all string combination from given numbers