Cube IV (Small)
InterviewTime limit5sMemory limit512 MB
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 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 . Vincenzo writes on the rooms, one number per room and no number twice, and then puts one person in every room, 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 .
The first line of each test case contains the side length of the maze. The next lines contain 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 is the test case number starting from , is the number of the room the winner starts in, and 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
- Each of the numbers from to is written in exactly one room.