Tracking Bio-bots
Time limit2sMemory limit1024 MB
Given a grid room with horizontal walls, count the squares from which a robot moving only north or east can never reach the northeast exit.
- Level
Medium6 of 10
- Topics
- Intervals, Sorting, Simulation
- Solved
- No attempts yet
Problem
The researchers at International Bio-bot Makers (IBM) have invented a new kind of Bio-bot, a robot with behavior that mimics biological organisms. The robot is still in an early stage of development, and the current models are similar to simple four-wheeled rovers. Like most modern robots, Bio-bots are not very mobile. Their weak motors and limited turning capability restrict their movement considerably, even in simple environments with few obstacles.
Bio-bots currently operate in a room shaped like an grid. The exit is in the northeast corner, and the room slopes down toward it. A Bio-bot can therefore move only north or east at any time. Some squares in the room are occupied by walls, which completely block the robot.

In Figure 1, a Bio-bot on square A can leave the room, but a Bio-bot on square B is trapped inside no matter what it does. Squares like B are called "stuck squares." (Walls are not stuck squares.) Given the description of a room, count the total number of stuck squares in the room.
Input
Input consists of multiple test cases, each describing one room. The first line of a test case has three integers m, n, and w (, ). They give the number of rows, the number of columns, and the number of horizontal walls in the room.
Each of the next w lines has four integers x1, y1, x2, y2, the coordinates of the squares at the two ends of one wall. All walls run from west to east, so and . Walls do not overlap. The southwest corner of the room is at (0,0), and the northeast corner is at (n-1, m-1).
The last test case is followed by a line containing 0 0 0.
Output
For each test case, print one line in the form "Case k: s", where k is the test case number and s is the number of stuck squares in that room.