Sweeping Robot
Time limit1sMemory limit128 MB
Count the distinct grid cells a robot sees sideways from each unit of its grid path inside an orthogonal polygonal museum, with walls blocking the view.
- Level
Medium4 of 10
- Topics
- Simulation, Geometry, Matrix
- Solved
- No attempts yet
Problem
A robot with a camera on it sweeps some areas of a museum. The museum is a polygon whose walls are all horizontal or vertical, drawn on a mesh of cells as in the figure, and every vertex of the polygon lies on a mesh vertex.
The robot also moves only along mesh edges, either horizontally or vertically. Its camera sweeps the two directions perpendicular to the direction of travel. While the robot moves horizontally the camera points vertically, so it sees only cells to the north and to the south of that horizontal part of the path. While the robot moves vertically it sees only cells to the east and to the west of that vertical part of the path. Walls stop the view. Cut the path into pieces of length 1, and from each piece the camera sees, in both perpendicular directions, every cell up to the first wall.
The figure shows a polygon together with a path of the robot (the dashed line). The dotted squares are the ones the robot sees.

Given such a polygon and a robot path inside it, compute the total area (total number of squares) the robot sees. A cell seen more than once counts once.
Input
The input has several test cases. Each test case starts with a line holding two integers and (), where is the number of walls (or vertices) of the museum and is the number of vertices of the robot path.
The next lines describe the museum vertices. The th of these lines holds two space separated non-negative integers and , neither exceeding 500, the and coordinates of the th vertex of the museum. There is a wall between vertex and vertex , and the th vertex is the first vertex.
The next lines describe the vertices of the robot path, in the order they appear on the path from the starting point to the ending point. Each of these lines holds the and coordinates of a vertex. The path lies inside the museum. Its vertices, but not its edges, may touch the museum walls. The path may cross itself.
The last line of the input is 0 0, which you should not process.
Output
For each test case, print on its own line the total area (total number of squares) the robot sees.