Under construction

Still being built. Things may change

Cube IV (Small)

Interview

Time limit5sMemory limit512 MB

Summary
Given a square grid holding 1 to S squared, find the smallest start of the longest chain that steps to an orthogonal neighbor one higher and report its length.
Level

Medium4 of 10

Topics
DFS, Dynamic programming, Matrix
Solved
No attempts yet

Problem

Vincenzo wanted to build cube IV, but his budget only covered one square maze. Every room of the maze is a square and has four doors, one on each side. A big number is written in every room.

A person moves from the room he is standing in to a neighboring room only when the number written there is exactly 11 larger than the number in his own room. Two rooms are neighbors when they share a side with a door in it.

The maze has side length SS. Vincenzo writes 1,2,3,…,S21, 2, 3, \dots, S^2 on the S2S^2 rooms, one number per room and no number twice, and then puts one person in every room, S2S^2 people in total. The person who moves the largest number of times wins. Find the number of the room the winner starts in and how many rooms he passes through. The room he starts in counts as one of them.

Input

The first line contains the number of test cases TT.

The first line of each test case contains the side length SS of the maze. The next SS lines contain SS numbers each, giving the numbers written in the rooms row by row from the top row of the maze.

Output

For each test case, print one line of the form Case #x: r d, where xx is the test case number starting from 11, rr is the number of the room the winner starts in, and dd is the number of rooms he passes through.

If several people move the largest number of times, the one standing in the room with the smallest number wins.

Limits

  • 1≤T≤1001 \le T \le 100
  • 1≤S≤101 \le S \le 10
  • Each of the numbers from 11 to S2S^2 is written in exactly one room.

Examples5

  1. Example 1

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

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

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

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

    Input
    1
    3
    1 3 4
    2 6 5
    7 8 9
    
    Expected output
    Case #1: 3 4