Count all axis-aligned D by D subgrids of an R by C grid that contain no monster cell.
Medium4Dynamic programmingMatrixPrefix sumImplementationInterviewNo attempts yetTime limit5sMemory limit512 MBCodejamon 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 D×D cells, with D≥1, that contains no monster. Count how many safe squares of any size the whole world has.
The first line gives the number of test cases, T. T test cases follow.
Each test case starts with a line holding three integers R, C, and K. The grid has R rows and C columns and contains K monsters. K more lines follow. Each of them holds the row Ri and the column Ci of the cell where the i-th monster stands. Rows are numbered from top to bottom starting at 0, and columns are numbered from left to right starting at 0.
For each test case, print one line in the form Case #x: y, where x is the test case number starting at 1 and y is the total number of safe squares in that test case.
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 1×1 and 2 of size 2×2.
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 1×1, 13 of size 2×2, 5 of size 3×3, and 1 of size 4×4.