Spinning Blade (Large)
Time limit5sMemory limit512 MB
Find the largest K by K square, with its four corner cells removed, whose cell masses balance exactly about the square center.
- Level
Medium6 of 10
- Topics
- Prefix sum, Brute force
- Solved
- No attempts yet
Problem
You are building a trap for your hideout: a spinning blade. The blade is cut out of a heavy metal sheet that carries a painted grid of square cells, rows by columns. First you cut out a square of cells aligned to the grid, where . Then you cut away the four corner cells of that square. What is left is the blade.
Every cell was supposed to have mass , but the sheet is not perfectly uniform, so the cell in row and column has mass . The shaft goes exactly through the center of the square, so the blade spins properly only if its center of mass sits exactly at that center.
Given the grid and the mass of every cell, find the largest such that some square inside the sheet gives a blade whose center of mass is exactly at the center of the square.
Input
The first line contains the number of test cases . Each test case starts with a line of three integers , and : the number of rows, the number of columns, and the mass you expected each cell to have. The next lines each contain digits with no spaces. The -th digit of the -th line is , the difference between the actual mass and the expected mass of that cell. Each cell has uniform density, and its mass is an integer between and , inclusive.
Limits
- The size of the input is at most 625KB.
Output
For each test case, print one line of the form Case #x: K, where is the test case number starting from 1 and is the largest blade size you can cut out. If no blade of size 3 or more has its center of mass at the center of its square, print IMPOSSIBLE in place of .
Note
The center of mass of a flat object is the point with this property: adding up over all points of the object gives the zero vector. Here , and the zero are two dimensional vectors. The definition still works when each grid cell is treated as a single point that carries all of its mass at the center of the cell.
For an example, take a sheet with whose digit rows are 123, 234 and 345 from top to bottom. The only candidate blade is the whole square with its four corner cells cut away. Put the origin at the bottom left corner of the sheet, let grow to the right and grow up, so every cell center lands on half integer coordinates. The five remaining cells have masses 9, 9, 10, 11 and 11, and their center of mass is :
The center of the square is , so this blade is not balanced.