#P6886. Safe Fly Swatter Placement
Safe Fly Swatter Placement
Safe Fly Swatter Placement
Norman has a fly swatter in the shape of a $K$-gon. The fly swatter has fixed orientation (i.e. it cannot be rotated or reflected) and its vertex coordinates are defined in a template with integer coordinates. Norman wants to count the number of ways to translate the fly swatter so that:
- All its vertices lie on the integer grid points inside the axis‐aligned rectangle with bottom‐left corner $(0,0)$ and top‐right corner $(X_p,Y_p)$.
- None of the $N$ flies, each given as a point $(X,Y)$, lies inside or on the boundary of the translated fly swatter. In other words, a fly is harmed if and only if it lies in the interior or on the boundary of the fly swatter.
The input provides:
- The first line contains three integers: $K$, $X_p$, and $Y_p$.
- The next $K$ lines each contain two integers describing the template coordinates of the fly swatter's vertices.
- The following line contains an integer $N$, the number of flies.
- Then $N$ lines follow, each with two integers representing the coordinates of a fly.
Let the template polygon's vertices be $(x_i, y_i)$, $1 \le i \le K$. A translation by $(dx,dy)$ moves each vertex to $(x_i+dx, y_i+dy)$. The translation is valid only if:
- All translated vertices satisfy $0\le x_i+dx\le X_p$ and $0\le y_i+dy\le Y_p$.
- No fly $(X,Y)$ satisfies that it lies inside or on the boundary of the translated polygon.
Your task is to compute the total number of valid translations.
Note: A point is a grid point if both its coordinates are integers. The formula for determining if a point is inside a polygon can be implemented using the ray casting algorithm. Also, to check if a point lies on a line segment between $(x_1,y_1)$ and $(x_2,y_2)$, you may check if the cross product is zero and the point lies between the endpoints.
inputFormat
The input is given from standard input and consists of the following lines:
- The first line contains three space-separated integers:
K
(the number of polygon vertices),X_p
, andY_p
(the rectangle's top‐right corner). - The next
K
lines each contain two integers, representing the coordinates of the fly swatter's vertices in the template. - The next line contains an integer
N
representing the number of flies. - Each of the following
N
lines contains two integers, the coordinates of a fly.
All coordinates are integers.
outputFormat
Output a single integer: the number of valid placements (translations) of the fly swatter such that all its vertices lie inside the rectangle and none of the flies is harmed.
sample
4 4 4
0 0
1 0
1 1
0 1
1
2 2
12