Problem J
Shredded Bowling
The figure above illustrates what a completed game looks like on a bowling score sheet. In each of ten frames, the bowler has two chances to knock down all the pins. If he gets them all on the first roll, an ‘X’ is placed in the first small box indicating a “strike”, and there is no second roll. Otherwise the number knocked down is recorded and he tries again. If he succeeds in getting the rest of them down, a slash ‘/’ is placed in the second small box indicating a “spare”. A frame that does not yield a strike or spare is called an “open”.
Scoring is done by adding up the pins knocked down in each of the ten frames. In addition, with a strike, the bowler adds in again the number of pins knocked down in the following two rolls. And with a spare, he adds in again the number knocked down in the following single roll. Typically the next roll(s) are from the next frame(s) so that they are included twice in the final score. The final tenth frame is a special case because it is not followed by frames from which to pick up more pins with a strike or spare. Instead, there is an extra small box. See how in the example the score was increased by 30 pins in the final frame because of three rolls, all being strikes. Three strikes in a row is called a “turkey”.
Input
The first line will contain the supposed final score followed by the number of pins knocked down on the third roll that rounds out the tenth frame (or just 0 if there was no third roll). The next ten lines contain two numbers each representing the number of pins knocked down in each roll of a frame, or a 0 if there was no second roll, or the pins knocked down in the extra roll after an opening strike in the tenth frame. (Discovering such a case would be a clue that you have nailed down the tenth frame.) Otherwise 0’s represent either no pins knocked down or there was no extra roll in that frame (as when getting a strike in any of frames 1-9.)
Sample input 1 shows the ten frames in a correct ordering that generates the final score. It is shown in the figure. Sample input 2 has the same ten frames presented in sorted order according to the first roll of the frame. So, of course, your program should discover that it too can generate the final score.
Output
If the total score does match at least one permutation of the ten frames, display the score followed by a “Yes”. If it can not be made to match any permutation, display the score followed by the highest possible score less than the supposed total, if it exists, and the lowest possible score greater than the supposed total, if it exists.
| Sample Input 1 | Sample Output 1 |
|---|---|
168 10 8 2 5 2 10 0 7 3 10 0 10 0 8 0 6 4 5 2 10 10 |
168 Yes |
| Sample Input 2 | Sample Output 2 |
|---|---|
168 10 5 2 5 2 6 4 7 3 8 0 8 2 10 0 10 0 10 0 10 10 |
168 Yes |
