TopCoder problem "Touchdown" used in TCHS SRM 20 (Division I Level Three)



Problem Statement

    In American football, there are two teams. At any one time one team is on offense and the other is on defense. The offense possesses the football and tries to move it toward the defense's goal line while the defense tries to prevent the offense from doing this. If the offense moves the ball beyond the defense's goal line, they score a touchdown. When the offense starts it is given 4 plays which are refered to as "downs". If the ball is moved yardsToGo within those 4 downs, they score a touchdown. If after the 4 downs, the offense failed to score a touchdown and also failed to move the ball at least 10 yards, they lose possession of the ball. If the offense moves the ball 10 or more yards towards the defense's goal line but doesn't score a touchdown, their down count resets to 4; that is no downs carry over and you can never have more than 4 downs at any one time. Each play costs the offense 1 down.



It's fourth quarter, time is running out and your team needs a touchdown to win. Coach has a set of plays that are guaranteed to make a specified number of yards. You will be given an int yardsToGo specifying the number of yards needed to make a touchdown and a int[] plays specifying the number of yards each play will make. Each play can only be run one time to guarantee its effectiveness. You are to return the total minimum number of downs required to make a touchdown or -1 if it is not possible. Note that you cannot exceed yardsToGo by more than 10 because that would put you out of bounds; that is, if you have 43 yardsToGo and no set of plays will advance the ball between 43 and 53 inclusive, you cannot make a touchdown. Also note that you must advance the ball by 10 or more yards to get a fresh set of 4 downs.
 

Definition

    
Class:Touchdown
Method:howMany
Parameters:int, int[]
Returns:int
Method signature:int howMany(int yardsToGo, int[] plays)
(be sure your method is public)
    
 

Constraints

-yardsToGo will be between 1 and 99, inclusive.
-plays will contain between 1 and 15 elements, inclusive.
-Each element in plays will be between 1 and yardsToGo, inclusive.
 

Examples

0)
    
25
{2, 2, 3, 2, 3, 3, 1, 2, 1, 4, 2}
Returns: 11
Doing these plays in this order will get a touchdown: {4, 2, 2, 2}, {3, 3, 2, 2} and {3, 1, 1}.
1)
    
60
{20, 20, 33, 39, 59, 59}
Returns: -1
There is no way to get 60 to 70 yards; any 3 plays is greater than 70, and any 2 plays is either less than 60 or greater than 70.
2)
    
13
{4, 4, 4, 2, 2, 1, 1}
Returns: 4
Running plays that get: {4, 4, 4, (2 or 1)} will get a touchdown!
3)
    
25
{7, 4, 4, 3, 1, 1, 1, 1, 1, 1, 1}
Returns: 11
Here, we can make a touchdown by running the following plays: {1, 1, 3, 7}, {1, 1, 4, 4} and {1, 1, 1}
4)
    
12
{2, 2, 2, 2, 2, 2}
Returns: -1
At first glance one might think a touchdown is possible in 6 plays, but note that 10 or more yards cannot be made in 4 plays.

Problem url:

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

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=10072&pm=6867

Writer:

Uranium-235

Testers:

PabloGilberto , brett1479 , Olexiy

Problem categories:

Dynamic Programming