Cube IV (Large)
InterviewTime limit5sMemory limit512 MB
Find the longest run of consecutive room numbers placed in neighboring cells and report its starting number and length.
- Level
Medium5 of 10
- Topics
- Dynamic programming, Graph, Array
- Solved
- No attempts yet
Problem
Vincenzo wanted to build Cube IV, but the budget only covered one square maze. The maze is an grid of rooms, so it holds rooms in total. Every room is a square with one door on each of its four sides, so it connects to the rooms above, below, to the left, and to the right of it.
Vincenzo wrote a number on each room, using every number from to exactly once. He then put one person in every room, people in all. A person may move only into a neighbouring room whose number is exactly larger than the number of the room they stand in. When no such neighbour exists, that person stops.
The person who moves the most times wins. Report the number of the room the winner started in and how many rooms that person stays in, counting the starting room.
Input
The first line contains the number of test cases, .
Each test case starts with , the number of rooms along one side of the maze. Then come the room numbers in grid order: the top row first, and within each row from left to right. The numbers are separated by whitespace, and the number of values on a line is not fixed.
Output
For each test case, print one line in the form Case #x: r d, where is the test case number starting from , is the number of the room the winner started in, and is how many rooms that person stays in.
If several people stay in the same largest number of rooms, the one who started in the smallest numbered room wins.
Constraints
- The sum of over all test cases is at most .
- Within one maze, every number from to appears exactly once.