Background
On its trip, a train has to pass a lot of points (American English: switches) and signals. The train抯 track depends on the status of points and signals. The responsible operator on the signal box does not handle them separately, but tells the signal box the start and destination signal of the train抯 journey. The box then determines the correct status of points and signals and brings them into the right position.
Figure 9 shows a sample scenario in which railway tracks are shown as solid lines and signals are drawn as triangles (this is also the first scenario of the sample input). Signals have a sense of direction: they are only valid for the direction in which the triangle points (e.g., signal A is valid for trains running from left to right, see also Figure 10). Points are located where railway tracks meet (e.g., at points W1, W2, etc.). Points have a front side (i.e., the side from which a train can take alternative directions) and a back side and can be in two positions, named + and -. If a train comes from the front side, it leaves the point at the + or - leg, dependent on the point抯 position (see Figure 11). If the train comes from one of the the back legs, it leaves it at the front leg. Even then the point has to be put into the right state, otherwise it gets damaged!
Problem
Your task is to implement an automatic signal box, i.e., write a program which finds the correct position of points and signals for a given start and destination. The signal box should follow these rules:
A journey can only start and end at a signal. Both signals have to be in the same direction!
During a journey a train must not change its direction.
The journey consists of a sequence of signal and point settings. A signal is only taken into account for the journey if it has the right direction. A point along the way is always taken into account.
If there is more than one possible track from the start signal to the destination signal, the correct one is determined by the following scheme:
Consider a set of path selection rules. These are given as a triple (x, y, z) of point identifiers x and y, and a position z. A selection rule has the following meaning:
If there are alternative paths starting at point x and ending at point y where x is approached from the front and y from the back, then consider only paths in which x is in position z (z is either + or -).
If no such rule exists for a given point x, the - position must be chosen.
The sample in- and output demonstrate the application of the rules. Furthermore, you can make the following assumptions:
The track plan is acyclic.
Within a path, each element is only used once or not at all.
If for a given point x several rules (x, y, z) exist, they will agree on the position to be chosen.