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
- Shortest Path using Meet In The Middle
- Some interesting shortest path questions | Set 1
- Shortest path in a Binary Maze
- Dijkstra’s shortest path algorithm using set in STL
- Print all subsequences of a string
- Dijkstra's Shortest Path Algorithm using priority_queue of STL
- 0-1 BFS (Shortest Path in a Binary Weight Graph)
- Dijkstra's shortest path with minimum edges
- Shortest Path in Directed Acyclic Graph
- Print all palindrome permutations of a string
- Print all permutations of a string in Java
- Given a string, print all possible palindromic partitions
- Print Concatenation of Zig-Zag String in 'n' Rows
- Print string of odd length in 'X' format