Planetbacke
InterviewTime limit1sMemory limit1024 MB
Given a grid of digit heights with at most 7 cells per height, find the longest simple path that moves between 8-neighbors and never goes to a strictly higher cell.
- Level
Medium7 of 10
- Topics
- DFS, Backtracking, Graph, Implementation
- Solved
- No attempts yet
Problem
In a not too distant future, researchers have discovered not only {(\it one)} previously unknown planet here in our own solar system (see the problem Planet X) but now {(\it another one), called planet Y.
Researchers are interested in what the topography of planet Y looks like, and have managed to measure this with great precision. We represent the surface as an grid, where each cell has a measured height between 0 and 9.
To many people's delight, it turns out that planet Y has a perfect climate for skiing (as you surely understand, there is no longer any snow on Earth at this point). Write a program that calculates the longest ski slope that can be built on planet Y.
The requirement for a ski slope is that it must be a contiguous sequence of cells where every pair of cells borders each other either via a shared side or a shared corner (see the figures below), and where each cell in the sequence does not have a higher height than the preceding one. In principle, it is thus permitted for all cells in the ski slope to have the same height. The same cell may not be used more than once, but the ski slope could still cross itself through a corner as in the second example below.
Input
On the first line are two integers , the number of rows and columns in the grid. Then follow lines with characters each. The :th character on line is a digit between 0 and 9 corresponding to the height of the cell. There are never more than cells in the grid that have the same height.
Output
The program shall print an integer: the largest number of cells that can be included in an approved ski slope.
Hint
