TopCoder problem "TopographicalImage" used in SRM 210 (Division II Level Three)

Problem Statement


Suppose you are standing at the highest point, called M0, of a mountainous landscape. As you look around, you wonder how many different points you could walk to starting from your lofty position without ever going uphill. The set of these points is called peak P0, and the entire landscape can be divided into peaks according to a similar definition. For i > 0, let Mi be the highest point of the landscape not contained in peaks P0 through Pi - 1, and let peak Pi be the set of points to which there is a path from Mi that never goes uphill (but may remain level) and never touches points already contained in P0 through Pi - 1. The number of peaks in the landscape is the smallest value of n for which all points of the landscape are contained in P0 through Pn - 1.

You have a topographical map of a rectangular landscape, and you are interested in the area of its peaks. Write a class TopographicalImage with a method calcPeakAreas that takes a String[] topoData containing the height of the landscape at each x and y position and returns a int[] with the areas of each peak. The ASCII value of character x of element y of topoData is the height of the landscape at point (x,y). You can walk from a point to each of its vertical, horizontal, and diagonal neighbors. The return value should have a number of elements equal to the number of peaks in the landscape, and element i should be the number of points in Pi. If there is a tie between multiple points for maximum height when choosing Mi, choose the point with the smallest y-coordinate. If there is still a tie between points with the same y-coordinate, choose the point with the smallest x-coordinate.



Method signature:int[] calcPeakAreas(String[] topoData)
(be sure your method is public)


-Point Mi is always contained in peak Pi, so the area of a peak is always at least 1.


-topoData will contain between 1 and 50 elements, inclusive.
-Each element of topoData will contain between 1 and 50 characters, inclusive.
-Each element of topoData will contain the same number of characters.
-Each element of topoData will contain only characters with ASCII value between 33 and 126, inclusive.


Returns: { 78,  3,  3 }
Returns: { 69,  3,  2,  5,  3,  1,  1 }
Returns: { 81,  6,  2,  1,  1 }
Returns: { 1 }
Returns: { 1918,  65,  483,  5,  5,  24 }

Problem url:

Problem stats url:




PabloGilberto , lbackstrom , brett1479

Problem categories:

Graph Theory, Recursion, Sorting