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
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.
- Dijkstra's shortest path algorithm | Greedy Algo-7
- Shortest Path in Directed Acyclic Graph
- Some interesting shortest path questions | Set 1
- Shortest path with exactly k edges in a directed and weighted graph
- Shortest Path in a weighted Graph where weight of an edge is 1 or 2
- Printing Paths in Dijkstra's Shortest Path Algorithm
- Dijkstra’s shortest path algorithm using set in STL
- Dijkstra's Shortest Path Algorithm using priority_queue of STL
- Find shortest safe route in a path with landmines
- 0-1 BFS (Shortest Path in a Binary Weight Graph)
- Shortest path to reach one prime to other by changing single digit at a time
- Probabilistic shortest path routing algorithm for optical networks
- Multi Source Shortest Path in Unweighted Graph
- Dijkstra's shortest path with minimum edges
- Dijkstra's shortest path algorithm in Java using PriorityQueue
- Shortest Path using Meet In The Middle
- Check if given path between two nodes of a graph represents a shortest paths
- D'Esopo-Pape Algorithm : Single Source Shortest Path
- Single source shortest path between two cities
- Shortest path in a Binary Maze