Connection Status:
Competition Arena > ConvexPoly
TCCC '04 Semifinals 3 · 2004-02-23 · by dgoodman · Brute Force, Geometry
Class Name: ConvexPoly
Return Type: long
Method Name: count
Arg Types: (int, int)
Problem Statement

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.

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)
10
2
Returns: 369
3)
2
10
Returns: 369
4)
37
24
Returns: 602297464

Submissions are judged against all 26 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class ConvexPoly with a public method long long count(int w, int h) · 26 test cases · 2 s / 256 MB per case

Submitting as anonymous