The Happy Worm
Time limit1sMemory limit128 MB
Count maximal horizontal and vertical runs of empty cells that are at least 2 long in a field with stones.
- Level
Medium4 of 10
- Topics
- Sorting, Implementation, Array, Hash map
- Solved
- No attempts yet
Problem
A happy worm lives in an rectangular field. Some cells of the field contain a stone; every other cell is empty (each cell is either empty or holds exactly one stone).
When the worm sleeps it lies in a straight line: either horizontally, along a single row, or vertically, along a single column. It then stretches so that its length is as large as possible, extending from where it lies in both directions until it is stopped by a stone or by the edge of the field. The worm may never occupy a cell that contains a stone or a cell outside the field, and while sleeping it must be at least cells long.
A sleeping position is therefore a maximal straight run of empty cells (in one row or one column) whose length is at least . Two positions are different when they cover different sets of cells; a horizontal run and a vertical run are always different positions.
Count how many different positions the worm can be in while sleeping.
Input
The first line contains an integer (), the number of test cases. Each test case is given as follows.
The first line of a test case contains three integers , , and (): the number of rows, the number of columns, and the number of stones. Each of the next lines contains two integers and , the row and column of one stone (, ). No stone is listed more than once.
Output
For each test case, print a single line containing the number of different positions in which the happy worm can sleep.