TopCoder problem "LogCabin" used in TCO04 Qual 1 (Division I Level Three)



Problem Statement

    

A log-cabin quilt is made by sewing strips of fabric in a rectangular spiral. All strips of fabric are 1 inch wide. The first two strips are 1 inch long, the second two strips are 2 inches long, the third two strips are 3 inches long, and so on. Each new strip is sewn perpendicular to the previous strip in a counter-clockwise spiral, as shown below:

   GFFFI
   GCBEI
   GCAEI
   GDDEI
   HHHHI
This quilt is made with nine strips, represented by the characters 'A' through 'I'. The first strip is labeled 'A', the second strip is labeled 'B', and so on.

You want to design your quilt so that no two adjacent strips are made from the same fabric. Two strips are adjacent if they share a common side. For example, the 'C' strip in the above picture is adjacent to the 'A', 'B', 'D', 'F', and 'G' strips, and therefore must be a different fabric from each of those five strips. It turns out that you never need more than four different fabrics to accomplish this goal.

You have four 1-inch wide scraps of different fabrics that you will be cutting into the strips that you need. You want to know how big a quilt you can make, as measured by the number of strips in the finished quilt. For example, the above quilt has nine strips. You will be given a int[] fabrics containing exactly four elements. Each element contains the length in inches of a different scrap.

 

Definition

    
Class:LogCabin
Method:maxStrips
Parameters:int[]
Returns:int
Method signature:int maxStrips(int[] fabrics)
(be sure your method is public)
    
 

Constraints

-fabrics will contain exactly four elements.
-Each element of fabrics will be between 1 and 100, inclusive.
 

Examples

0)
    
{ 1, 2, 3, 4 }
Returns: 5
The maximum is five strips. For example, if we label the fabrics 1-4 according to their original length, then we could make the following quilt:
   234
   214
   334
We cut fabric 3 into two strips: one that is 1 inch long and one that is 2 inches long. We also cut a 3-inch strip from fabric 4. Notice that we have 1 inch of fabric 4 left over.
1)
    
{ 1, 1, 1, 1 }
Returns: 2
2)
    
{ 100, 100, 100, 100 }
Returns: 39
3)
    
{ 10, 36, 20, 38 }
Returns: 16

Problem url:

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

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=5873&pm=2008

Writer:

vorthys

Testers:

PabloGilberto , lbackstrom , schveiguy

Problem categories:

Brute Force