Safe Squares (Small)
InterviewTime limit5sMemory limit512 MB
Count all axis-aligned D by D subgrids of an R by C grid that contain no monster cell.
- Level
Medium4 of 10
- Topics
- Dynamic programming, Matrix, Prefix sum, Implementation
- Solved
- No attempts yet
Problem
Codejamon trainers keep hunting for monsters, but to anyone who is not a trainer those monsters are dangerous. You want to find the safe spots that hold no monster at all.
Treat the world as a grid. Some cells are occupied by monsters. A safe square is a grid aligned block of cells, with , that contains no monster. Count how many safe squares of any size the whole world has.
Input
The first line gives the number of test cases, . test cases follow.
Each test case starts with a line holding three integers , , and . The grid has rows and columns and contains monsters. more lines follow. Each of them holds the row and the column of the cell where the -th monster stands. Rows are numbered from top to bottom starting at 0, and columns are numbered from left to right starting at 0.
Output
For each test case, print one line in the form Case #x: y, where is the test case number starting at 1 and is the total number of safe squares in that test case.
Constraints
- for . No two monsters share a cell.
Hint
The grid of the first test case of the example is:
0 0 0
0 0 0
0 1 0
Here 0 is a cell with no monster and 1 is a cell with a monster. It has 10 safe squares: 8 of size and 2 of size .
The grid of the second test case is:
0 1 0 1 1 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1 0
1 0 0 0 1 0 0 0 0 1 1
0 0 0 0 1 0 0 0 0 0 1
It has 51 safe squares: 32 of size , 13 of size , 5 of size , and 1 of size .