Mattress stain removal
Time limit3sMemory limit256 MB
Cover all stained cells on an m by n grid with the fewest 3 by 3 blocks placed inside the grid.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Bit manipulation
- Solved
- No attempts yet
Problem
Hongjun threw a party at his house over the Chuseok holiday. Many small children came, and the next morning he found several stains that had soaked through the bed sheet into the mattress. Washing the whole mattress costs too much, so Hongjun wants to buy a small stain removal tool instead.
The top surface of the mattress is a rectangular grid with rows and columns. The top left cell is and the bottom right cell is . The coordinate means the cell in row from the top and column from the left. One stain covers one cell.
The tool takes a block of cells that lies entirely inside the mattress and erases every stain in that block at once. The sides of the block are parallel to the sides of the mattress. The tool is thrown away after one use, so one tool clears one block. Given the size of the mattress and the positions of the stains, find the smallest number of tools needed to erase every stain.
On a mattress with stains at , , , , two tools are enough. The block spanning rows 4 to 6 and columns 3 to 5 erases and , and the block spanning rows 2 to 4 and columns 5 to 7 erases and .
Input
The first line contains the number of test cases . The test cases follow.
The first line of each test case contains two natural numbers and , the size of the mattress. (, ) The second line contains the number of stains . () Each of the next lines contains the row number and the column number of one stain, separated by a space. The row number is between and , the column number is between and , and no two stains share a coordinate.
Output
For each test case, print the smallest number of tools needed to erase every stain, one number per line.