Marble Launchers
Time limit1sMemory limit1024 MB
Rotate marble launchers at per-45-degree cost so a marble fired from launcher s reaches launcher e, minimizing total rotation cost and outputting the path.
- Level
Hard8 of 10
- Topics
- Graph, Shortest path, Hash map, Implementation
- Solved
- No attempts yet
Problem
There are marble launchers on the coordinate plane. A marble fired from a launcher travels infinitely until it meets a launcher. If the marble meets a launcher while moving, it is fired again in the direction the launcher faces, and it cannot pass by a launcher without visiting it. A marble launcher can face one of directions: north, northeast, east, southeast, south, southwest, west, and northwest. You can also rotate a launcher clockwise by any amount, and the cost to rotate launcher by degrees is . A launcher can fire a marble at most once. Therefore, if a marble visits a launcher that has already been used, the marble stops at that launcher and moves no further.
By rotating the launchers appropriately, you want the marble to travel from launcher to launcher at minimum cost. Write a program that finds the minimum cost and which launchers the marble passed through.
Input
The first line gives the integers , , and . ()
The next lines give the information for launcher : , , , and , separated by spaces.
- and are the -coordinate and -coordinate of launcher . ()
- is the cost to rotate launcher . ()
- is the direction launcher initially faces. It is one of the uppercase strings
N,NE,E,SE,S,SW,W,NW, meaning north, northeast, east, southeast, south, southwest, west, and northwest, respectively.
No two distinct launchers share the same coordinates, and for every , , , and are integers.
Output
Print the minimum cost on the first line.
On the second line, print the indices of the launchers the marble passed through, including launcher and launcher . If there are multiple routes that achieve the minimum cost, print any of them.
If it is impossible to reach launcher , print on the first line and terminate.
Hint
East is the direction in which the -coordinate increases, and north is the direction in which the -coordinate increases.