ACSL Practice 2009
The road map of Kansas is a grid where there are straight roads going North to South and straight roads East to West each mile apart. A truck driver gets instructions in the following form: Drive for two hours north at miles per hour, then for hours east at miles per hour, etc. The driver has to take a hour break after hours of driving. After how many breaks does the driver first pass through or return to the place where he started?
Example 1. Drive for two hours south at miles per hour, then for hours west at miles per hour, then for hours north at miles per hour, then for hours east at miles per hour, then for hours north at miles per hour, and then for hours south at miles per hour.
When following these instructions, the driver will pass through his starting point after taking two breaks.
Example 2. Drive for hour south at miles per hour, then for hours west at miles per hour, then for hour north at miles per hour, then for hour east at miles per hour, and then for hours north at miles per hour.
When following these instructions, the driver will not pass through nor return to where he started.
Example 3. Drive for four hours west at miles per hour, then drive for eight hours north at miles per hour, then drive for four hours east at miles per hour, then drive for eight hours south at miles per hour, then drive for ten hours east at miles per hour.
When following these instructions, the driver will pass through his starting point after taking breaks.
Input Specification
The input consists of several lines. The first line contains an integer which is the number of instructions, followed by one line for each instruction, indicating the direction, the number of hours and the speed in miles per hour in this order. The geographical directions are indicated by the position of the small hand on an analog clock; stands for north, for south, for west, and for east.
Output Specification
The output contains a single integer which is the number of breaks taken
before the driver first passes through or returns to the place where he
started. If the driver does not reach or pass through the place where he
started after following all instructions, the output should contain the
number -1
.
Sample Input 1
6
6 2 30
9 4 40
12 3 20
3 6 60
12 5 30
6 6 60
Sample Output 1
2
Sample Input 2
5
6 1 30
9 2 40
12 1 30
3 1 79
12 5 30
Sample Output 2
-1
Sample Input 3
5
9 4 60
12 8 80
3 4 60
6 8 100
3 10 20
Sample Output 3
4
Comments
It is unclear whether or not the driver's break interrupts the instruction, i.e. whether or not the idle break still uses up an hour of the instruction's time. (edit: nvm)