This page is still under construction.

Parts of this page are still being built. What you see may change.

Grid Aliens

Time limit1sMemory limit256 MB

Summary
Assign each cell of an N by N grid to one of two regions to maximize node rewards minus the cut edge costs.
Level

Medium7 of 10

Topics
Graph
Solved
No attempts yet

Problem

On one planet every alien aia_i lives on a point of an N×NN \times N grid. Some aliens like water and some do not. This tendency is called the hydrophilic property, and alien aia_i has its own degree hih_i, a real number with 0≤hi≤10 \le h_i \le 1. An alien that sits away from the border has 4 grid neighbours (up, down, left, right). An alien on a border edge has 3 neighbours, and an alien on a corner has 2.

The friendship of two neighbouring aliens aia_i and aja_j comes from the two hydrophilic degrees hih_i and hjh_j.

friend(ai,aj)=1−∣hi−hj∣\mathrm{friend}(a_i, a_j) = 1 - |h_i - h_j|

If two neighbours have the same hydrophilic degree, the friendship is perfect, friend(ai,aj)=1\mathrm{friend}(a_i, a_j) = 1. The critical situation friend(ai,aj)=0\mathrm{friend}(a_i, a_j) = 0 happens when hi=0h_i = 0 and hj=1h_j = 1. Friendship is not defined for a pair that is not adjacent, such as (a,c)(a, c) and (f,p)(f, p) in Figure 1.

The aliens decided to put up separating fences to reduce the conflicts between groups with different hydrophilic degrees. They first split the grid space into two disjoint regions: the W region for hydrophilic aliens, and the Q region for hydrophobic aliens, who dislike a watery environment and have small hydrophilic degrees. Then they build a closed fence around the Q region to keep conflicting aliens apart. The dotted red curves in Figure 1 are those fences. The aliens bb, cc, ff, xx, yy sit inside a fence and belong to the Q region, while aa, dd, ee, pp, qq sit outside and belong to the W region.

Figure 1. Three dotted red fences separate the Q region from the W region.

There are very many ways to set up the fences. When you build them, it is better to put hydrophilic aliens in the W region and hydrophobic aliens in the Q region. You also want the sum of friend(ai,aj)\mathrm{friend}(a_i, a_j) over the neighbour pairs with ai∈Wa_i \in W and aj∈Qa_j \in Q to be as small as possible. Here x∈Qx \in Q means that alien xx is assigned to the Q region. Every alien belongs to exactly one of Q and W.

The objective function KK of this (W,Q)(W, Q) separation problem is the following.

K=max⁡(CostW+CostQ−CostWQ)K = \max \left( \mathrm{Cost}_W + \mathrm{Cost}_Q - \mathrm{Cost}_{WQ} \right)

CostW=∑ai∈Whi,CostQ=∑aj∈Q(1−hj)\mathrm{Cost}_W = \sum_{a_i \in W} h_i, \qquad \mathrm{Cost}_Q = \sum_{a_j \in Q} (1 - h_j)

CostWQ=∑ai∈W, aj∈Qfriend(ai,aj)\mathrm{Cost}_{WQ} = \sum_{a_i \in W,\ a_j \in Q} \mathrm{friend}(a_i, a_j)

The sum in CostWQ\mathrm{Cost}_{WQ} runs over every pair (ai,aj)(a_i, a_j) joined by a grid edge.

Write a program that computes KK from the hydrophilic degrees of the aliens on the grid points.

Input

Your program reads from standard input. The first line holds the number of test cases TT. The first line of each test case holds the grid size NN (3≤N≤503 \le N \le 50). The next NN lines hold the hydrophilic degree hi,jh_{i,j} of the alien living at grid position (i,j)(i, j) as an N×NN \times N matrix. Every hi,jh_{i,j} is a real number with 0≤hi,j≤10 \le h_{i,j} \le 1 and exactly two digits after the decimal point.

Output

Your program writes to standard output. Print exactly one line for each test case. The line holds the real number KK with two digits after the decimal point.

Examples2

  1. Example 1

    Input
    2
    4
    0.94 0.89 0.99 0.11
    0.77 0.87 0.87 0.93
    0.78 0.05 0.89 0.15
    0.95 0.15 0.14 0.05
    5
    0.21 0.21 0.83 0.21 0.21
    0.21 0.83 0.83 0.83 0.21
    0.21 0.83 0.21 0.83 0.21
    0.21 0.83 0.83 0.83 0.21
    0.21 0.83 0.83 0.21 0.21
    
    Expected output
    12.39
    14.67
    
  2. Example 2

    Input
    1
    3
    0.50 0.50 0.50
    0.50 0.50 0.50
    0.50 0.50 0.50
    
    Expected output
    4.50