#P13375. [GCJ 2011 #2] Spinning Blade

[GCJ 2011 #2] Spinning Blade

题目描述

Being bored with the traps in your secret hideout design, you decided to go for something classical, but always enjoyable - the spinning blade. You ordered a really heavy metal sheet out of which you will cut the blade; a uniform square CC-by-RR grid will be painted on the sheet. You have determined the best shape for the blade -- you will first cut a large square consisting of KK-by-KK grid cells, where K3K \geq 3. Then, you will cut out the four 1-by-1 corner cells out of the square to end up with a blade. After determining all this, you started waiting for the sheet to arrive.

When the sheet arrived, you were shocked to find out that the sheet had imperfections in it! You expected each cell to have mass DD, but it turned out that the mass can vary a bit because of differences in thickness. This is bad because you want to insert a shaft exactly in the center of the blade and spin it very fast, so the center of mass of the blade must be exactly in its center as well. The definition of the center of mass of a flat body can be found below.

Given the grid and the mass of each cell, what is the largest possible size of the blade you can make so that the center of mass is exactly in its center?

输入格式

The first line of the input gives the number of test cases, TT. TT test cases follow. Each one starts with a line containing 3 integers: RR, CC and DD — the dimensions of the grid and the mass you expected each cell to have. The next RR lines each contain CC digits wijw_{ij} each, giving the differences between the actual and the expected mass of the grid cells. Each cell has a uniform density, but could have an integer mass between D+0D+0 and D+9D+9, inclusive.

输出格式

For each test case, output one line containing "Case #xx: KK", where xx is the case number (starting from 11) and KK is the largest possible size of the blade you can cut out. If no acceptable blade of size at least 33 can be found, print "IMPOSSIBLE" instead.

2
6 7 2
1111111
1122271
1211521
1329131
1242121
1122211
3 3 7
123
234
345
Case #1: 5
Case #2: IMPOSSIBLE

提示

Sample Explanation

The center of mass of a 2D object is formally defined as a point cc. If you compute the sum of (pc)×mass(p)(p - c) \times \text{mass}(p) for all points pp in the object, you must get 00. Here, pp, cc and 00 are two-dimensional vectors. This definition also works if you treat each grid cell as a "point", with all of its mass at its center.

In real life, you could place your finger under a flat object's center of mass, and balance that object on your finger. It would not fall.

To illustrate with an example, the only blade that is possible to cut out in the second sample test case, the 3×33\times 3 blade created by cutting away the corners, has its center of mass at the point (1.54,1.46)(1.54, 1.46), where we assume the bottom-left corner of the sheet has coordinates (0,0)(0, 0), and the coordinates grow right and up, respectively. This is verified by checking the following equality: $(-1.04, 0.04) \times 9 + (-0.04, 1.04) \times 9 + (-0.04, 0.04) \times 10 + (-0.04, -0.96) \times 11 + (0.96, 0.04) \times 11 = (0, 0)$.

Limits

  • 1T201 \leq T \leq 20.
  • 0wij90 \leq w_{ij} \leq 9.
  • The size of the input file will not exceed 625KB.

Small dataset (8 Pts, Test set 1 - Visible)

  • 3R103 \leq R \leq 10. -3C103 \leq C \leq 10.
  • 1D1001 \leq D \leq 100.
  • Time limit: 30 3 seconds.

Large dataset (12 Pts, Test set 2 - Hidden)

  • 3R5003 \leq R \leq 500.
  • 3C5003 \leq C \leq 500.
  • 1D1061 \leq D \leq 10^6.
  • Time limit: 60 6 seconds.