Find the House
InterviewTime limit1sMemory limit1024 MB
Starting from a given position, repeatedly follow the triple whose start matches the current position, then print the position reached after all triples are used.
- Level
Medium4 of 10
- Topics
- Hash map, Simulation, Implementation
- Solved
- No attempts yet
Problem
Younghee is on a one-dimensional road and is looking for her friend Jisun's house. To learn the exact position of Jisun's house, Younghee sends Jisun a message with her current position (assume that all positions are represented as integers). A couple of minutes later, Younghee receives a reply: a list of triples from Jisun, with the following explanation attached.
- For each triple in the list, is an integer that denotes the current position, denotes the direction to move from and is either (left) or (right), and is a positive integer that denotes the distance to move from .
- For any two triples and in the list, and are distinct.
- If you are currently at position , a triple always exists in the list (unless every triple in the list has already been referred to). In this case, refer to the triple and move to (if ) or (if ).
- Each triple in the list is referred to exactly once.
- The position after referring to all the triples in the list is the position of Jisun's house.
For example, suppose Younghee is currently at position 0 with a list of four triples: , , , and . Then Younghee first refers to the triple and moves to position . After that, Younghee refers to the triples , , and in order and moves to position , which is the position of Jisun's house. Given , Younghee's current position, and a list of triples, write a program to find the position of Jisun's house.
Input
Your program reads from standard input. The input starts with a line containing an integer (), where is the number of triples in the list. In the following lines, triples are given, where each triple is represented as three values , , and : two integers and and one character (, , and ). After the lines of triples, there is a line containing Younghee's current position as an integer between and .
Output
Your program writes to standard output. Print exactly one line. The line contains the position of Jisun's house.