Arcade!
Time limit2sMemory limit512 MB
Given a triangular grid of holes with per-hole bounce probabilities and payouts, compute the expected payout of one dropped ball.
- Level
Medium7 of 10
- Topics
- Probability, Dynamic programming, Math, Simulation
- Solved
- No attempts yet
Problem

Have you been to an arcade lately? Arcade games have needed less and less skill over the years, and the machines you find today decide almost everything by luck. Look at the machine in the picture. Its holes are arranged in a triangle. A ball is dropped near the hole at the top. The ball either falls into that hole, which ends the game, or it bounces to one of the neighboring holes marked by the red arrows (up to 4 of them). Every hole has its own payout, and some payouts are negative. When the ball reaches another hole, the same thing happens again. It either falls in and ends the game, or it bounces to a neighbor. The bouncing can go on forever.
Write a program that computes the expected payout of dropping one ball into the machine.
Input
The first line contains the number of rows of the machine ().
The second line contains integers (), where is the payout the player receives if the ball falls into hole . A payout may be negative. Holes are numbered from top to bottom. Row 1 holds hole 1, row 2 holds holes 2 and 3, and row starts with hole and holds exactly holes.
The next lines each contain five real numbers p0 p1 p2 p3 p4. Line gives, for hole , the probability that the ball bounces to its top-left neighbor (p0), to its top-right neighbor (p1), to its bottom-left neighbor (p2), to its bottom-right neighbor (p3), and the probability that the ball falls into the hole (p4). Each probability is given with at most 3 digits after the decimal point, , and the five values sum to exactly .
For the hole in position of row , the top-left neighbor is hole of row , the top-right neighbor is hole of row , the bottom-left neighbor is hole of row , and the bottom-right neighbor is hole of row . When a hole sits on the boundary and a neighbor does not exist, the probability of bouncing there is always given as . Hole 1, for instance, has no hole above it, so p0 and p1 are both .
You can assume that after the ball has bounced times, the probability that it has not yet fallen into any hole is at most .
Output
Print the expected payout of one game on a single line, rounded to six digits after the decimal point. Always write all six digits, and when the rounded value is , print 0.000000 with no sign.
A Monte Carlo approach, dropping many balls and picking each bounce at random, does not reach this accuracy.