TopCoder problem "ConvexPoly" used in TCCC '04 Semifinals 3 (Division I Level Two)



Problem Statement

    Given a rectangular grid of squares, how many different convex polygons can be drawn whose vertices are all points on the grid and whose sides are either horizontal, vertical or (45 degree) diagonal?

We define the width of the given rectangular grid to be the number of intersection points along one if its horizontals (including both the leftmost and rightmost points). Similarly the height of the grid is the number of intersection points along one of its verticals. We want to count the number of different "grid polygons" that could be formed. A grid polygon is a polygon with the following properties:

  1. 1) Each vertex is located at a grid intersection point
  2. 2) Each edge of the polygon is either vertical, horizontal, or at an angle of 45 degrees.
  3. 3) It is a proper polygon, with no edge touching or intersecting another edge (except that adjacent edges touch at their common vertex).
  4. 4) The polygon is convex: every straight line joining any two interior points of the polygon is entirely contained in the interior of the polygon.

Two grid polygons should be counted as different if their boundaries are not identical. If they are the same shape but in different locations they are different. But placing an extra vertex in the middle of an edge does not change the boundary so it does not create a new grid polygon.

Create a class ConvexPoly that contains a method count that is given two ints w, the grid width, and h, the grid height, and that returns the number of grid polygons that could be formed.

 

Definition

    
Class:ConvexPoly
Method:count
Parameters:int, int
Returns:long
Method signature:long count(int w, int h)
(be sure your method is public)
    
 

Constraints

-w will be between 2 and 100 inclusive
-h will be between 2 and 100 inclusive
 

Examples

0)
    
3
2
Returns: 19

XXXXXXX    XXXXXXX    XXXXXXX    XXXXXXX
 X    X     X   X     X     X    X    X
  X   X      X X      X     X    X   X
.  XXXX    .  X  .    XXXXXXX    XXXX  .

XXXX  .    XXXX  .    XXXX  .    X  .  .    .  X  .
 X X       X  X       X X        XX           XX
  XX       X  X       XX         X X         X X
.  X  .    XXXX  .    X  .  .    XXXX  .    XXXX  .


.  XXXX    .  X  .    XXXX  .    XXXX  .    .  XXXX
  X   X      X X       X  X      X   X        X  X
 X    X     X   X       X  X     X    X      X  X
XXXXXXX    XXXXXXX    .  XXXX    XXXXXXX    XXXX  .

.  XXXX    .  XXXX    .  XXXX    .  X  .    .  .  X  
    X X       X  X       X X        XX           XX  
     XX       X  X       XX         X X         X X  
.  .  X    .  XXXX    .  X  .    .  XXXX    .  XXXX  

All 19 possible grid polygons are shown above, with the polygon sides shown with 'X' and unoccupied grid points with '.'
1)
    
2
2
Returns: 5
2)
    
100
100
Returns: 8658940474595

Problem url:

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

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=5078&pm=2012

Writer:

dgoodman

Testers:

lbackstrom , brett1479 , vorthys

Problem categories:

Brute Force, Geometry