Spacetime Sugoroku Road
Time limit8sMemory limit512 MB
Each square i has a forced displacement pi that chains until a plain square is reached; find the minimum number of die rolls to go from square 1 to or past square N, avoiding infinite effect loops.
- Level
Medium7 of 10
- Topics
- Graph, BFS, Shortest path, Simulation
- Solved
- No attempts yet
Problem
The All-Spacetime Unified Dimensional Sugoroku Tournament. You are taking part in that tournament as the representative of 21st-century Earth, competing to decide the single absolute sugoroku champion among more than 500 trillion participants.
The task you face now is one-dimensional sugoroku with yourself as the piece. You start on the starting square at one end and repeatedly roll a giant six-sided die with the numbers 1 through 6, one on each face, advancing by the number rolled. This is the familiar form of sugoroku. You finish when you stop on the goal square at the opposite end from the start. Naturally, the fewer times you roll the die before finishing, the better your result.
Among the squares there are squares with special effects, "advance ○ squares" and "go back ○ squares." If you stop on one, you must advance or go back by the specified number of squares. If moving because of a square's effect lands you on another square with an effect, you keep moving as instructed.
But this is a spacetime sugoroku that cannot be beaten by ordinary means. Frighteningly, an "advance 3 squares" can have a "go back 3 squares" placed 3 squares ahead of it. If you stop on such a square and fall into an infinite loop from the square's effect, you must travel back and forth between squares forever.
Fortunately, your body holds a special ability, 'Probability Warping,' which can turn any desired event into a certain event. With this ability, you can even control the die's outcome freely. Making use of this advantage while avoiding infinite loops, what is the minimum number of die rolls before you finish?
Input
N
p1
p2
.
.
.
pN
The first line of input contains the integer N (3 ≤ N ≤ 100,000). This is the number of squares in the sugoroku. The squares are numbered 1 through N. Square 1 is the start, then 2, 3, ..., N - 1 follow in order of increasing distance from the start, and square N is the goal. If you roll the die on square i and get j, you move to square i + j. However, if i + j exceeds N, you do not go back by the remainder; you are considered to have finished.
The following N lines contain integers pi (-100,000 ≤ pi ≤ 100,000). The integer pi on line 1 + i describes the instruction on square i. If pi > 0 it is "advance pi squares," if pi < 0 it is "go back -pi squares," and if pi = 0 the square has no effect. p1 and pN are always 0. No square's effect ever instructs you to move before the start or past the goal.
You may assume the given sugoroku can be finished.
Output
Print the minimum number of die rolls before finishing.