Vector Field
InterviewTime limit5sMemory limit512 MB
Given n points that each redirect a proton along an axis, find the most points a proton can pass through when its start position and direction are chosen freely.
- Level
Medium4 of 10
- Topics
- Graph, Simulation
- Solved
- No attempts yet
Problem
In 20015, JAG (Jagan Acceleration Group) built a new accelerator called "Force Point" for proton collision experiments on the two-dimensional -plane. When a proton touches a Force Point, its speed doubles and its direction changes. The new direction is one of four: the positive or negative direction of the -axis or the -axis. The type of the Force Point determines the direction. A Force Point accelerates a proton only once, then it disappears. A set of Force Points on the plane is called a 2D Force Point Field. Passing a proton through the Force Points in sequence can accelerate it up to a target speed.
JAG has these technical limitations:
- JAG cannot generate a Force Point at a specified position with a specified type.
- JAG cannot generate a Force Point after a proton is put into the field.
- JAG cannot move Force Points.
- JAG cannot change a proton's direction except through Force Points.
- JAG can use only one proton for each 2D Force Point Field.
- JAG can place a proton moving in any direction with speed 1 at any position in the field.
To reach the maximum speed, the engineers must observe the generated field, then pick the initial position and initial direction of the proton so that it is accelerated by as many Force Points as possible.
The field has Force Points. The position and the direction type of each point are known. Write a program that computes the maximum speed a proton can reach when JAG places the proton optimally.
Input
The input is a single test case in this format:
$n$
$x_1$ $y_1$ $d_1$
...
$x_n$ $y_n$ $d_n$
The first line contains one integer (). Each of the next lines contains two integers and () and one character , which is one of '>', 'v', '<', or '^'. The type '>' sends the proton in the positive direction, 'v' in the positive direction, '<' in the negative direction, and '^' in the negative direction. No two Force Points share the same coordinates.
Output
Print one line with the integer , where is the fastest speed the proton can reach.