TopCoder problem "BaseballLineup" used in TCO '03 Finals (Division I Level Two)



Problem Statement

    Baseball is a sport played on a diamond with three bases, first second and third, and a home plate. Players start at home plate, and attempt to hit a ball and run around all three of the bases in order, and end up back at home plate. If a player gets back to home plate, then a run is scored. There are 6 things that can happen to a player when he attempts to hit the ball:
  1. The player may get an out, in which case the next batter steps up, and the runners on base do not move (unless it is the third out of the inning, see below).
  2. The player may be walked, and end up at first base. If there was a player on first base, that player would advance to second. If there were players on first and second, they would advance to second and third base respectively. If there were players on all three bases, the player on third base would score a run and the players on first and second would advance to second and third base respectively. In other words, when a walk occurs, a player only advances when another player advances to the base he is on. Thus, if there were players on first and third bases, and a player was walked, only the player on first would advance.
  3. The player may hit a single. In this case, the player who hit the ball ends up on first base. If there was a player on first base, that player would advance to second. Each player who was on second or third base would make it to home plate and score a run. (So, if there were players on both second and third, two runs would score.)
  4. The player may hit a double, and end up on second base. Each player who was on base scores a run.
  5. The player may hit a triple, and end up on third base. Each player who was on base scores a run.
  6. The player may hit a home run, in which case he and all of the players on base score runs.
A game of baseball goes 9 innings. Each inning consists of 3 outs (outs have no effect until there are 3 of them in an inning). Players take turns batting in order, starting with player 1 (indexed from 1) in the first inning, and continue to bat until there are 3 outs. Once 3 outs have been made, the inning ends and any runners who were on base are no longer on base. In the next inning, the batting order picks up where it left off. So, if player 4 made the last out in the 3rd inning, then player 5 would bat first in the 4th inning. Furthermore, after player 9 bats, it is then player 1's turn.



Your task is, given the stats of all 9 players on a team, return the expected number of runs that team will score in 9 innings. For each player you will be given a String, formatted as "<outs> <walks> <singles> <doubles> <triples> <homeRuns>". Each term in the String will be an integer representing the number of each event per 1000 appearances. Thus, the sum of all the terms in each String will be 1000. For example, "646 107 141 37 0 69" would indicate that 646/1000 times the players gets out, 107/1000 times the player is walked, 141/1000 times the player gets a single, and so on.



Here is an example of how an inning might go, assuming that player 3 is the first batter of the inning and that we start with 0 runs:
player | action   | outs | runs | runners on
-------+----------+------+------+-----------
 3     | walk     | 0    | 0    | first
 4     | out      | 1    | 0    | first
 5     | double   | 1    | 1    | second
 6     | walk     | 1    | 1    | first and second
 7     | single   | 1    | 2    | first and second
 8     | out      | 2    | 2    | first and second
 9     | home run | 2    | 5    | none
 1     | walk     | 2    | 5    | first
 2     | walk     | 2    | 5    | first and second
 3     | out      | 3    | 5    | 
In the next inning, player 4 would bat first. Note that there would be no one on base at the beginning of the next inning, despite the fact that there were two players on base at the end of this inning.



In order to make things a little simpler, we will only allow 20 batters to hit in a single inning. So, after the 20th batter of an inning hits, the inning ends, regardless of how many outs there are. Thus, if every player hits a home run in every at bat, the team will score 20 runs per inning.
 

Definition

    
Class:BaseballLineup
Method:expectedRuns
Parameters:String[]
Returns:double
Method signature:double expectedRuns(String[] stats)
(be sure your method is public)
    
 

Notes

-Your result must have absolute or relative error less than 1e-9.
 

Constraints

-stats will contain exactly 9 elements.
-Each element of stats will be formatted as "<outs> <walks> <singles> <doubles> <triples> <homeRuns>".
-Each of the terms in each element of stats will represent a non-negative integer, with no extra leading 0's.
-The sum of the integers in each element of stats will be 1000.
 

Examples

0)
    
{"652 77 185 53 13 20",
 "649 58 213 74 1 5",
 "646 107 141 37 0 69",
 "650 100 159 55 1 35",
 "683 64 160 49 3 41",
 "663 76 184 43 2 32",
 "712 80 111 63 0 34",
 "693 99 135 48 2 23",
 "824 16 112 16 0 32"}
Returns: 4.799858944836131
The stats of the Cubs' starting lineup in game 7.
1)
    
{"1 0 0 0 0 999",
 "1 0 0 0 0 999",
 "1 0 0 0 0 999",
 "1 0 0 0 0 999",
 "1 0 0 0 0 999",
 "1 0 0 0 0 999",
 "1 0 0 0 0 999",
 "1 0 0 0 0 999",
 "1 0 0 0 0 999"}
Returns: 179.81995685471114
Here, players hit home runs almost every single time. Thus, since there are at most 20 at bats per inning, they are expected to score almost 20 runs per inning, for almost 180 during the whole game.
2)
    
{"0 0 1000 0 0 0",
 "0 0 1000 0 0 0",
 "0 0 1000 0 0 0",
 "0 0 1000 0 0 0",
 "0 0 1000 0 0 0",
 "0 0 1000 0 0 0",
 "0 0 1000 0 0 0",
 "0 0 1000 0 0 0",
 "0 0 1000 0 0 0"}
Returns: 162.0
3)
    
{"0 0 1000 0 0 0",
 "0 0 0 1000 0 0",
 "0 0 1000 0 0 0",
 "0 0 0 1000 0 0",
 "0 0 1000 0 0 0",
 "0 0 0 1000 0 0",
 "0 0 1000 0 0 0",
 "0 0 0 1000 0 0",
 "1000 0 0 0 0 0"}
Returns: 151.0

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=1972

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=4711&pm=1972

Writer:

lars2520

Testers:

zoidal , NGBronson , brett1479 , schveiguy

Problem categories:

Advanced Math, Dynamic Programming