Elegant Diamond (Small)

Time limit5sMemory limit512 MB

Summary
You embed the given digit diamond in a larger diamond with horizontal and vertical symmetry and add as few digits as possible.
Level

Medium6 of 10

Topics
Brute force, Matrix
Solved
No attempts yet

Problem

The king hired you to make him an elegant diamond. An elegant diamond is a two dimensional figure made of digits that is symmetric about a horizontal axis and about a vertical axis. Each of the following four figures is an elegant diamond.

  2
 3 3
4 1 4
 3 3
  2

 8
8 8
 8

 3
2 2
 3

7

The next three figures are diamonds, but none of them is elegant.

 2
1 1
 1

  1
 1 2
1 1 1
 2 1
  1

  3
 1 1
3 1 3
 1 1
  2

The next three figures are not diamonds at all.

  1
 1 1

 2
222
 2

8   8
  0
00000

The king gives you one diamond, which may not be elegant. Your job is to make it elegant by enhancing it, that is, by adding digits so that it becomes a bigger diamond. You do not want to spend much, so you have to do it at the smallest possible cost.

Definitions

A diamond of size kk is 2k−12k-1 lines of digits from 0 to 9, separated by single spaces, laid out this way:

  • Line ii with 1≤i≤k1 \le i \le k has k−ik-i spaces, then ii digits separated by single spaces.
  • Line ii with k<i<2kk < i < 2k has i−ki-k spaces, then 2k−i2k-i digits separated by single spaces.

An elegant diamond of size kk is a diamond of size kk that has both of the following symmetry properties. Let cic_i be the number of digits on line ii.

  • Horizontal symmetry: the jjth digit on line ii (where j=1j=1 is the first digit) equals the (ci+1−j)(c_i+1-j)th digit on the same line.
  • Vertical symmetry: the jjth digit on line ii (where i=1i=1 is the first line) equals the jjth digit on line 2k−i2k-i.

A diamond of size kk is enhanced by adding digits to it. The result of enhancing a diamond of size kk has these properties:

  • The result is a diamond of size at least kk.
  • The original diamond is part of the result. In other words, there exist integers XX and YY such that, for every ii and jj where the jjth character of the iith line of the original is a digit rather than a space, the (j+X)(j+X)th character of the (i+Y)(i+Y)th line of the result is also a digit and holds the same value.

The cost of enhancing a diamond is the number of digits in the result minus the number of digits in the original diamond.

Input

The first line of the input has the number of test cases, TT. TT test cases follow. Each test case is a single integer kk on a line of its own, followed by a diamond of size kk.

Limits

  • 1≤T≤1001 \le T \le 100
  • 1≤k≤101 \le k \le 10

Output

For each test case, print one line in the form Case #x: y, where xx is the case number starting from 1 and yy is the minimum cost of enhancing the given diamond into an elegant diamond. If the diamond is already elegant, yy is 0.

Hint

The sample has four cases. The first two are already elegant diamonds, of size 1 and of size 2, so they need no enhancement and the cost is 0. The third one can be enhanced into this diamond:

  3
 1 1
1 2 1
 1 1
  3

Several enhancements are possible, and this one reaches the lowest cost, 5. The fourth one can be enhanced into this diamond:

   9
  1 1
 6 3 6
9 5 5 9
 6 3 6
  1 1
   9

That costs 7.

Examples6

  1. Example 1

    Input
    4
    1
    0
    2
     1
    2 2
     1
    2
     1
    1 2
     1
    3
      1
     6 3
    9 5 5
     6 3
      1
    
    Expected output
    Case #1: 0
    Case #2: 0
    Case #3: 5
    Case #4: 7
    
  2. Example 2

    Input
    1
    1
    7
    
    Expected output
    Case #1: 0
    
  3. Example 3

    Input
    10
    1
    3
    2
     3
    5 5
     3
    3
      2
     3 3
    7 5 7
     3 3
      2
    4
       4
      0 0
     2 7 2
    1 8 8 1
     2 7 2
      0 0
       4
    5
        3
       6 6
      6 9 6
     3 3 3 3
    6 6 4 6 6
     3 3 3 3
      6 9 6
       6 6
        3
    6
         6
        1 1
       0 6 0
      1 4 4 1
     7 2 8 2 7
    4 0 1 1 0 4
     7 2 8 2 7
      1 4 4 1
       0 6 0
        1 1
         6
    7
          1
         3 3
        5 4 5
       5 3 3 5
      1 5 8 5 1
     5 5 5 5 5 5
    0 1 3 2 3 1 0
     5 5 5 5 5 5
      1 5 8 5 1
       5 3 3 5
        5 4 5
         3 3
          1
    8
           3
          4 4
         4 0 4
        7 5 5 7
       8 7 4 7 8
      9 7 5 5 7 9
     9 9 8 8 8 9 9
    9 9 6 3 3 6 9 9
     9 9 8 8 8 9 9
      9 7 5 5 7 9
       8 7 4 7 8
        7 5 5 7
         4 0 4
          4 4
           3
    9
            2
           5 5
          0 6 0
         2 0 0 2
        7 3 1 3 7
       5 9 6 6 9 5
      4 6 1 9 1 6 4
     9 4 2 7 7 2 4 9
    8 2 3 0 2 0 3 2 8
     9 4 2 7 7 2 4 9
      4 6 1 9 1 6 4
       5 9 6 6 9 5
        7 3 1 3 7
         2 0 0 2
          0 6 0
           5 5
            2
    10
             0
            4 4
           6 8 6
          4 1 1 4
         9 9 9 9 9
        0 8 3 3 8 0
       5 1 8 0 8 1 5
      0 2 7 9 9 7 2 0
     5 6 7 6 6 6 7 6 5
    2 0 5 3 0 0 3 5 0 2
     5 6 7 6 6 6 7 6 5
      0 2 7 9 9 7 2 0
       5 1 8 0 8 1 5
        0 8 3 3 8 0
         9 9 9 9 9
          4 1 1 4
           6 8 6
            4 4
             0
    
    Expected output
    Case #1: 0
    Case #2: 0
    Case #3: 0
    Case #4: 0
    Case #5: 0
    Case #6: 0
    Case #7: 0
    Case #8: 0
    Case #9: 0
    Case #10: 0
    
  4. Example 4

    Input
    1
    2
     1
    2 3
     4
    
    Expected output
    Case #1: 12
    
  5. Example 5

    Input
    4
    2
     1
    2 2
     3
    2
     1
    2 3
     1
    3
      5
     4 4
    1 2 1
     4 4
      5
    3
      5
     4 6
    1 2 3
     4 6
      5
    
    Expected output
    Case #1: 5
    Case #2: 5
    Case #3: 0
    Case #4: 16
    
  6. Example 6

    Input
    4
    1
    3
    4
       3
      3 3
     3 3 3
    3 3 3 3
     3 3 3
      3 3
       3
    7
          3
         3 3
        3 3 3
       3 3 3 3
      3 3 3 3 3
     3 3 3 3 3 3
    3 3 3 3 3 3 3
     3 3 3 3 3 3
      3 3 3 3 3
       3 3 3 3
        3 3 3
         3 3
          3
    10
             3
            3 3
           3 3 3
          3 3 3 3
         3 3 3 3 3
        3 3 3 3 3 3
       3 3 3 3 3 3 3
      3 3 3 3 3 3 3 3
     3 3 3 3 3 3 3 3 3
    3 3 3 3 3 3 3 3 3 3
     3 3 3 3 3 3 3 3 3
      3 3 3 3 3 3 3 3
       3 3 3 3 3 3 3
        3 3 3 3 3 3
         3 3 3 3 3
          3 3 3 3
           3 3 3
            3 3
             3
    
    Expected output
    Case #1: 0
    Case #2: 0
    Case #3: 0
    Case #4: 0